Navigator.of(context).push()
IconButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (BuildContext context) {
return SettingsScreen();
}), // MaterialPageRoute
); // Navigator
}, // onPressed
icon: Icon(
Icons.settings,
color: RED_COLOR,
), // Icon
), // IconButton
Dart
복사
Navigator.of(context).pushNamed()
// main.dart
void main() {
runApp(
MaterialApp({
initialRoute: '/',
routes: {
'/': (context) => HomeScreen(),
'/one': (context) => RouteOneScreen(),
'/two': (context) => RouteTwoScreen(),
'/three': (context) => RouteThreeScreen(),
},
),
);
}
// pushNamed
// [home(), one(), two(), three()]
() {
Navigator.of(context).pushNamed('/one', argument: 123);
}
// pushReplacementNamed
// [home(), one(), two(), three()]
() {
Navigator.of(context).pushReplacementNamed('/one', arguement)
}
// pushNamedandRemoveUntil
// [home(), one(), two(), three()]
() {
Navigator.of(context).pushNamedandRemoveUntil(
'/three',
(route) => route.settings.name == '/',
);
}
// target screen
Widget build(BuildContext context) {
final arguments = ModalRoute.of(context)!.settings.arguments;
return ...,
Dart
복사
Unpoppable
return WillPopScope(
onWillPop: () async {
// true - popable
// false - unpopable
return canPop = Navigator.of(context).canPop();
},
child: MainLayout(),
)
Navigator.of(context).popUntil((route) => route.isFirst);
Dart
복사
needs async
Navigator.of(context).canPop(); return true or false
Navigator.of(context).maybePop(); pop if routing stack is not empty
