fix switch to setting page (#7849)

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2024-04-27 23:24:07 +08:00 committed by GitHub
parent e9d9a656ab
commit 45137d5506
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 104 additions and 49 deletions

View File

@ -153,7 +153,13 @@ class _DesktopHomePageState extends State<DesktopHomePage>
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<DesktopHomePage>
).marginOnly(right: 8, top: 4),
),
),
onTap: () => DesktopSettingPage.switch2page(0),
onTap: () => DesktopSettingPage.switch2page(
SettingsTabKey.safety),
onHover: (value) => editHover.value = value,
),
],

View File

@ -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<SettingsTabKey> 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<DesktopSettingPage> 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<PageController>(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<SettingsTabKey> 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<DesktopSettingPage>
with TickerProviderStateMixin, AutomaticKeepAliveClientMixin {
late PageController controller;
late RxInt selectedIndex;
late Rx<SettingsTabKey> selectedTab;
@override
bool get wantKeepAlive => true;
@ -84,14 +107,20 @@ class _DesktopSettingPageState extends State<DesktopSettingPage>
@override
void initState() {
super.initState();
selectedIndex =
(widget.initialPage < _kPageCount ? widget.initialPage : 0).obs;
Get.put<RxInt>(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<Rx<SettingsTabKey>>(selectedTab, tag: _kSettingPageTabKeyTag);
controller = PageController(initialPage: initialIndex);
Get.put<PageController>(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<DesktopSettingPage>
void dispose() {
super.dispose();
Get.delete<PageController>(tag: _kSettingPageControllerTag);
Get.delete<RxInt>(tag: _kSettingPageIndexTag);
Get.delete<RxInt>(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<DesktopSettingPage>
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,

View File

@ -17,7 +17,8 @@ class DesktopTabPage extends StatefulWidget {
@override
State<DesktopTabPage> 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');