marcos930807 / awesomeDialogs

A new Flutter package project for simple a awesome dialogs
Other
339 stars 110 forks source link

Use AwesomeDialog with WillPopScope to confirm app exit #62

Closed giaur500 closed 2 years ago

giaur500 commented 3 years ago

This is how I did it with standard AlertDialog:


return WillPopScope(
      onWillPop: _promptExit,
      child: Container() /*Remaining window layout*/

Future<bool> _promptExit {

return showDialog(
      context: context,
      builder: (context) => new AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20.0))),
        title: new Text(Strings.prompt_exit_title),
        content: new Text(Strings.prompt_exit_content),
        actions: <Widget>[
          FlatButton(
            child: new Text(Strings.no),
            onPressed: () => Navigator.of(context).pop(false),
          ),
          SizedBox(height: 16),
          FlatButton(
            child: new Text(Strings.yes),
            onPressed: () => Navigator.of(context).pop(true),
          ),
        ],
      ),
    ) ??
        false;

}

How to do that with AwesomeDialog? I can;t see any way to put it into showDialog. Any advice?

actumn commented 3 years ago

I know it's been a long time, but below may help you.

class MyWidget extends StatelessWidget {

  Future<bool> _onWillPop() async {
    return AwesomeDialog(context: context).show() as bool;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        backgroundColor: Colors.black,
        body: Column(
          children: [
          ],
        ),
      ),
    );
  }
}
ducquynhnguyeninfo commented 2 years ago

I know it's been a long time, but below may help you.

class MyWidget extends StatelessWidget {

  Future<bool> _onWillPop() async {
    return AwesomeDialog(context: context).show() as bool;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        backgroundColor: Colors.black,
        body: Column(
          children: [
          ],
        ),
      ),
    );
  }
}

Hi, I can not use this as a return from the AwesomeDialog

var awesomeDialog = AwesomeDialog(
              context: context,
              headerAnimationLoop: false,
              dialogType: DialogType.NO_HEADER,
              title: 'Dialog Title',
              desc: 'Dialog description here.............',
              btnCancelOnPress: () {},
              btnOkOnPress: () {},
            );
var value = await awesomeDialog.show() as bool;
return value;

as it yields [VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'bool' in type cast

OutdatedGuy commented 2 years ago

Yo @giaur500 and @ducquynhnguyeninfo, from the version ^2.1.3 you can now pass data from the dialog using custom Navigator.pop in the onDissmissCallback function.

Set autoDismiss to false and provide onDissmissCallback to however you want it to work

Try using this code below:

onWillPop: () async {
        return await AwesomeDialog(
          context: context,
          dialogType: DialogType.QUESTION,
          desc: 'Are you sure you want to exit the app?',
          btnOkText: 'Yes',
          btnOkOnPress: () {},
          btnCancelText: 'No',
          btnCancelOnPress: () {},
          autoDismiss: false,
          onDissmissCallback: (dismissType) {
            Navigator.of(context).pop(dismissType == DismissType.BTN_OK);
          },
        ).show() as bool;
      }

For full working example, try this code below

Code ```dart import 'package:flutter/material.dart'; import 'package:awesome_dialog/awesome_dialog.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return const MaterialApp( home: FirstPage(), ); } } class FirstPage extends StatefulWidget { const FirstPage({Key? key}) : super(key: key); @override State createState() => _FirstPageState(); } class _FirstPageState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('FirstPage'), ), body: Center( child: ElevatedButton( onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => const SecondPage()), ); }, child: const Text('Go to SecondPage'), ), ), ); } } class SecondPage extends StatefulWidget { const SecondPage({Key? key}) : super(key: key); @override State createState() => _SecondPageState(); } class _SecondPageState extends State { @override Widget build(BuildContext context) { return WillPopScope( onWillPop: () async { return await AwesomeDialog( context: context, dialogType: DialogType.QUESTION, desc: 'Go back to First Page?', btnOkText: 'Yes', btnOkOnPress: () {}, btnCancelText: 'No', btnCancelOnPress: () {}, autoDismiss: false, onDissmissCallback: (dismissType) { Navigator.of(context).pop(dismissType == DismissType.BTN_OK); }, ).show() as bool; }, child: Scaffold( appBar: AppBar( title: const Text('SecondPage'), ), ), ); } } ```
OutdatedGuy commented 2 years ago

@marcos930807 Please close this issue if resolved