chore: fix route change error

This commit is contained in:
Kingkor Roy Tirtho 2023-09-26 22:57:12 +06:00
parent d3b7a435f9
commit 460f3471b5

View File

@ -19,10 +19,10 @@ import 'package:spotube/provider/download_manager_provider.dart';
import 'package:spotube/utils/persisted_state_notifier.dart'; import 'package:spotube/utils/persisted_state_notifier.dart';
const rootPaths = { const rootPaths = {
0: "/", "/": 0,
1: "/search", "/search": 1,
2: "/library", "/library": 2,
3: "/lyrics", "/lyrics": 3,
}; };
class RootApp extends HookConsumerWidget { class RootApp extends HookConsumerWidget {
@ -34,7 +34,6 @@ class RootApp extends HookConsumerWidget {
@override @override
Widget build(BuildContext context, ref) { Widget build(BuildContext context, ref) {
final index = useState(0);
final isMounted = useIsMounted(); final isMounted = useIsMounted();
final showingDialogCompleter = useRef(Completer()..complete()); final showingDialogCompleter = useRef(Completer()..complete());
final downloader = ref.watch(downloadManagerProvider); final downloader = ref.watch(downloadManagerProvider);
@ -42,20 +41,6 @@ class RootApp extends HookConsumerWidget {
final theme = Theme.of(context); final theme = Theme.of(context);
final location = GoRouterState.of(context).matchedLocation; final location = GoRouterState.of(context).matchedLocation;
useEffect(() {
final newIndex = rootPaths.entries.firstWhereOrNull((e) {
if (e.value == "/" || location == "/") {
return location == e.value;
}
return location.startsWith(e.value);
})?.key;
if (newIndex != null) {
index.value = newIndex;
}
return null;
}, [location]);
useEffect(() { useEffect(() {
WidgetsBinding.instance.addPostFrameCallback((_) async { WidgetsBinding.instance.addPostFrameCallback((_) async {
final sharedPreferences = await SharedPreferences.getInstance(); final sharedPreferences = await SharedPreferences.getInstance();
@ -163,13 +148,21 @@ class RootApp extends HookConsumerWidget {
return null; return null;
}, [backgroundColor]); }, [backgroundColor]);
void onSelectIndexChanged(int d) {
final invertedRouteMap =
rootPaths.map((key, value) => MapEntry(value, key));
if (context.mounted) {
WidgetsBinding.instance.addPostFrameCallback((_) {
GoRouter.of(context).go(invertedRouteMap[d]!);
});
}
}
return Scaffold( return Scaffold(
body: Sidebar( body: Sidebar(
selectedIndex: index.value, selectedIndex: rootPaths[location] ?? 0,
onSelectedIndexChanged: (i) { onSelectedIndexChanged: onSelectIndexChanged,
index.value = i;
GoRouter.of(context).go(rootPaths[index.value]!);
},
child: child, child: child,
), ),
extendBody: true, extendBody: true,
@ -178,11 +171,8 @@ class RootApp extends HookConsumerWidget {
children: [ children: [
BottomPlayer(), BottomPlayer(),
SpotubeNavigationBar( SpotubeNavigationBar(
selectedIndex: index.value, selectedIndex: rootPaths[location] ?? 0,
onSelectedIndexChanged: (selectedIndex) { onSelectedIndexChanged: onSelectIndexChanged,
index.value = selectedIndex;
GoRouter.of(context).go(rootPaths[selectedIndex]!);
},
), ),
], ],
), ),