如何停用或覆盖Android的“返回"按钮,在Flutter中? [英] How to deactivate or override the Android "BACK" button, in Flutter?

查看:454
本文介绍了如何停用或覆盖Android的“返回"按钮,在Flutter中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在特定页面上,是否可以停用Android后退按钮?

Is there a way to deactivate the Android back button when on a specific page?

class WakeUpApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "Time To Wake Up ?",
      home: new WakeUpHome(),
      routes: <String, WidgetBuilder>{
        '/pageOne': (BuildContext context) => new pageOne(),
        '/pageTwo': (BuildContext context) => new pageTwo(),
      },
    );
  }
}

在pageOne上,我有一个转到第二页的按钮:

On pageOne I have a button to go to pageTwo:

new FloatingActionButton(
  onPressed: () {    
    Navigator.of(context).pushNamed('/pageTwo');
  },
)

我的问题是,如果我按下android屏幕底部的后退"箭头,则会回到pageOne.我希望此按钮完全不显示. 理想情况下,除非用户例如在屏幕上按住手指5秒钟,否则我将无法离开该屏幕. (我正在尝试为幼儿编写一个应用程序,希望只有父母能够导航到特定屏幕之外).

My problem is that if I press the Back arrow at the bottom of the android screen, I go back to pageOne. I would like this button to not show up at all. Ideally, I would like to have no possible way out of this screen unless the user for example keeps his finger pressed on the screen for 5 seconds. (I am trying to write an App for toddlers, and would like only the parents to be able to navigate out of the particular screen).

推荐答案

答案是WillPopScope.这将防止系统弹出页面.您仍然可以使用Navigator.of(context).pop()

The answer is WillPopScope. It will prevent the page from being popped by the system. You'll still be able to use Navigator.of(context).pop()

@override
Widget build(BuildContext context) {
  return new WillPopScope(
    onWillPop: () async => false,
    child: new Scaffold(
      appBar: new AppBar(
        title: new Text("data"),
        leading: new IconButton(
          icon: new Icon(Icons.ac_unit),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
    ),
  );
}

这篇关于如何停用或覆盖Android的“返回"按钮,在Flutter中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆