mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
feat: navigation Bar编辑
Co-authored-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -2,25 +2,21 @@ import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:PiliPalaX/grpc/grpc_repo.dart';
|
||||
import 'package:PiliPalaX/pages/dynamics/view.dart';
|
||||
import 'package:PiliPalaX/pages/home/view.dart';
|
||||
import 'package:PiliPalaX/pages/media/view.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:PiliPalaX/pages/dynamics/index.dart';
|
||||
import 'package:PiliPalaX/pages/home/view.dart';
|
||||
import 'package:PiliPalaX/pages/media/index.dart';
|
||||
import 'package:PiliPalaX/utils/storage.dart';
|
||||
import '../../models/common/dynamic_badge_mode.dart';
|
||||
import '../../models/common/nav_bar_config.dart';
|
||||
|
||||
class MainController extends GetxController {
|
||||
List<Widget> pages = <Widget>[
|
||||
const HomePage(),
|
||||
// const RankPage(),
|
||||
const DynamicsPage(),
|
||||
const MediaPage(),
|
||||
];
|
||||
RxList navigationBars = defaultNavigationBars.obs;
|
||||
List<Widget> pages = <Widget>[];
|
||||
RxList navigationBars = [].obs;
|
||||
|
||||
final StreamController<bool> bottomBarStream =
|
||||
StreamController<bool>.broadcast();
|
||||
Box setting = GStorage.setting;
|
||||
@@ -33,30 +29,31 @@ class MainController extends GetxController {
|
||||
late bool checkDynamic;
|
||||
late int dynamicPeriod;
|
||||
int? _lastCheckAt;
|
||||
int? dynIndex;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
checkDynamic = GStorage.checkDynamic;
|
||||
dynamicPeriod = GStorage.dynamicPeriod;
|
||||
// if (setting.get(SettingBoxKey.autoUpdate, defaultValue: false)) {
|
||||
// Utils.checkUpdate();
|
||||
// }
|
||||
hideTabBar = setting.get(SettingBoxKey.hideTabBar, defaultValue: true);
|
||||
int defaultHomePage =
|
||||
setting.get(SettingBoxKey.defaultHomePage, defaultValue: 0) as int;
|
||||
selectedIndex = defaultNavigationBars
|
||||
.indexWhere((item) => item['id'] == defaultHomePage);
|
||||
dynamic userInfo = userInfoCache.get('userInfoCache');
|
||||
userLogin.value = userInfo != null;
|
||||
dynamicBadgeType = DynamicBadgeMode.values[setting.get(
|
||||
SettingBoxKey.dynamicBadgeMode,
|
||||
defaultValue: DynamicBadgeMode.number.code)];
|
||||
|
||||
setNavBarConfig();
|
||||
if (dynamicBadgeType != DynamicBadgeMode.hidden) {
|
||||
if (checkDynamic) {
|
||||
_lastCheckAt = DateTime.now().millisecondsSinceEpoch;
|
||||
dynIndex = navigationBars.indexWhere((e) => e['id'] == 1);
|
||||
if (dynIndex != -1) {
|
||||
if (checkDynamic) {
|
||||
_lastCheckAt = DateTime.now().millisecondsSinceEpoch;
|
||||
}
|
||||
getUnreadDynamic();
|
||||
} else {
|
||||
checkDynamic = false;
|
||||
}
|
||||
getUnreadDynamic();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,36 +79,28 @@ class MainController extends GetxController {
|
||||
}
|
||||
|
||||
void getUnreadDynamic() async {
|
||||
if (!userLogin.value) {
|
||||
if (!userLogin.value || dynIndex == null || dynIndex == -1) {
|
||||
return;
|
||||
}
|
||||
// not needed yet
|
||||
// int dynamicItemIndex =
|
||||
// navigationBars.indexWhere((item) => item['label'] == "动态");
|
||||
// if (dynamicItemIndex == -1) return;
|
||||
// var res = await CommonHttp.unReadDynamic();
|
||||
// var data = res['data'];
|
||||
// navigationBars[1]['count'] =
|
||||
// data == null ? 0 : data.length; // 修改 count 属性为新的值
|
||||
await GrpcRepo.dynRed().then((res) {
|
||||
if (res['status']) {
|
||||
navigationBars[1]['count'] = res['data'];
|
||||
navigationBars[dynIndex!]['count'] = res['data'];
|
||||
}
|
||||
});
|
||||
navigationBars.refresh();
|
||||
}
|
||||
|
||||
void clearUnread() async {
|
||||
// not needed yet
|
||||
// int dynamicItemIndex =
|
||||
// navigationBars.indexWhere((item) => item['label'] == "动态");
|
||||
// if (dynamicItemIndex == -1) return;
|
||||
navigationBars[1]['count'] = 0; // 修改 count 属性为新的值
|
||||
navigationBars.refresh();
|
||||
if (dynamicBadgeType != DynamicBadgeMode.hidden) {
|
||||
navigationBars[dynIndex!]['count'] = 0; // 修改 count 属性为新的值
|
||||
navigationBars.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
void checkUnreadDynamic() {
|
||||
if (!userLogin.value ||
|
||||
if (dynIndex == null ||
|
||||
dynIndex == -1 ||
|
||||
!userLogin.value ||
|
||||
dynamicBadgeType == DynamicBadgeMode.hidden ||
|
||||
!checkDynamic) return;
|
||||
int now = DateTime.now().millisecondsSinceEpoch;
|
||||
@@ -120,4 +109,28 @@ class MainController extends GetxController {
|
||||
getUnreadDynamic();
|
||||
}
|
||||
}
|
||||
|
||||
void setNavBarConfig() async {
|
||||
List defaultNavTabs = [...defaultNavigationBars];
|
||||
List navBarSort =
|
||||
setting.get(SettingBoxKey.navBarSort, defaultValue: [0, 1, 2]);
|
||||
defaultNavTabs.retainWhere((item) => navBarSort.contains(item['id']));
|
||||
defaultNavTabs.sort((a, b) =>
|
||||
navBarSort.indexOf(a['id']).compareTo(navBarSort.indexOf(b['id'])));
|
||||
navigationBars.value = defaultNavTabs;
|
||||
int defaultHomePage =
|
||||
setting.get(SettingBoxKey.defaultHomePage, defaultValue: 0) as int;
|
||||
int defaultIndex =
|
||||
navigationBars.indexWhere((item) => item['id'] == defaultHomePage);
|
||||
// 如果找不到匹配项,默认索引设置为0或其他合适的值
|
||||
selectedIndex = defaultIndex != -1 ? defaultIndex : 0;
|
||||
pages = navigationBars
|
||||
.map<Widget>((e) => switch (e['id']) {
|
||||
0 => HomePage(),
|
||||
1 => DynamicsPage(),
|
||||
2 => MediaPage(),
|
||||
_ => throw UnimplementedError(),
|
||||
})
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,58 +152,71 @@ class _MainAppState extends State<MainApp>
|
||||
36.801 +
|
||||
MediaQuery.of(context).padding.left,
|
||||
child: Obx(
|
||||
() => NavigationRail(
|
||||
groupAlignment: 1,
|
||||
minWidth: context.width * 0.0286 + 28.56,
|
||||
backgroundColor: Colors.transparent,
|
||||
selectedIndex: _mainController.selectedIndex,
|
||||
onDestinationSelected: (value) => setIndex(value),
|
||||
labelType: NavigationRailLabelType.none,
|
||||
leading: UserAndSearchVertical(ctr: _homeController),
|
||||
destinations: _mainController.navigationBars
|
||||
.map((e) => NavigationRailDestination(
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
),
|
||||
selectedIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
),
|
||||
label: Text(e['label']),
|
||||
padding: EdgeInsets.symmetric(
|
||||
vertical: 0.01 * context.height),
|
||||
))
|
||||
.toList(),
|
||||
trailing: SizedBox(height: 0.1 * context.height),
|
||||
),
|
||||
() => _mainController.navigationBars.length > 1
|
||||
? NavigationRail(
|
||||
groupAlignment: 1,
|
||||
minWidth: context.width * 0.0286 + 28.56,
|
||||
backgroundColor: Colors.transparent,
|
||||
selectedIndex: _mainController.selectedIndex,
|
||||
onDestinationSelected: setIndex,
|
||||
labelType: NavigationRailLabelType.none,
|
||||
leading:
|
||||
UserAndSearchVertical(ctr: _homeController),
|
||||
destinations: _mainController.navigationBars
|
||||
.map((e) => NavigationRailDestination(
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
),
|
||||
selectedIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
),
|
||||
label: Text(e['label']),
|
||||
padding: EdgeInsets.symmetric(
|
||||
vertical: 0.01 * context.height),
|
||||
))
|
||||
.toList(),
|
||||
trailing: SizedBox(height: 0.1 * context.height),
|
||||
)
|
||||
: Container(
|
||||
padding: EdgeInsets.only(
|
||||
top: MediaQuery.paddingOf(context).top + 10),
|
||||
constraints: BoxConstraints(
|
||||
minWidth: context.width * 0.0286 + 28.56,
|
||||
),
|
||||
child:
|
||||
UserAndSearchVertical(ctr: _homeController),
|
||||
),
|
||||
),
|
||||
),
|
||||
] else if (!isPortait)
|
||||
Obx(
|
||||
() => NavigationRail(
|
||||
onDestinationSelected: (value) => setIndex(value),
|
||||
selectedIndex: _mainController.selectedIndex,
|
||||
destinations: _mainController.navigationBars
|
||||
.map(
|
||||
(e) => NavigationRailDestination(
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
),
|
||||
selectedIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
),
|
||||
label: Text(e['label']),
|
||||
),
|
||||
() => _mainController.navigationBars.length > 1
|
||||
? NavigationRail(
|
||||
onDestinationSelected: setIndex,
|
||||
selectedIndex: _mainController.selectedIndex,
|
||||
destinations: _mainController.navigationBars
|
||||
.map(
|
||||
(e) => NavigationRailDestination(
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
),
|
||||
selectedIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
),
|
||||
label: Text(e['label']),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
: const SizedBox.shrink(),
|
||||
),
|
||||
VerticalDivider(
|
||||
width: 1,
|
||||
@@ -240,60 +253,65 @@ class _MainAppState extends State<MainApp>
|
||||
offset: Offset(0, snapshot.data ? 0 : 1),
|
||||
child: enableMYBar
|
||||
? Obx(
|
||||
() => NavigationBar(
|
||||
onDestinationSelected: (value) =>
|
||||
setIndex(value),
|
||||
selectedIndex: _mainController.selectedIndex,
|
||||
destinations:
|
||||
_mainController.navigationBars.map(
|
||||
(e) {
|
||||
return NavigationDestination(
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
),
|
||||
selectedIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
),
|
||||
label: e['label'],
|
||||
);
|
||||
},
|
||||
).toList(),
|
||||
),
|
||||
() => _mainController.navigationBars.length > 1
|
||||
? NavigationBar(
|
||||
onDestinationSelected: setIndex,
|
||||
selectedIndex:
|
||||
_mainController.selectedIndex,
|
||||
destinations:
|
||||
_mainController.navigationBars.map(
|
||||
(e) {
|
||||
return NavigationDestination(
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
),
|
||||
selectedIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
),
|
||||
label: e['label'],
|
||||
);
|
||||
},
|
||||
).toList(),
|
||||
)
|
||||
: const SizedBox.shrink(),
|
||||
)
|
||||
: Obx(
|
||||
() => BottomNavigationBar(
|
||||
currentIndex: _mainController.selectedIndex,
|
||||
onTap: (value) => setIndex(value),
|
||||
iconSize: 16,
|
||||
selectedFontSize: 12,
|
||||
unselectedFontSize: 12,
|
||||
type: BottomNavigationBarType.fixed,
|
||||
// selectedItemColor:
|
||||
// Theme.of(context).colorScheme.primary, // 选中项的颜色
|
||||
// unselectedItemColor:
|
||||
// Theme.of(context).colorScheme.onSurface,
|
||||
items: _mainController.navigationBars
|
||||
.map(
|
||||
(e) => BottomNavigationBarItem(
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
),
|
||||
activeIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
),
|
||||
label: e['label'],
|
||||
),
|
||||
() => _mainController.navigationBars.length > 1
|
||||
? BottomNavigationBar(
|
||||
currentIndex:
|
||||
_mainController.selectedIndex,
|
||||
onTap: setIndex,
|
||||
iconSize: 16,
|
||||
selectedFontSize: 12,
|
||||
unselectedFontSize: 12,
|
||||
type: BottomNavigationBarType.fixed,
|
||||
// selectedItemColor:
|
||||
// Theme.of(context).colorScheme.primary, // 选中项的颜色
|
||||
// unselectedItemColor:
|
||||
// Theme.of(context).colorScheme.onSurface,
|
||||
items: _mainController.navigationBars
|
||||
.map(
|
||||
(e) => BottomNavigationBarItem(
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
),
|
||||
activeIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
),
|
||||
label: e['label'],
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
: const SizedBox.shrink(),
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'package:PiliPalaX/http/loading_state.dart';
|
||||
import 'package:PiliPalaX/models/home/rcmd/result.dart';
|
||||
import 'package:PiliPalaX/pages/common/popup_controller.dart';
|
||||
import 'package:PiliPalaX/http/video.dart';
|
||||
import 'package:PiliPalaX/utils/storage.dart';
|
||||
|
||||
100
lib/pages/setting/navigation_bar_set.dart
Normal file
100
lib/pages/setting/navigation_bar_set.dart
Normal file
@@ -0,0 +1,100 @@
|
||||
import 'package:PiliPalaX/utils/storage.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
|
||||
import '../../../models/common/nav_bar_config.dart';
|
||||
|
||||
class NavigationBarSetPage extends StatefulWidget {
|
||||
const NavigationBarSetPage({super.key});
|
||||
|
||||
@override
|
||||
State<NavigationBarSetPage> createState() => _NavigationbarSetPageState();
|
||||
}
|
||||
|
||||
class _NavigationbarSetPageState extends State<NavigationBarSetPage> {
|
||||
late List defaultNavTabs;
|
||||
late List<int> navBarSort;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
defaultNavTabs = defaultNavigationBars;
|
||||
navBarSort =
|
||||
GStorage.setting.get(SettingBoxKey.navBarSort, defaultValue: [0, 1, 2]);
|
||||
// 对 tabData 进行排序
|
||||
defaultNavTabs.sort((a, b) {
|
||||
int indexA = navBarSort.indexOf(a['id']);
|
||||
int indexB = navBarSort.indexOf(b['id']);
|
||||
|
||||
// 如果类型在 sortOrder 中不存在,则放在末尾
|
||||
if (indexA == -1) indexA = navBarSort.length;
|
||||
if (indexB == -1) indexB = navBarSort.length;
|
||||
|
||||
return indexA.compareTo(indexB);
|
||||
});
|
||||
}
|
||||
|
||||
void saveEdit() {
|
||||
List<int> sortedTabbar = defaultNavTabs
|
||||
.where((i) => navBarSort.contains(i['id']))
|
||||
.map<int>((i) => i['id'])
|
||||
.toList();
|
||||
if (sortedTabbar.isEmpty) {
|
||||
sortedTabbar = [0, 1, 2];
|
||||
}
|
||||
GStorage.setting.put(SettingBoxKey.navBarSort, sortedTabbar);
|
||||
SmartDialog.showToast('保存成功,下次启动时生效');
|
||||
}
|
||||
|
||||
void onReorder(int oldIndex, int newIndex) {
|
||||
setState(() {
|
||||
if (newIndex > oldIndex) {
|
||||
newIndex -= 1;
|
||||
}
|
||||
final tabsItem = defaultNavTabs.removeAt(oldIndex);
|
||||
defaultNavTabs.insert(newIndex, tabsItem);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Navbar编辑'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: saveEdit,
|
||||
child: const Text('保存'),
|
||||
),
|
||||
const SizedBox(width: 12)
|
||||
],
|
||||
),
|
||||
body: ReorderableListView(
|
||||
onReorder: onReorder,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
footer: SizedBox(
|
||||
height: MediaQuery.of(context).padding.bottom + 30,
|
||||
),
|
||||
children: defaultNavTabs
|
||||
.map(
|
||||
(item) => CheckboxListTile(
|
||||
key: Key(item['label']),
|
||||
value: navBarSort.contains(item['id']),
|
||||
onChanged: (bool? newValue) {
|
||||
int tabTypeId = item['id'];
|
||||
if (!newValue!) {
|
||||
navBarSort.remove(tabTypeId);
|
||||
} else {
|
||||
navBarSort.add(tabTypeId);
|
||||
}
|
||||
setState(() {});
|
||||
},
|
||||
title: Text(item['label']),
|
||||
secondary: const Icon(Icons.drag_indicator_rounded),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -359,13 +359,19 @@ class _StyleSettingState extends State<StyleSetting> {
|
||||
subtitle: Text('删除或调换首页标签页', style: subTitleStyle),
|
||||
leading: const Icon(Icons.toc_outlined),
|
||||
),
|
||||
ListTile(
|
||||
dense: false,
|
||||
onTap: () => Get.toNamed('/navbarSetting'),
|
||||
title: Text('Navbar编辑', style: titleStyle),
|
||||
leading: const Icon(Icons.toc_outlined),
|
||||
),
|
||||
if (Platform.isAndroid)
|
||||
ListTile(
|
||||
dense: false,
|
||||
onTap: () => Get.toNamed('/displayModeSetting'),
|
||||
title: Text('屏幕帧率', style: titleStyle),
|
||||
leading: const Icon(Icons.autofps_select_outlined),
|
||||
)
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import 'package:PiliPalaX/pages/member/new/member_page.dart';
|
||||
import 'package:PiliPalaX/pages/member/new/widget/edit_profile_page.dart';
|
||||
import 'package:PiliPalaX/pages/setting/navigation_bar_set.dart';
|
||||
import 'package:PiliPalaX/pages/setting/sponsor_block_page.dart';
|
||||
import 'package:PiliPalaX/pages/video/detail/introduction/widgets/create_fav_page.dart';
|
||||
import 'package:PiliPalaX/pages/webview/webview_page.dart';
|
||||
@@ -186,6 +187,9 @@ class Routes {
|
||||
CustomGetPage(name: '/sponsorBlock', page: () => const SponsorBlockPage()),
|
||||
CustomGetPage(name: '/createFav', page: () => const CreateFavPage()),
|
||||
CustomGetPage(name: '/editProfile', page: () => const EditProfilePage()),
|
||||
// navigation bar
|
||||
CustomGetPage(
|
||||
name: '/navbarSetting', page: () => const NavigationBarSetPage()),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -304,7 +304,8 @@ class SettingBoxKey {
|
||||
tabbarSort = 'tabbarSort', // 首页tabbar
|
||||
dynamicBadgeMode = 'dynamicBadgeMode',
|
||||
hiddenSettingUnlocked = 'hiddenSettingUnlocked',
|
||||
enableGradientBg = 'enableGradientBg';
|
||||
enableGradientBg = 'enableGradientBg',
|
||||
navBarSort = 'navBarSort';
|
||||
}
|
||||
|
||||
class LocalCacheKey {
|
||||
|
||||
Reference in New Issue
Block a user