Search code examples
flutterdartflutter-navigation

How to pop back on previous screen from alert and send there a value?


The questions I'm going to ask are really connected. The idea is pop back on previous screen from alert and send there a value, but as it turned out there are some difficulties with it. I use cool_alert package in these pieces.

  1. I can show alert, but how after it pop back on previous screen and send a value?
onPressed: () {
    ...
    _coolAlert(context);
},

if I use the following then why the alert even doesn't show?

onPressed: () {
    ...
    _coolAlert(context);
    Navigator.pop(context, true);
},

where _coolAlert defined as

_coolAlert(Build Context context) {
    CoolAlert.show(
       context: context,
       type: CoolAlertType.success,
       text: "Your transaction was successful!",
       onConfirmBtnTap: () {
           debugPrint('success');
       },
    );
}

the return type of show is Future<dynamic>

  1. and finally if change it like this, then why the receiving value on previous screen is null?
_coolAlert(Build Context context) {
  CoolAlert.show(
     context: context,
     type: CoolAlertType.success,
     text: "Your transaction was successful!",
     onConfirmBtnTap: () {
         debugPrint('success');
     },
     Navigator.pop(context, true);
  );
}

Solution

  • There is one more param which is need for this to work.

    closeOnConfirmBtnTap: false,

    This will allow you to manually call pop method with a value. Here is a sample code

    import 'package:cool_alert/cool_alert.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(const CoolAlertExample());
    }
    
    class CoolAlertExample extends StatelessWidget {
      const CoolAlertExample({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData.light(
            useMaterial3: true,
          ),
          home: Scaffold(
            appBar: AppBar(title: const Text('Cool Alert Example')),
            body: _HomePage(),
          ),
        );
      }
    }
    
    class _HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: ElevatedButton(
            onPressed: () async {
              final result = await Navigator.of(context).push<bool?>(
                    MaterialPageRoute(
                      builder: (_) => _MyPage(),
                    ),
                  ) ??
                  false;
    
              print('HomePage: result = $result');
            },
            child: const Text('Next page'),
          ),
        );
      }
    }
    
    class _MyPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('My Page')),
          body: Center(
            child: ElevatedButton(
              onPressed: () async {
                final navigator = Navigator.of(context);
                final result = await CoolAlert.show(
                  context: context,
                  type: CoolAlertType.success,
                  text: "Your transaction was successful!",
                  closeOnConfirmBtnTap: false,
                  onConfirmBtnTap: () {
                    // return from dialog with success
                    debugPrint('success');
                    navigator.pop(true);
                  },
                  onCancelBtnTap: () {
                    // return from dialog with cancel
                    debugPrint('cancel');
                    navigator.pop(false);
                  },
                );
    
                print('returned from cool dialog = $result');
    
                //return to home page with value retuned from cool dialog.
                navigator.pop<bool>(result);
              },
              child: const Text('Show Alert'),
            ),
          ),
        );
      }
    }
    
    

    Output

    flutter: success
    flutter: returned from cool dialog = true
    flutter: HomePage: result = true