Olance
Olance

Reputation: 107

Flutter Web: How to disable backward button of browser in flutter web application

After successfully login the user redirected to the Home page but when the user clicks on the browser back button it easily redirected to the login screen. What should I do to disable backward redirection?

Upvotes: 5

Views: 8522

Answers (2)

Shubhanshu Kashiva
Shubhanshu Kashiva

Reputation: 192

Updating @FederickJonathan answer for 2023, now you have to return a bool value for onWillPop argument like this:

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

And you can also wrap it on MaterialApp class in main.dart to work for whole application at once like this:

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

Upvotes: 1

Federick Jonathan
Federick Jonathan

Reputation: 2864

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

Upvotes: 4

Related Questions