diff --git a/lib/components/root/sidebar.dart b/lib/components/root/sidebar.dart index 0dc8b5b41..7fb1b95f4 100644 --- a/lib/components/root/sidebar.dart +++ b/lib/components/root/sidebar.dart @@ -22,7 +22,7 @@ import 'package:spotube/utils/platform.dart'; import 'package:spotube/utils/type_conversion_utils.dart'; class Sidebar extends HookConsumerWidget { - final int selectedIndex; + final int? selectedIndex; final void Function(int) onSelectedIndexChanged; final Widget child; @@ -57,7 +57,7 @@ class Sidebar extends HookConsumerWidget { ref.watch(userPreferencesProvider.select((s) => s.layoutMode)); final controller = useSidebarXController( - selectedIndex: selectedIndex, + selectedIndex: selectedIndex ?? 0, extended: mediaQuery.lgAndUp, ); @@ -75,17 +75,21 @@ class Sidebar extends HookConsumerWidget { ); useEffect(() { - if (controller.selectedIndex != selectedIndex) { - controller.selectIndex(selectedIndex); + if (controller.selectedIndex != selectedIndex && selectedIndex != null) { + controller.selectIndex(selectedIndex!); } return null; }, [selectedIndex]); useEffect(() { - controller.addListener(() { + void listener() { onSelectedIndexChanged(controller.selectedIndex); - }); - return null; + } + + controller.addListener(listener); + return () { + controller.removeListener(listener); + }; }, [controller]); useEffect(() { diff --git a/lib/components/root/spotube_navigation_bar.dart b/lib/components/root/spotube_navigation_bar.dart index 9cea56038..b62d19d1f 100644 --- a/lib/components/root/spotube_navigation_bar.dart +++ b/lib/components/root/spotube_navigation_bar.dart @@ -16,7 +16,7 @@ import 'package:spotube/provider/user_preferences_provider.dart'; final navigationPanelHeight = StateProvider((ref) => 50); class SpotubeNavigationBar extends HookConsumerWidget { - final int selectedIndex; + final int? selectedIndex; final void Function(int) onSelectedIndexChanged; const SpotubeNavigationBar({ @@ -33,7 +33,7 @@ class SpotubeNavigationBar extends HookConsumerWidget { final layoutMode = ref.watch(userPreferencesProvider.select((s) => s.layoutMode)); - final insideSelectedIndex = useState(selectedIndex); + final insideSelectedIndex = useState(selectedIndex ?? 0); final buttonColor = useBrightnessValue( theme.colorScheme.inversePrimary, @@ -46,7 +46,9 @@ class SpotubeNavigationBar extends HookConsumerWidget { final panelHeight = ref.watch(navigationPanelHeight); useEffect(() { - insideSelectedIndex.value = selectedIndex; + if (selectedIndex != null) { + insideSelectedIndex.value = selectedIndex!; + } return null; }, [selectedIndex]); diff --git a/lib/pages/root/root_app.dart b/lib/pages/root/root_app.dart index b2bd4620b..bdbc1c75b 100644 --- a/lib/pages/root/root_app.dart +++ b/lib/pages/root/root_app.dart @@ -159,7 +159,7 @@ class RootApp extends HookConsumerWidget { return Scaffold( body: Sidebar( - selectedIndex: rootPaths[location] ?? 0, + selectedIndex: rootPaths[location], onSelectedIndexChanged: onSelectIndexChanged, child: child, ), @@ -169,7 +169,7 @@ class RootApp extends HookConsumerWidget { children: [ BottomPlayer(), SpotubeNavigationBar( - selectedIndex: rootPaths[location] ?? 0, + selectedIndex: rootPaths[location], onSelectedIndexChanged: onSelectIndexChanged, ), ],