From 45137d5506a83487d5b77578ed48267266e989df Mon Sep 17 00:00:00 2001 From: 21pages Date: Sat, 27 Apr 2024 23:24:07 +0800 Subject: [PATCH] fix switch to setting page (#7849) Signed-off-by: 21pages --- .../lib/desktop/pages/desktop_home_page.dart | 11 +- .../desktop/pages/desktop_setting_page.dart | 137 ++++++++++++------ .../lib/desktop/pages/desktop_tab_page.dart | 5 +- 3 files changed, 104 insertions(+), 49 deletions(-) diff --git a/flutter/lib/desktop/pages/desktop_home_page.dart b/flutter/lib/desktop/pages/desktop_home_page.dart index 6ec9fd360..0f190b181 100644 --- a/flutter/lib/desktop/pages/desktop_home_page.dart +++ b/flutter/lib/desktop/pages/desktop_home_page.dart @@ -153,7 +153,13 @@ class _DesktopHomePageState extends State size: 22, ), ), - onTap: () => DesktopSettingPage.switch2page(0), + onTap: () => { + if (DesktopSettingPage.tabKeys.isNotEmpty) + { + DesktopSettingPage.switch2page( + DesktopSettingPage.tabKeys[0]) + } + }, onHover: (value) => _editHover.value = value, ), ), @@ -347,7 +353,8 @@ class _DesktopHomePageState extends State ).marginOnly(right: 8, top: 4), ), ), - onTap: () => DesktopSettingPage.switch2page(0), + onTap: () => DesktopSettingPage.switch2page( + SettingsTabKey.safety), onHover: (value) => editHover.value = value, ), ], diff --git a/flutter/lib/desktop/pages/desktop_setting_page.dart b/flutter/lib/desktop/pages/desktop_setting_page.dart index 17998dd9a..ba61ef7aa 100644 --- a/flutter/lib/desktop/pages/desktop_setting_page.dart +++ b/flutter/lib/desktop/pages/desktop_setting_page.dart @@ -36,34 +36,57 @@ const double _kTitleFontSize = 20; const double _kContentFontSize = 15; const Color _accentColor = MyTheme.accent; const String _kSettingPageControllerTag = 'settingPageController'; -const String _kSettingPageIndexTag = 'settingPageIndex'; -const int _kPageCount = 6; +const String _kSettingPageTabKeyTag = 'settingPageTabKey'; class _TabInfo { + late final SettingsTabKey key; late final String label; late final IconData unselected; late final IconData selected; - _TabInfo(this.label, this.unselected, this.selected); + _TabInfo(this.key, this.label, this.unselected, this.selected); +} + +enum SettingsTabKey { + general, + safety, + network, + display, + plugin, + account, + about, } class DesktopSettingPage extends StatefulWidget { - final int initialPage; + final SettingsTabKey initialTabkey; + static final List tabKeys = [ + SettingsTabKey.general, + if (!bind.isOutgoingOnly() && !bind.isDisableSettings()) + SettingsTabKey.safety, + if (!bind.isDisableSettings()) SettingsTabKey.network, + if (!bind.isIncomingOnly()) SettingsTabKey.display, + if (!isWeb && !bind.isIncomingOnly() && bind.pluginFeatureIsEnabled()) + SettingsTabKey.plugin, + if (!bind.isDisableAccount()) SettingsTabKey.account, + SettingsTabKey.about, + ]; - const DesktopSettingPage({Key? key, required this.initialPage}) - : super(key: key); + DesktopSettingPage({Key? key, required this.initialTabkey}) : super(key: key); @override State createState() => _DesktopSettingPageState(); - static void switch2page(int page) { - if (page >= _kPageCount) return; + static void switch2page(SettingsTabKey page) { try { + int index = tabKeys.indexOf(page); + if (index == -1) { + return; + } if (Get.isRegistered(tag: _kSettingPageControllerTag)) { DesktopTabPage.onAddSetting(initialPage: page); PageController controller = Get.find(tag: _kSettingPageControllerTag); - RxInt selectedIndex = Get.find(tag: _kSettingPageIndexTag); - selectedIndex.value = page; - controller.jumpToPage(page); + Rx selected = Get.find(tag: _kSettingPageTabKeyTag); + selected.value = page; + controller.jumpToPage(index); } else { DesktopTabPage.onAddSetting(initialPage: page); } @@ -76,7 +99,7 @@ class DesktopSettingPage extends StatefulWidget { class _DesktopSettingPageState extends State with TickerProviderStateMixin, AutomaticKeepAliveClientMixin { late PageController controller; - late RxInt selectedIndex; + late Rx selectedTab; @override bool get wantKeepAlive => true; @@ -84,14 +107,20 @@ class _DesktopSettingPageState extends State @override void initState() { super.initState(); - selectedIndex = - (widget.initialPage < _kPageCount ? widget.initialPage : 0).obs; - Get.put(selectedIndex, tag: _kSettingPageIndexTag); - controller = PageController(initialPage: widget.initialPage); + var initialIndex = DesktopSettingPage.tabKeys.indexOf(widget.initialTabkey); + if (initialIndex == -1) { + initialIndex = 0; + } + selectedTab = DesktopSettingPage.tabKeys[initialIndex].obs; + Get.put>(selectedTab, tag: _kSettingPageTabKeyTag); + controller = PageController(initialPage: initialIndex); Get.put(controller, tag: _kSettingPageControllerTag); controller.addListener(() { if (controller.page != null) { - selectedIndex.value = controller.page!.toInt(); + int page = controller.page!.toInt(); + if (page < DesktopSettingPage.tabKeys.length) { + selectedTab.value = DesktopSettingPage.tabKeys[page]; + } } }); } @@ -100,26 +129,43 @@ class _DesktopSettingPageState extends State void dispose() { super.dispose(); Get.delete(tag: _kSettingPageControllerTag); - Get.delete(tag: _kSettingPageIndexTag); + Get.delete(tag: _kSettingPageTabKeyTag); } List<_TabInfo> _settingTabs() { - final List<_TabInfo> settingTabs = <_TabInfo>[ - _TabInfo('General', Icons.settings_outlined, Icons.settings), - if (!bind.isOutgoingOnly() && !bind.isDisableSettings()) - _TabInfo('Security', Icons.enhanced_encryption_outlined, - Icons.enhanced_encryption), - if (!bind.isDisableSettings()) - _TabInfo('Network', Icons.link_outlined, Icons.link), - if (!bind.isIncomingOnly()) - _TabInfo( - 'Display', Icons.desktop_windows_outlined, Icons.desktop_windows), - if (!isWeb && !bind.isIncomingOnly() && bind.pluginFeatureIsEnabled()) - _TabInfo('Plugin', Icons.extension_outlined, Icons.extension), - if (!bind.isDisableAccount()) - _TabInfo('Account', Icons.person_outline, Icons.person), - _TabInfo('About', Icons.info_outline, Icons.info) - ]; + final List<_TabInfo> settingTabs = <_TabInfo>[]; + for (final tab in DesktopSettingPage.tabKeys) { + switch (tab) { + case SettingsTabKey.general: + settingTabs.add(_TabInfo( + tab, 'General', Icons.settings_outlined, Icons.settings)); + break; + case SettingsTabKey.safety: + settingTabs.add(_TabInfo(tab, 'Security', + Icons.enhanced_encryption_outlined, Icons.enhanced_encryption)); + break; + case SettingsTabKey.network: + settingTabs + .add(_TabInfo(tab, 'Network', Icons.link_outlined, Icons.link)); + break; + case SettingsTabKey.display: + settingTabs.add(_TabInfo(tab, 'Display', + Icons.desktop_windows_outlined, Icons.desktop_windows)); + break; + case SettingsTabKey.plugin: + settingTabs.add(_TabInfo( + tab, 'Plugin', Icons.extension_outlined, Icons.extension)); + break; + case SettingsTabKey.account: + settingTabs.add( + _TabInfo(tab, 'Account', Icons.person_outline, Icons.person)); + break; + case SettingsTabKey.about: + settingTabs + .add(_TabInfo(tab, 'About', Icons.info_outline, Icons.info)); + break; + } + } return settingTabs; } @@ -198,26 +244,26 @@ class _DesktopSettingPageState extends State child: ListView( physics: DraggableNeverScrollableScrollPhysics(), controller: scrollController, - children: tabs - .asMap() - .entries - .map((tab) => _listItem(tab: tab.value, index: tab.key)) - .toList(), + children: tabs.map((tab) => _listItem(tab: tab)).toList(), )); } - Widget _listItem({required _TabInfo tab, required int index}) { + Widget _listItem({required _TabInfo tab}) { return Obx(() { - bool selected = index == selectedIndex.value; + bool selected = tab.key == selectedTab.value; return SizedBox( width: _kTabWidth, height: _kTabHeight, child: InkWell( onTap: () { - if (selectedIndex.value != index) { + if (selectedTab.value != tab.key) { + int index = DesktopSettingPage.tabKeys.indexOf(tab.key); + if (index == -1) { + return; + } controller.jumpToPage(index); } - selectedIndex.value = index; + selectedTab.value = tab.key; }, child: Row(children: [ Container( @@ -2064,8 +2110,9 @@ void changeSocks5Proxy() async { Expanded( child: TextField( decoration: InputDecoration( - errorText: proxyMsg.isNotEmpty ? proxyMsg : null, - hintText: translate('Default protocol and port are Socks5 and 1080'), + errorText: proxyMsg.isNotEmpty ? proxyMsg : null, + hintText: translate( + 'Default protocol and port are Socks5 and 1080'), ), controller: proxyController, autofocus: true, diff --git a/flutter/lib/desktop/pages/desktop_tab_page.dart b/flutter/lib/desktop/pages/desktop_tab_page.dart index 46d15be54..99a28206d 100644 --- a/flutter/lib/desktop/pages/desktop_tab_page.dart +++ b/flutter/lib/desktop/pages/desktop_tab_page.dart @@ -17,7 +17,8 @@ class DesktopTabPage extends StatefulWidget { @override State createState() => _DesktopTabPageState(); - static void onAddSetting({int initialPage = 0}) { + static void onAddSetting( + {SettingsTabKey initialPage = SettingsTabKey.general}) { try { DesktopTabController tabController = Get.find(); tabController.add(TabInfo( @@ -27,7 +28,7 @@ class DesktopTabPage extends StatefulWidget { unselectedIcon: Icons.build_outlined, page: DesktopSettingPage( key: const ValueKey(kTabLabelSettingPage), - initialPage: initialPage, + initialTabkey: initialPage, ))); } catch (e) { debugPrintStack(label: '$e');