是否有一种方法可以在特定页面上禁用Android后退按钮?

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(),
      },
    );
  }
}

在第一页,我有一个按钮去第二页:

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

我的问题是,如果我按下android屏幕底部的后退箭头,我就会回到pageOne。我想这个按钮不显示在所有。 理想情况下,除非用户的手指持续按在屏幕上5秒钟,否则我不希望用户有任何办法离开屏幕。(我正在尝试为幼儿编写一个应用程序,希望只有父母能够导航出特定的屏幕)。


当前回答

如果你需要系统返回按钮点击和应用程序栏返回按钮点击有不同的行为:你可以在调用Navigator.of(context).pop()之前删除onWillPop回调:

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: onWillPop,
      child: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            onPressed: () {
              ModalRoute.of(context)?.removeScopedWillPopCallback(onWillPop);
              Navigator.of(context).pop();
            },
            icon: const Icon(Icons.arrow_back),
          ),
          title: Text(context.l10n.searchResults),
        ),
        body: MyBody(),
      ),
    );
  }

在这种情况下,当用户点击系统返回按钮时,onWillPop回调将决定是否弹出屏幕。但当用户点击应用栏后退按钮时,屏幕会立即弹出。

其他回答

尝试这将杀死你的应用程序状态

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      ////////////////
      onWillPop: () => showDialog<bool>(
        context: context,
        builder: (c) => AlertDialog(
          title: Text(
            'Warning',
            textAlign: TextAlign.center,
          ),
          content: Text('Are you sure to exit?'),
          actions: [
            TextButton(
              style: TextButton.styleFrom(
                primary: Colors.green,
              ),
              onPressed: () async {
                exit(0);// kill app
              },
              child: Text('Yes'),
            ),
            TextButton(
              style: TextButton.styleFrom(
                primary: Colors.red,
              ),
              onPressed: () => Navigator.pop(c, false),
              child: Text('No'),
            )
          ],
        ),
      ),
      /////////////////////
      child: Scaffold(),
    );
  }

虽然Remi的回答是正确的,但通常情况下,您不希望简单地阻止返回按钮,而是希望用户确认退出。

你可以用类似的方式从确认对话框中获取答案,因为onWillPop是一个未来。

@override
Widget build(BuildContext context) {
  return WillPopScope(
    child: Scaffold(...),
    onWillPop: () => showDialog<bool>(
      context: context,
      builder: (c) => AlertDialog(
        title: Text('Warning'),
        content: Text('Do you really want to exit'),
        actions: [
          FlatButton(
            child: Text('Yes'),
            onPressed: () => Navigator.pop(c, true),
          ),
          FlatButton(
            child: Text('No'),
            onPressed: () => Navigator.pop(c, false),
          ),
        ],
      ),
    ),
  );
}

WillPopScope是一个很好的解决方案,onWillPop: () async => false返回按钮被禁用,但图标仍然在那里。

要删除和禁用后退按钮,只需使用AppBar中的leading: Container()。

如果你需要系统返回按钮点击和应用程序栏返回按钮点击有不同的行为:你可以在调用Navigator.of(context).pop()之前删除onWillPop回调:

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: onWillPop,
      child: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            onPressed: () {
              ModalRoute.of(context)?.removeScopedWillPopCallback(onWillPop);
              Navigator.of(context).pop();
            },
            icon: const Icon(Icons.arrow_back),
          ),
          title: Text(context.l10n.searchResults),
        ),
        body: MyBody(),
      ),
    );
  }

在这种情况下,当用户点击系统返回按钮时,onWillPop回调将决定是否弹出屏幕。但当用户点击应用栏后退按钮时,屏幕会立即弹出。

使用生成的路由,您可以通过这样做返回到目标页面-

AppBar(
        leading: new IconButton(
        icon: new Icon(Icons.arrow_back),
         onPressed: () {
         Navigator.of(context) .pushNamedAndRemoveUntil(AppRouter.dashboard, (route) => false);
         }),
       )