Flutter应用返回按钮事件未重定向到主页



我正在开发一个flutter安卓应用程序。它有三个屏幕。第1页,第2页,第3页。当我从第2页进入第3页时。如果我点击电话返回按钮,它想进入第2页。但它正在重定向到第1页。我从那里得到推荐信后试过了捕捉Flutter 上的Android后退按钮事件

我试过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"),)
);
}
}

第2页

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"),)
],
)
);
}
}

第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);
},
);
}
}

您有点混淆了创建的ScreenPages。实际上,你拥有的小工具比你需要的要多。

这可能是你想做的。

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(home: MyAppPage());
}
}
class MyAppPage extends StatefulWidget {
@override
_MyAppPageState createState() => _MyAppPageState();
}
class _MyAppPageState extends State<MyAppPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Page 1")),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Center(child: Text("got to page 1")),
RaisedButton(
child: Text("Go to Page 2"),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => SecondPage()));
},
),
],
),
);
}
}
class SecondPage extends StatefulWidget {
@override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Page 2")),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Center(
child: Text("I'm in Page 2"),
),
RaisedButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => ThirdPage()));
},
child: Text("go to third Page 3"),
)
],
)
);
}
}
class ThirdPage extends StatefulWidget {
@override
_ThirdPageState createState() => _ThirdPageState();
}
class _ThirdPageState extends State<ThirdPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Page 3")),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Center(child: Text('PAGE 3')),
RaisedButton(
child: Text("aditional back button"),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
}
}

在第3页上,尝试使用

onWillPop: () {
Navigator.of(context).pop();
},

对于我的情况,我应该将fluttermaster分支升级到最新的代码。

flutter channel master
flutter upgrade --force
flutter doctor -v

最新更新