Home > Enterprise >  how to exit the app if the device back button is pressed flutter
how to exit the app if the device back button is pressed flutter

Time:05-19

I want to display a dialog when the device back button is pressed.... if user press no then nothing will happens...... but if the user press yes then the application shall close..... I have tried willpopscope but it is not working......

Can anyone help me with this???

this is my main.dart

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:users_app/splashScreen/splash_screen.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp();

  runApp(MyApp(
      child: MaterialApp(
    title: 'Drivers App',
    home: const MySplashScreen(),
    debugShowCheckedModeBanner: false,
  )));
}

class MyApp extends StatefulWidget {
  final Widget? child;

  MyApp({this.child});

  static void restartApp(BuildContext context) {
    context.findAncestorStateOfType<_MyAppState>()!.restartApp();
  }

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Key key = UniqueKey();

  void restartApp() {
    setState(() {
      key = UniqueKey();
    });
  }

  Future<bool?> showWarning(BuildContext context) async => showDialog<bool>(
      context: context,
      builder: (context) => AlertDialog(
            title: const Text('DO you want to exit app?'),
            actions: [
              ElevatedButton(
                  onPressed: () => Navigator.of(context).pop(false),
                  child: Text('No')),
              ElevatedButton(
                  onPressed: () => Navigator.of(context).pop(true),
                  child: Text('Yes')),
            ],
          ));

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        print('Back Button Pressed');

        final shouldPop = await showWarning(context);

        return shouldPop ?? false;
      },
      child: KeyedSubtree(
        key: key,
        child: widget.child!,
      ),
    );
  }
}

CodePudding user response:

you can simply use

SystemNavigator.pop();

and it'll close the app

CodePudding user response:

Widget build(BuildContext context) {
  return WillPopScope(
    child: /*Your scaffold widget*/
    onWillPop: () {
      return showDialog(
        context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text("Confirm Exit"),
        content: Text("Are you sure you want to exit app?"),
        actions: <Widget>[
          FlatButton(
            child: Text("Exit"),
            onPressed: () {
              SystemNavigator.pop();
            },
          ),
          FlatButton(
            child: Text("NO"),
                onPressed: () {
                  Navigator.of(context).pop();
                },
            )
          ],
        );
      }
    );
    return Future.value(true);
  },
  • Related