Created
February 2, 2024 14:35
-
-
Save chooyan-eng/85619327a8107908f1df95d7b25a8ce7 to your computer and use it in GitHub Desktop.
Sample main.dart for navigating with GlobalObjectKey
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(const MainApp()); | |
} | |
class MainApp extends StatelessWidget { | |
const MainApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: SizedBox( | |
width: 300, | |
height: 300, | |
child: Navigator( | |
key: const GlobalObjectKey<NavigatorState>('navigator'), | |
onGenerateRoute: (settings) => MaterialPageRoute( | |
builder: (_) => const FirstPage(), | |
), | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
class FirstPage extends StatelessWidget { | |
const FirstPage({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return InkWell( | |
onTap: () { | |
const key = GlobalObjectKey<NavigatorState>('navigator'); | |
key.currentState?.push( | |
MaterialPageRoute(builder: (_) => const NextPage()), | |
); | |
}, | |
child: const ColoredBox( | |
color: Colors.blue, | |
child: Center(child: Text('First')), | |
), | |
); | |
} | |
} | |
class NextPage extends StatelessWidget { | |
const NextPage({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return InkWell( | |
onTap: () { | |
const key = GlobalObjectKey<NavigatorState>('navigator'); | |
key.currentState?.pop(); | |
}, | |
child: const ColoredBox( | |
color: Colors.green, | |
child: Center(child: Text('Next')), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment