Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-05-30 14:40:13 +08:00
parent 5f2e863cc2
commit 3031d5e3b0
2 changed files with 105 additions and 155 deletions

View File

@@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:math' show max;
import 'package:PiliPlus/grpc/dyn.dart'; import 'package:PiliPlus/grpc/dyn.dart';
import 'package:PiliPlus/grpc/im.dart'; import 'package:PiliPlus/grpc/im.dart';
@@ -11,8 +12,8 @@ import 'package:PiliPlus/utils/utils.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
class MainController extends GetxController { class MainController extends GetxController {
RxList<NavigationBarType> navigationBars = <NavigationBarType>[].obs; List<NavigationBarType> navigationBars = <NavigationBarType>[];
int dynCount = 0; RxInt dynCount = 0.obs;
StreamController<bool>? bottomBarStream; StreamController<bool>? bottomBarStream;
late bool hideTabBar; late bool hideTabBar;
@@ -137,9 +138,8 @@ class MainController extends GetxController {
} }
Future<void> setCount([int count = 0]) async { Future<void> setCount([int count = 0]) async {
if (dynIndex == -1 || dynCount == count) return; if (dynIndex == -1 || dynCount.value == count) return;
dynCount = count; dynCount.value = count;
navigationBars.refresh();
} }
void checkUnreadDynamic() { void checkUnreadDynamic() {
@@ -166,10 +166,10 @@ class MainController extends GetxController {
} else { } else {
navigationBars = navigationBars =
navBarSort.map((i) => NavigationBarType.values[i]).toList(); navBarSort.map((i) => NavigationBarType.values[i]).toList();
if (!navBarSort.contains(defaultHomePage)) defaultHomePage = 0;
} }
this.navigationBars.value = navigationBars; this.navigationBars = navigationBars;
selectedIndex.value = defaultHomePage; selectedIndex.value =
max(0, navigationBars.indexWhere((e) => e.index == defaultHomePage));
} }
@override @override

View File

@@ -207,8 +207,7 @@ class _MainAppState extends State<MainApp>
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
if (useSideBar || !isPortrait) ...[ if (useSideBar || !isPortrait) ...[
Obx( _mainController.navigationBars.length > 1
() => _mainController.navigationBars.length > 1
? context.isTablet && GStorage.optTabletNav ? context.isTablet && GStorage.optTabletNav
? Column( ? Column(
children: [ children: [
@@ -224,7 +223,8 @@ class _MainAppState extends State<MainApp>
child: MediaQuery.removePadding( child: MediaQuery.removePadding(
context: context, context: context,
removeRight: true, removeRight: true,
child: NavigationDrawer( child: Obx(
() => NavigationDrawer(
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
tilePadding: const EdgeInsets.symmetric( tilePadding: const EdgeInsets.symmetric(
vertical: 5, horizontal: 12), vertical: 5, horizontal: 12),
@@ -237,38 +237,27 @@ class _MainAppState extends State<MainApp>
onDestinationSelected: setIndex, onDestinationSelected: setIndex,
selectedIndex: selectedIndex:
_mainController.selectedIndex.value, _mainController.selectedIndex.value,
children: [ children: _mainController.navigationBars
..._mainController.navigationBars .map(
.map((e) { (e) =>
return NavigationDrawerDestination( NavigationDrawerDestination(
label: Text(e.label), label: Text(e.label),
icon: _buildIcon( icon: _buildIcon(type: e),
type: e,
count: e ==
NavigationBarType
.dynamics
? _mainController.dynCount
: 0,
),
selectedIcon: _buildIcon( selectedIcon: _buildIcon(
type: e, type: e,
count: e ==
NavigationBarType
.dynamics
? _mainController.dynCount
: 0,
selected: true, selected: true,
)),
)
.toList(),
), ),
);
}),
],
), ),
), ),
), ),
), ),
], ],
) )
: NavigationRail( : Obx(
() => NavigationRail(
groupAlignment: 0.5, groupAlignment: 0.5,
selectedIndex: selectedIndex:
_mainController.selectedIndex.value, _mainController.selectedIndex.value,
@@ -276,37 +265,25 @@ class _MainAppState extends State<MainApp>
labelType: NavigationRailLabelType.selected, labelType: NavigationRailLabelType.selected,
leading: userAndSearchVertical(theme), leading: userAndSearchVertical(theme),
destinations: _mainController.navigationBars destinations: _mainController.navigationBars
.map( .map((e) => NavigationRailDestination(
(e) => NavigationRailDestination(
label: Text(e.label), label: Text(e.label),
icon: _buildIcon( icon: _buildIcon(type: e),
type: e,
count: e == NavigationBarType.dynamics
? _mainController.dynCount
: 0,
),
selectedIcon: _buildIcon( selectedIcon: _buildIcon(
type: e, type: e,
count: e == NavigationBarType.dynamics
? _mainController.dynCount
: 0,
selected: true, selected: true,
), ),
), ))
)
.toList(), .toList(),
),
) )
: SafeArea( : SafeArea(
right: false, right: false,
child: Container( child: Container(
padding: const EdgeInsets.only( padding: const EdgeInsets.only(top: 10),
top: 10,
),
width: 80, width: 80,
child: userAndSearchVertical(theme), child: userAndSearchVertical(theme),
), ),
), ),
),
VerticalDivider( VerticalDivider(
width: 1, width: 1,
indent: MediaQuery.of(context).padding.top, indent: MediaQuery.of(context).padding.top,
@@ -357,41 +334,27 @@ class _MainAppState extends State<MainApp>
duration: const Duration(milliseconds: 500), duration: const Duration(milliseconds: 500),
offset: Offset(0, snapshot.data ? 0 : 1), offset: Offset(0, snapshot.data ? 0 : 1),
child: enableMYBar child: enableMYBar
? _mainController.navigationBars.length > 1
? Obx( ? Obx(
() => _mainController.navigationBars.length > 1 () => NavigationBar(
? NavigationBar(
onDestinationSelected: setIndex, onDestinationSelected: setIndex,
selectedIndex: selectedIndex:
_mainController.selectedIndex.value, _mainController.selectedIndex.value,
destinations: destinations: _mainController.navigationBars
_mainController.navigationBars.map( .map((e) => NavigationDestination(
(e) {
return NavigationDestination(
label: e.label, label: e.label,
icon: _buildIcon( icon: _buildIcon(type: e),
type: e,
count: e ==
NavigationBarType.dynamics
? _mainController.dynCount
: 0,
),
selectedIcon: _buildIcon( selectedIcon: _buildIcon(
type: e, type: e,
count: e ==
NavigationBarType.dynamics
? _mainController.dynCount
: 0,
selected: true, selected: true,
)))
.toList(),
), ),
);
},
).toList(),
) )
: const SizedBox.shrink(), : const SizedBox.shrink()
) : _mainController.navigationBars.length > 1
: Obx( ? Obx(
() => _mainController.navigationBars.length > 1 () => BottomNavigationBar(
? BottomNavigationBar(
currentIndex: currentIndex:
_mainController.selectedIndex.value, _mainController.selectedIndex.value,
onTap: setIndex, onTap: setIndex,
@@ -400,32 +363,17 @@ class _MainAppState extends State<MainApp>
unselectedFontSize: 12, unselectedFontSize: 12,
type: BottomNavigationBarType.fixed, type: BottomNavigationBarType.fixed,
items: _mainController.navigationBars items: _mainController.navigationBars
.map( .map((e) => BottomNavigationBarItem(
(e) => BottomNavigationBarItem(
label: e.label, label: e.label,
icon: _buildIcon( icon: _buildIcon(type: e),
type: e,
count: e ==
NavigationBarType
.dynamics
? _mainController.dynCount
: 0,
),
activeIcon: _buildIcon( activeIcon: _buildIcon(
type: e, type: e,
count: e ==
NavigationBarType
.dynamics
? _mainController.dynCount
: 0,
selected: true, selected: true,
), )))
),
)
.toList(), .toList(),
),
) )
: const SizedBox.shrink(), : const SizedBox.shrink(),
),
); );
}, },
), ),
@@ -436,17 +384,19 @@ class _MainAppState extends State<MainApp>
Widget _buildIcon({ Widget _buildIcon({
required NavigationBarType type, required NavigationBarType type,
required int count,
bool selected = false, bool selected = false,
}) { }) {
final icon = selected ? type.selectIcon : type.icon; final icon = selected ? type.selectIcon : type.icon;
return count > 0 return type == NavigationBarType.dynamics
? Badge( ? Obx(
() => Badge(
isLabelVisible: _mainController.dynCount.value > 0,
label: _mainController.dynamicBadgeMode == DynamicBadgeMode.number label: _mainController.dynamicBadgeMode == DynamicBadgeMode.number
? Text(count.toString()) ? Text(_mainController.dynCount.value.toString())
: null, : null,
padding: const EdgeInsets.fromLTRB(6, 0, 6, 0), padding: const EdgeInsets.symmetric(horizontal: 6),
child: icon, child: icon,
),
) )
: icon; : icon;
} }