Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振应用程序返回按钮事件未重定向到返回页面_Flutter - Fatal编程技术网

Flutter 颤振应用程序返回按钮事件未重定向到返回页面

Flutter 颤振应用程序返回按钮事件未重定向到返回页面,flutter,Flutter,我正在开发一款颤振android应用程序。它有三个屏幕。第1页,第2页,第3页。当我从第2页进入第3页时。如果我点击“电话后退”按钮,它将进入第2页。 但它正在重定向到第1页。我从他那里得到推荐信后就试过了 我尝试了WillPopScope。它没有进入onWillPop 如何解决这个问题。我的代码如下所示 第1页 void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // Thi

我正在开发一款颤振android应用程序。它有三个屏幕。第1页,第2页,第3页。当我从第2页进入第3页时。如果我点击“电话后退”按钮,它将进入第2页。 但它正在重定向到第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"),)
        );
    }
    }
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:新的appBar(),
正文:MyAppPage()
),
);
}
}
类MyAppPage扩展了StatefulWidget{
MyAppPage({Key,}):超级(Key:Key);
@凌驾
_MyAppPageState createState()=>new_MyAppPageState();
}
类_MyAppPageState扩展状态{
@凌驾
小部件构建(构建上下文){
归还新脚手架(
body:RaisedButton(on按下:(){Navigator.push(上下文,MaterialPageRoute(builder:(上下文)=>SecondScreen());},
子项:新文本(“转到第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"),)
            ],
            )
        );
    }
    }
class SecondScreen扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:新的appBar(),
正文:第二页()
),
);
}
}
类SecondPage扩展StatefulWidget{
第二页({Key,}):超级(Key:Key);
@凌驾
SecondPageState createState()=>新建SecondPageState();
}
类SecondPageState扩展了状态{
@凌驾
小部件构建(构建上下文){
归还新脚手架(
正文:专栏(
儿童:[
新中心(
子项:新文本(“第2页”),
),
RaisedButton(按下时:(){Navigator.push(上下文,MaterialPage路线(生成器:(上下文)=>ThirdScreen());},
子项:新文本(“转到第三页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);
        },
        );
    }
    }
class ThirdScreen扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:新的appBar(),
正文:第三页()
),
);
}
}
类ThirdPage扩展了StatefulWidget{
第三页({Key,}):超级(Key:Key);
@凌驾
ThirdPageState createState()=>新的ThirdPageState();
}
类ThirdPageState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回新的Willposcope(
儿童:新脚手架(
正文:新中心(
子项:新文本(“第3页”),
),
),
onWillPop:(){
调试打印(“onWillPop”);
返回新的未来(()=>false);
},
);
}
}

在第三页上,尝试使用

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

你有点被你创建的
屏幕和
页面弄糊涂了。实际上,您拥有的小部件比您需要的多

这就是你可能想要做的

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(),
          ),
        ],
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回物料app(主页:MyAppPage());
}
}
类MyAppPage扩展了StatefulWidget{
@凌驾
_MyAppPageState createState()=>\u MyAppPageState();
}
类_MyAppPageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“第1页”),
正文:专栏(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
中间(子项:文本(“转到第1页”),
升起的按钮(
子项:文本(“转到第2页”),
已按下:(){
push(context,MaterialPageRoute(builder:(context)=>SecondPage());
},
),
],
),
);
}
}
类SecondPage扩展StatefulWidget{
@凌驾
_SecondPageState createState()=>\u SecondPageState();
}
类_SecondPageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“第2页”),
正文:专栏(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
居中(
孩子:文本(“我在第2页”),
),
升起的按钮(
已按下:(){
Navigator.push(上下文,
MaterialPageRoute(生成器:(上下文)=>ThirdPage());
},
子项:文本(“转到第三页3”),
)
],
)
);
}
}
类ThirdPage扩展了StatefulWidget{
@凌驾
_ThirdPageState createState();
}
类ThirdPageState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“第3页”),
正文:专栏(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
居中(子项:文本(“第3页”),
升起的按钮(
子项:文本(“传统后退按钮”),
onPressed:()=>Navigator.of(context.pop(),
),
],
),
);
}
}

对于我的情况,我应该将颤振
主分支升级为最新代码

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

我试过这个,但没用。这不是输入onWillPop方法。是的,我得到了答案。我避免使用无状态,使用有状态。感谢不要避免无状态,这很有用>:)但是,如果是一个带有支架的页面,你需要这种状态-并且保持一个页面x一个支架的比率,你在同一个页面中嵌套了很多。但是我