skip to Main Content

This is a widget I am returning on build:

getBackgroundWidget() {
    return WillPopScope(
        onWillPop: _onBackPressed,
        child: Scaffold(
            key: scaffoldKey,
            backgroundColor: Colors.white,
            drawer: AppNavigationDrawer(onSelectMenuItem),
            appBar: AppBar(
              elevation: 0.5,
              shadowColor: Colors.grey,
              backgroundColor: Colors.white,
              titleSpacing: 0,
              leading: IconButton(
                onPressed: () {
                  scaffoldKey.currentState!.openDrawer();
                },
                icon: Image.asset('assets/img/landing/title_icon.png',
                    height: 30, width: 30),
              ),
              title: Text(
                AppUtils.HOME_BOTTOM_LABEL_LIST[homeCubit!.currentIndex]
                    .toUpperCase(),
                style: TextStyle(fontFamily: 'NotoSansBold', fontSize: 20),
              ),);
my on back pressed code is as follows:


  Future<bool> _onBackPressed() {
    return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          backgroundColor: Colors.white,
          surfaceTintColor: Colors.transparent,
          title: Text(
            'Switch ION',
            style: TextStyle(fontSize: 23),
          ),
          content: Text('Do you want to exit the app'),
          actions: <Widget>[
            TextButton(
              child: Text(
                'No',
                style: TextStyle(color: AppTheme.appColors),
              ),
              onPressed: () {
                Navigator.of(context).pop(false);
              },
            ),
            TextButton(
              child: Text(
                'Yes',
                style: TextStyle(color: AppTheme.appColors),
              ),
              onPressed: () {
               // Navigator.of(context).pop(true);
                SystemNavigator.pop();
              },
            )
          ],
        );
      },
    ).then((value) => value ?? false);
  }

2

Answers


  1. onWillPop works according to the bool value it receives. I think your show dialog is not returning bool value. So you might create one bool variable in _onBackPressed. For example,

    Future<bool> _onBackPressed()async{
    bool shouldPop = false
    await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          backgroundColor: Colors.white,
          surfaceTintColor: Colors.transparent,
          title: Text(
            'Switch ION',
            style: TextStyle(fontSize: 23),
          ),
          content: Text('Do you want to exit the app'),
          actions: <Widget>[
            TextButton(
              child: Text(
                'No',
                style: TextStyle(color: AppTheme.appColors),
              ),
              onPressed: () {
    Navigator.pop(context)
    shouldPop = false;
              },
            ),
            TextButton(
              child: Text(
                'Yes',
                style: TextStyle(color: AppTheme.appColors),
              ),
              onPressed: () {
    Navigator.pop(context);
               shouldPop = true;
              },
            )
          ],
        );
    return shouldPop;
    }
    

    So, now it should work.

    Login or Signup to reply.
  2. When you use the then word, the future function passes its result as an argument, but it is not visible outside of it. You should use async and await, that waits for the response and enables you to get its value.

    With that change, your back function should be like this:

      Future<bool> _onBackPressed() async {
        final value = await showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              backgroundColor: Colors.white,
              surfaceTintColor: Colors.transparent,
              title: const Text(
                'Switch ION',
                style: TextStyle(fontSize: 23),
              ),
              content: const Text('Do you want to exit the app'),
              actions: <Widget>[
                TextButton(
                  child: Text(
                    'No',
                    style: TextStyle(color: AppTheme.appColors),
                  ),
                  onPressed: () {
                    Navigator.of(context).pop(false);
                  },
                ),
                TextButton(
                  child: Text(
                    'Yes',
                    style: TextStyle(color: AppTheme.appColors),
                  ),
                  onPressed: () {
                    // Navigator.of(context).pop(true);
                    SystemNavigator.pop();
                  },
                )
              ],
            );
          },
        );
        return value ?? false;
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search