Created
December 1, 2023 02:56
-
-
Save leighajarett/8412fd22046c7358abd4be3d6523f41a to your computer and use it in GitHub Desktop.
NavBar with navigators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Root(), | |
); | |
} | |
} | |
class Root extends StatefulWidget { | |
@override | |
State<Root> createState() => _RootState(); | |
} | |
class _RootState extends State<Root> { | |
int currentPageIndex = 0; | |
final destinations = const [ | |
NavigationDestination( | |
icon: Icon(Icons.home), | |
selectedIcon: Icon(Icons.home, color: Colors.blue), | |
label: 'Home', | |
tooltip: '', | |
), | |
NavigationDestination( | |
icon: Icon(Icons.business), | |
selectedIcon: Icon(Icons.business, color: Colors.blue), | |
label: 'Home', | |
tooltip: '', | |
)]; | |
final pages = const [Home(destinationIndex: 0), Center(child: Text('Business Page'))]; | |
// Generate a GlobalKey<NavigatorState> for each page | |
late final List<GlobalKey<NavigatorState>> navigatorKeys = List.generate(pages.length, (int index) => GlobalKey()); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
bottomNavigationBar: NavigationBar( | |
onDestinationSelected: (int index) { | |
setState(() { | |
currentPageIndex = index; | |
}); | |
}, | |
backgroundColor: Colors.transparent, | |
indicatorColor: Colors.transparent, | |
overlayColor: MaterialStateProperty.all(Colors.transparent), | |
surfaceTintColor: Colors.transparent, | |
selectedIndex: currentPageIndex, | |
labelBehavior: NavigationDestinationLabelBehavior.alwaysHide, | |
destinations: destinations | |
), | |
body: IndexedStack( | |
index: currentPageIndex, | |
children: pages.asMap().entries.map((entry) { | |
int idx = entry.key; | |
Widget page = entry.value; | |
return Navigator( | |
key: navigatorKeys[idx], | |
onGenerateRoute: (settings) { | |
return MaterialPageRoute(builder: (context) => page); | |
}, | |
); | |
}).toList(), | |
), | |
); | |
} | |
} | |
class Home extends StatelessWidget { | |
final int destinationIndex; | |
const Home({Key? key, required this.destinationIndex}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Home Page')), | |
body: Center( | |
child: ElevatedButton( | |
child: const Text('Go to List Page'), | |
onPressed: () { | |
Navigator.of(context).push(MaterialPageRoute( | |
builder: (context) => ListPage(), | |
)); | |
}, | |
), | |
), | |
); | |
} | |
} | |
class ListPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('List Page')), | |
body: const Center( | |
child: Text('Welcome to List Page'), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment