faj*_*nul 7 android flutter flutter-navigation
我有带有嵌套导航器的 Flutter 应用程序,我想WillPopScope在我的嵌套屏幕中使用覆盖“onBackPressed” 。
假设我有MainScreen, PrimaryScreen, 和SecondaryScreen。
PrimaryScreen嵌套导航仪,它有几个屏幕一样PrimaryOneScreen,PrimaryTwoScreen和PrimaryThreeScreen。
SecondaryScreen也是一个嵌套的 Navigator,就像PrimaryScreen,它有 3 个屏幕。
这是我想要实现的目标的说明
MainScreen-> PrimaryScreen( PrimaryOneScreen-> PrimaryTwoScreen-> PrimaryThreeScreen)
当我在 上的位置时PrimaryTwoScreen,我想回到PrimaryOneScreen使用WillPopScopeWidget覆盖“onBackPressed”的状态。但是onWillPop当我按下后退按钮时从未调用过,我的屏幕直接返回到MainScreen.
这是我的代码
主屏幕.dart
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MainApp(),
onGenerateRoute: (settings){
WidgetBuilder builder;
switch(settings.name){
case 'primary' :
builder = (BuildContext _) => PrimaryScreen();
break;
case 'secondary' :
builder = (BuildContext _) => SecondaryScreen();
break;
case 'main' :
builder = (BuildContext _) => MainApp();
break;
default :
throw Exception('Invalid route: ${settings.name}');
}
return MaterialPageRoute(
builder: builder,
settings: settings
);
},
);
}
}
class MainApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
FlatButton(
onPressed: (){
Navigator.pushNamed(context, 'primary');
},
child: Text('To Page Primary'),
),
FlatButton(
onPressed: (){
Navigator.pushNamed(context, 'secondary');
},
child: Text('To Page Secondary'),
)
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
主屏幕.dart
class PrimaryScreen extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Navigator(
initialRoute: 'primary/pageone',
onGenerateRoute: (RouteSettings settings){
WidgetBuilder builder;
switch(settings.name){
case 'primary/pageone' :
builder = (BuildContext _) => PrimaryOneScreen();
break;
case 'primary/pagetwo' :
builder = (BuildContext _) => PrimaryTwoScreen();
break;
case 'primary/pagethree' :
builder = (BuildContext _) => PrimaryThreeScreen();
break;
default :
throw Exception('Invalid route: ${settings.name}');
}
return MaterialPageRoute(
builder: builder,
settings: settings
);
},
);
}
}
Run Code Online (Sandbox Code Playgroud)
PrimaryOneScreen.dart
class PrimaryOneScreen extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Primary Page'),
),
body: Center(
child: Column(
children: <Widget>[
FlatButton(
onPressed: (){
Navigator.pushNamed(context, 'primary/pagetwo');
},
child: Text('To Page Two'),
),
FlatButton(
onPressed: (){
Navigator.pushNamed(context, 'primary/pagethree');
},
child: Text('To Page Three'),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
PrimaryTwoScreen.dart
class PrimaryTwoScreen extends StatelessWidget{
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: ()async{
print('willPopScope');
Navigator.of(context, rootNavigator: false).pop();
return true;
},
child: Scaffold(
appBar: AppBar(
title: Text('Secondary Page'),
),
body: Center(
child: Column(
children: <Widget>[
FlatButton(
onPressed: (){
Navigator.pushNamed(context, 'primary/pageone');
},
child: Text('To Page One'),
),
FlatButton(
onPressed: (){
Navigator.pushNamed(context, 'primary/pagethree');
},
child: Text('To Page Three'),
),
],
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
PrimaryThreeScreen.dart
class PrimaryThreeScreen extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Primary Page'),
),
body: Center(
child: Column(
children: <Widget>[
FlatButton(
onPressed: (){
Navigator.pushNamed(context, 'primary/pageone');
},
child: Text('To Page One'),
),
FlatButton(
onPressed: (){
Navigator.pushNamed(context, 'primary/pagetwo');
},
child: Text('To Page Two'),
)
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
如何仅在嵌套导航器上弹出?
谢谢!
小智 0
尝试使用此链接https://medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31
检查用例:pushReplacementNamed标签描述
| 归档时间: |
|
| 查看次数: |
2058 次 |
| 最近记录: |