【问题标题】:back page with flutter android back button带有颤振的android后退按钮的后退页
【发布时间】:2022-01-03 13:22:02
【问题描述】:

我应该使用什么代码返回到带有颤振 android 后退按钮的页面?我查看了 youtube,尤其是 Stackoverflow,但没有得到任何结果。

【问题讨论】:

  • 这能回答你的问题吗? catch Android back button event on Flutter
  • 您是说以编程方式返回吗?使用 Navigator.of(context).pop() 转到上一条路线。使用 Navigator.of(context).maybePop() 来密切模拟按下后退按钮。这更好,因为它确保您可以在返回之前返回并使用父级的任何 WillPopScopes(如果指定)。
  • 如果你想在 Flutter 中通过 android 的后退按钮关闭整个应用程序,那么你可以使用 exit(0)

标签: flutter webview flutter-web


【解决方案1】:

您可以使用WillPopScope

这里是示例代码:

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        // This function will handle back button
        // When true, it's can back to previous page
        // when false, back button will do nothing
        return true;
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter WillPopScope demo'),
        ),
        body: Center(
          child: Text('Hello world')
        ),
      ),
    );
  }
}

【讨论】:

    【解决方案2】:
    class Page2 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new WillPopScope(
          child: new Scaffold(
            appBar: new AppBar(
              title: new Text('Page 2'),
            ),
            body: new Center(
              child: new Text('PAGE 2'),
            ),
          ),
          onWillPop: () async {
            return false;
          },
        );
      }
    }
    
    Future<T> pushPage<T>(BuildContext context, Widget page) {
      return Navigator.of(context)
          .push<T>(MaterialPageRoute(builder: (context) => page));
    }
    Can call the page like:
    
    pushPage(context, Page2());
    

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 2021-03-22
      • 1970-01-01
      • 2022-09-26
      • 2021-02-13
      • 2015-08-05
      • 1970-01-01
      • 2019-10-29
      • 1970-01-01
      相关资源
      最近更新 更多