How to make an AlertDialog in Flutter?

前端 未结 9 2674
小蘑菇
小蘑菇 2020-11-29 00:56

I am learning to build apps in Flutter. Now I have come to alert dialogs. I have done them before in Android and iOS, but how do I make an alert in Flutter?

Here are

相关标签:
9条回答
  • 2020-11-29 01:21

    If you want beautiful and responsive alert dialog then you can use flutter packages like

    rflutter alert ,fancy dialog,rich alert,sweet alert dialogs,easy dialog & easy alert

    These alerts are good looking and responsive. Among them rflutter alert is the best. currently I am using rflutter alert for my apps.

    0 讨论(0)
  • 2020-11-29 01:24
    showAlertDialog(BuildContext context, String message, String heading,
          String buttonAcceptTitle, String buttonCancelTitle) {
        // set up the buttons
        Widget cancelButton = FlatButton(
          child: Text(buttonCancelTitle),
          onPressed: () {},
        );
        Widget continueButton = FlatButton(
          child: Text(buttonAcceptTitle),
          onPressed: () {
    
          },
        );
    
        // set up the AlertDialog
        AlertDialog alert = AlertDialog(
          title: Text(heading),
          content: Text(message),
          actions: [
            cancelButton,
            continueButton,
          ],
        );
    
        // show the dialog
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return alert;
          },
        );
      }
    

    called like:

    showAlertDialog(context, 'Are you sure you want to delete?', "AppName" , "Ok", "Cancel");
    
    0 讨论(0)
  • 2020-11-29 01:27

    I used similar approach, but I wanted to

    1. Keep the Dialog code as a widget in a separated file so I can reuse it.
    2. Blurr the background when the dialog is shown.

    Code: 1. alertDialog_widget.dart

    import 'dart:ui';
    import 'package:flutter/material.dart';
    
    
    class BlurryDialog extends StatelessWidget {
    
      String title;
      String content;
      VoidCallback continueCallBack;
    
      BlurryDialog(this.title, this.content, this.continueCallBack);
      TextStyle textStyle = TextStyle (color: Colors.black);
    
      @override
      Widget build(BuildContext context) {
        return BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 6, sigmaY: 6),
          child:  AlertDialog(
          title: new Text(title,style: textStyle,),
          content: new Text(content, style: textStyle,),
          actions: <Widget>[
            new FlatButton(
              child: new Text("Continue"),
               onPressed: () {
                continueCallBack();
              },
            ),
            new FlatButton(
              child: Text("Cancel"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
          ));
      }
    }
    

    You can call this in main (or wherever you want) by creating a new method like:

     _showDialog(BuildContext context)
    {
    
      VoidCallback continueCallBack = () => {
     Navigator.of(context).pop(),
        // code on continue comes here
    
      };
      BlurryDialog  alert = BlurryDialog("Abort","Are you sure you want to abort this operation?",continueCallBack);
    
    
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return alert;
        },
      );
    }
    
    0 讨论(0)
提交回复
热议问题