我正在开发一个Flutter Android应用程序,它有三个屏幕:页面1、页面2和页面3。当我从页面2进入页面3时,如果我点击手机的返回按钮,它应该返回到页面2。但是它却重定向到了页面1。我从在Flutter中捕获Android返回按钮事件中获取引用后尝试了一下。
我尝试了
如何解决这个问题?我的代码如下: 页面1
我尝试了
WillPopScope
,但它没有进入onWillPop
。如何解决这个问题?我的代码如下: 页面1
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: new AppBar(),
body: MyAppPage()
),
);
}
}
class MyAppPage extends StatefulWidget{
MyAppPage({Key key,}):super(key:key);
@override
_MyAppPageState createState()=> new _MyAppPageState();
}
class _MyAppPageState extends State<MyAppPage>{
@override
Widget build(BuildContext context){
return new Scaffold(
body:RaisedButton(onPressed:(){ Navigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen()));},
child: new Text("got to page 1"),)
);
}
}
第二页
class SecondScreen extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: new AppBar(),
body: SecondPage()
),
);
}
}
class SecondPage extends StatefulWidget{
SecondPage({Key key,}):super(key:key);
@override
SecondPageState createState()=> new SecondPageState();
}
class SecondPageState extends State<SecondPage>{
@override
Widget build(BuildContext context){
return new Scaffold(
body:Column(
children: <Widget>[
new Center(
child: new Text("Page 2"),
),
RaisedButton(onPressed:(){ Navigator.push(context, MaterialPageRoute(builder: (context) => ThirdScreen()));},
child: new Text("go to third Page 3"),)
],
)
);
}
}
第三页
class ThirdScreen extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: new AppBar(),
body: ThirdPage()
),
);
}
}
class ThirdPage extends StatefulWidget{
ThirdPage({Key key,}):super(key:key);
@override
ThirdPageState createState()=> new ThirdPageState();
}
class ThirdPageState extends State<ThirdPage>{
@override
Widget build(BuildContext context){
return new WillPopScope(
child: new Scaffold(
body: new Center(
child: new Text('PAGE 3'),
),
),
onWillPop: (){
debugPrint("onWillPop");
return new Future(() => false);
},
);
}
}
WillPopScope
并在其中放置onWillPop: () async => Navigator.of(context).pop()
也不起作用吗? - CularBytes