小编典典

如何在Flutter中停用或覆盖Android“后退”按钮?

flutter

在特定页面上,有没有办法停用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(),
      },
    );
  }
}

在pageOne上,有一个按钮可以转到pageTwo:

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

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


阅读 345

收藏
2020-08-13

共1个答案

小编典典

答案是WillPopScope。这将防止系统弹出页面。您仍然可以使用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(),
        ),
      ),
    ),
  );
}
2020-08-13