From 96f3ca3baef6879b11f9d4de1760a5430b8d1baf Mon Sep 17 00:00:00 2001 From: Bo Lopker Date: Thu, 31 Oct 2024 10:22:38 +0700 Subject: [PATCH] Better config page selector --- lib/settings.dart | 98 +++++++++++++++++++++++++++++------------------ pubspec.lock | 8 ++++ pubspec.yaml | 1 + 3 files changed, 69 insertions(+), 38 deletions(-) diff --git a/lib/settings.dart b/lib/settings.dart index c94a509..50dd448 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -2,7 +2,7 @@ import 'package:alic/config.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:signals/signals_flutter.dart'; - +import 'package:custom_sliding_segmented_control/custom_sliding_segmented_control.dart'; import 'src/rust/api/compressor.dart'; enum SettingsPages { @@ -29,43 +29,65 @@ class _SettingsWidgetState extends State { @override Widget build(BuildContext context) { return Dialog( - child: Padding( - padding: const EdgeInsets.all(20.0), - child: Column(children: [ - SegmentedButton( - selectedIcon: Container(), - onSelectionChanged: (p0) { - final newPage = SettingsPages.values.firstWhere( - (element) => element.toString() == p0.first.toString()); - setState(() { - _selectedPage = newPage; - }); - }, - segments: SettingsPages.values - .map((e) => - ButtonSegment(value: e.toString(), label: Text(e.title))) - .toList(), - selected: {_selectedPage.toString()}, - ), - const SizedBox(height: 10), - _getSelectedPage(), - const Spacer(), - Row( - children: [ - TextButton( - onPressed: () { - Config.reset(); - }, - child: const Text('Reset')), - const Spacer(), - TextButton( - onPressed: () { - Navigator.pop(context); - }, - child: const Text('Close')), - ], - ) - ]), + child: KeyboardListener( + focusNode: FocusNode(), + onKeyEvent: (event) { + var nextPage = SettingsPages.values[ + (SettingsPages.values.indexOf(_selectedPage) + 1) % + SettingsPages.values.length]; + if (event.logicalKey == LogicalKeyboardKey.tab) { + setState(() { + _selectedPage = nextPage; + }); + } + }, + child: Padding( + padding: const EdgeInsets.all(20.0), + child: Column(children: [ + CustomSlidingSegmentedControl( + initialValue: _selectedPage, + clipBehavior: Clip.antiAlias, + children: SettingsPages.values.fold({}, (map, element) { + map[element] = Text(element.title); + return map; + }), + decoration: BoxDecoration( + color: Colors.black, + borderRadius: BorderRadius.circular(16), + ), + thumbDecoration: BoxDecoration( + color: Theme.of(context).colorScheme.secondaryContainer, + borderRadius: BorderRadius.circular(16), + ), + customSegmentSettings: CustomSegmentSettings( + borderRadius: BorderRadius.circular(25), + ), + onValueChanged: (newPage) { + setState(() { + _selectedPage = newPage; + }); + }, + ), + const SizedBox(height: 10), + _getSelectedPage(), + const Spacer(), + Row( + children: [ + TextButton( + onPressed: () { + Config.reset(); + }, + child: const Text('Reset')), + const Spacer(), + TextButton( + onPressed: () { + Navigator.pop(context); + }, + child: const Text('Close')), + ], + ) + ]), + ), ), ); } diff --git a/pubspec.lock b/pubspec.lock index a821f0f..efc1cf7 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -246,6 +246,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.5.14" + custom_sliding_segmented_control: + dependency: "direct main" + description: + name: custom_sliding_segmented_control + sha256: "53c3e931c3ae1f696085d1ec70ac8e934da836595a9b7d9b88fdd0fcbf2a5574" + url: "https://pub.dev" + source: hosted + version: "1.8.3" dart_style: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 921a8b8..e54100f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,6 +27,7 @@ dependencies: package_info_plus: ^8.0.0 http: ^1.2.1 url_launcher: ^6.2.6 + custom_sliding_segmented_control: ^1.8.3 dev_dependencies: flutter_test: