Created
November 28, 2022 18:59
-
-
Save jonahwilliams/a12a29c5ca5e44a2716fcabdb0cc4cc3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: MainPage(), | |
); | |
} | |
} | |
class MainPage extends StatelessWidget { | |
final navigatorKey = GlobalKey<NavigatorState>(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Column( | |
children: <Widget>[ | |
Expanded( | |
child: Navigator( | |
key: navigatorKey, | |
onGenerateRoute: (route) => MaterialPageRoute( | |
settings: route, | |
builder: (context) => PageOne(), | |
), | |
), | |
), | |
BottomNavigationBar(navigatorKey) | |
], | |
), | |
); | |
} | |
} | |
class BottomNavigationBar extends StatelessWidget { | |
final GlobalKey<NavigatorState> navigatorKey; | |
BottomNavigationBar(this.navigatorKey) : assert(navigatorKey != null); | |
Future<void> push(Route route) { | |
return navigatorKey.currentState!.push(route); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.blue, | |
child: ButtonBar( | |
alignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
TextButton( | |
child: Text("PageOne", style: TextStyle(color: Colors.red)), | |
onPressed: () { | |
push(MaterialPageRoute(builder: (context) => PageOne())); | |
}, | |
), | |
TextButton( | |
child: Text("PageTwo", style: TextStyle(color: Colors.red)), | |
onPressed: () { | |
push(MaterialPageRoute(builder: (context) => PageTwo())); | |
}, | |
) | |
], | |
), | |
); | |
} | |
} | |
class PageOne extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text("Page One"), | |
TextButton( | |
onPressed: (){ | |
Navigator.of(context).pop(); | |
}, | |
child: Text("Pop"), | |
), | |
], | |
), | |
); | |
} | |
} | |
class PageTwo extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text("Page Two"), | |
TextButton( | |
onPressed: (){ | |
Navigator.of(context).pop(); | |
}, | |
child: Text("Pop"), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment