Skip to content

Instantly share code, notes, and snippets.

@akshdeep-singh
Last active September 20, 2022 18:01
Show Gist options
  • Save akshdeep-singh/0f25d4876fe7e2a1138301ca42c0e6e5 to your computer and use it in GitHub Desktop.
Save akshdeep-singh/0f25d4876fe7e2a1138301ca42c0e6e5 to your computer and use it in GitHub Desktop.
Flutter sample to use multiple navigators with `bottomNavigationBar`
class MyComponentHomePage extends StatefulWidget {
const MyComponentHomePage();
@override
State<MyComponentHomePage> createState() => _StateHomePage();
}
class _StateHomePage extends State<MyComponentHomePage> {
final _keys = [
GlobalKey<NavigatorState>(),
null,
GlobalKey<NavigatorState>(),
];
var _index = 1;
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
if (_index != 1) {
return !await _keys[_index]!.currentState!.maybePop();
}
return true;
},
child: Scaffold(
body: child: IndexedStack(
index: _index,
children: [
/// Simply use Navigator.push() inside MyComponentPlaylist()
Navigator(
key: _keys[0],
onGenerateRoute: (settings) => MaterialPageRoute(
builder: (context) => const MyComponentPlaylist(),
),
),
MyComponentPlayer(),
Navigator(
key: _keys[2],
onGenerateRoute: (settings) => MaterialPageRoute(
builder: (context) => const MyComponentLibrary(),
),
),
],
),
bottomNavigationBar: NavigationBar(
selectedIndex: _index,
onDestinationSelected: (value) => setState(() {
_index = value;
}),
destinations: const [
NavigationDestination(
icon: Icon(Icons.queue_music),
label: 'Playlist',
),
NavigationDestination(
icon: Icon(Icons.music_note_outlined),
selectedIcon: Icon(Icons.music_note),
label: 'Player',
),
NavigationDestination(
icon: Icon(Icons.library_music_outlined),
selectedIcon: Icon(Icons.library_music),
label: 'Library',
),
],
),
);
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment