skip to Main Content

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

3

Answers


  1. bool? callBack= await _coolAlert(context);
    Cuz dialog returns future, you have to wait for it

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
  3. simply add await

    enter image description here

    import 'package:cool_alert/cool_alert.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(
        const MaterialApp(
          home: MyApp(),
        ),
      );
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => const NewPage(),
                  ),
                );
              },
              child: const Text('new page'),
            ),
          ),
        );
      }
    }
    
    class NewPage extends StatelessWidget {
      const NewPage({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () async {
                await CoolAlert.show(
                  context: context,
                  type: CoolAlertType.success,
                  text: "Your transaction was successful!",
                  onConfirmBtnTap: () {
                    debugPrint('success');
                  },
                );
                Navigator.of(context).pop();
              },
              child: const Text('alert and pop'),
            ),
          ),
        );
      }
    }
    
    

    in your code

    2.

    onPressed: () async {
        ...
        await_coolAlert(context);
        Navigator.pop(context, true);
    },
    
      Future<void> _coolAlert(BuildContext context) {
        return CoolAlert.show(
          context: context,
          type: CoolAlertType.success,
          text: "Your transaction was successful!",
          onConfirmBtnTap: () {
            debugPrint('success');
          },
        );
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search