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

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

问题描述

有没有办法在特定页面上停用 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 上,我有一个按钮可以转到 pageTwo:

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 “BACK"按钮,在颤振中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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