Flutter Web:如何在Flutter Web应用程序中禁用浏览器的后退按钮

pcww981p  于 2023-06-24  发布在  Flutter
关注(0)|答案(2)|浏览(181)

成功登录后,用户重定向到主页,但当用户单击浏览器上一步按钮时,它很容易重定向到登录屏幕。要禁用后向重定向,我应该怎么做?

ftf50wuq

ftf50wuq1#

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    /// Hope WillPopScope will do the job for you.
    return WillPopScope(
      onWillPop: () async => null,
      child: Scaffold(
        body: Center(
          child: Text('asd'),
        ),
      ),
    );
  }
}
o7jaxewo

o7jaxewo2#

更新2023年的@FederickJonathan答案,现在你必须为onWillPop参数返回一个bool值:

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    /// Hope WillPopScope will do the job for you.
    return WillPopScope(
      onWillPop: () async => false, // update here
      child: Scaffold(
        body: Center(
          child: Text('asd'),
        ),
      ),
    );
  }
}

您也可以将其 Package 在main.dart中的MaterialApp类中,以便一次为整个应用程序工作,如下所示:

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => false,
      child: MaterialApp(
       ...
      ),
    );
  }
}

相关问题