Farhana Naaz Ansari
Farhana Naaz Ansari

Reputation: 7948

Flutter: close app on back press is not working

In the app, on starting splash screen appears after that I reach to the login screen where there is no app bar or back button but I'm able to back from device back button. I don't want to back on the splash screen when the back is pressed from login screen. I tried many solutions but they are not working.

class Login extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'xyz',
      home: LoginPage(),
      debugShowCheckedModeBanner: false,

    );
  }
}


class LoginPage extends StatefulWidget {

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () {
          if (Navigator.canPop(context)) {
            Navigator.pop(context);  // i tried both the way
            Navigator.of(context).pop();
          } else {
            SystemNavigator.pop();
          }
        }, 
        child:Scaffold(
      body: Container(.........................//here is my desiging stuff

I also tried return Future.value(false); with true and false value –

Splash screen code

 @override
  void initState (){
    super.initState();
    // TODO initial state stuff
    new Future.delayed(
        const Duration(seconds: 2),
            () => Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => Login()),
        ));
  }

Upvotes: 3

Views: 10743

Answers (3)

Fartab
Fartab

Reputation: 5513

I had the same problem, and it was because wrongly surrounding MaterialApp with WillPopScope (instead of surrounding Scaffold).

Appreciating @anmol-majhail, here is the correct solution in my case:

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fetch Data Example',
      home: WillPopScope(
          child: Scaffold(...),
          onWillPop: () async {
            return false;
          }),
    );
  }

Upvotes: 0

anmol.majhail
anmol.majhail

Reputation: 51316

Simply pass empty function to - WillPopScope widget.

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () {},   // Empty Function.
        child: Scaffold(
          body: Container(), //here is my desiging stuff
        ));
  }
}

OR

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return false;
      },
      child: Scaffold(
        body: Container(),
        appBar: AppBar(),
      ),
    );
  }

If you never want to goto Splash Screen.

It's Better to use:

Navigator.of(context)
                    .pushReplacement(MaterialPageRoute(builder: (context) => Login()));

Upvotes: 4

Mo Meshkani
Mo Meshkani

Reputation: 1694

Just change your code to:

class _LoginPageState extends State<LoginPage> {

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: ()async => false,
        child:Scaffold(
      body: Container(.........................//here is my desiging stuff

Upvotes: 1

Related Questions