Flutter应用程序后退按钮事件未重定向到后页

时间:2018-12-16 08:26:33

标签: flutter

我正在开发一款流畅的android应用。它具有三个屏幕。页面1,页面2,页面3。当我从页面2输入页面3时。如果单击电话后退按钮,它想转到页面2。 但是它正在重定向到页面1。我尝试从获得引用后 catch Android back button event on Flutter

我尝试了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);
        },
        );
    }
    }

3 个答案:

答案 0 :(得分:3)

您对创建的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(),
          ),
        ],
      ),
    );
  }
}

答案 1 :(得分:1)

在您的第三页上,尝试使用

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

答案 2 :(得分:0)

对于我的情况,我应该将Flutter PopupMenuItem( value: MenuItems.switchTheme, child: Text('Auto-switch color scheme', style: Theme.of(context).textTheme.button), enabled: prefs.getBool(describeEnum(Prefs.manuallySetDark)) != null, ), 分支升级到最新代码。

master