Rutvik Gumasana
Rutvik Gumasana

Reputation: 1630

How to create custom reusable alert dialog in flutter

i've question about custom alert dialog, I've created one custom reusable alert dialog but when I am using it. it will not work. I mean on the pressed event is not working. when I click on it nothing happens so please can anyone check my code and give me exact solution. your help will be appreciated :)

Here is some code I've tried :)

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:pin_code_fields/pin_code_fields.dart';
import 'package:tudo/src/styles/colors.dart';

class TudoDialogWidget extends StatefulWidget {
  const TudoDialogWidget({
    this.onPressed,
    this.title,
    this.height,
    this.width,
    this.alignment,
    this.padding,
    this.subText,
    this.hasTimerStopped,
    this.secondsremaining,
    this.timerfontsize,
    this.timercolor,
    this.timerfontWeight,
    this.passcode,
    this.ondone,
    this.onErrorcheck,
    this.hasError,
    this.buttontext,
    this.color,
    this.fontweight,
    this.fontsize,
  });
  final GestureTapCallback onPressed;
  final Widget title;
  final double height;
  final double width;
  final AlignmentGeometry alignment;
  final EdgeInsetsGeometry padding;
  final String subText;
  final bool hasTimerStopped;
  final int secondsremaining;
  final double timerfontsize;
  final Color timercolor;
  final FontWeight timerfontWeight;
  final String passcode;
  final ValueChanged<String> ondone;
  final ValueChanged<bool> onErrorcheck;
  final bool hasError;
  final String buttontext;
  final Color color;
  final FontWeight fontweight;
  final double fontsize;

  @override
  _TudoDialogWidgetState createState() => _TudoDialogWidgetState();
}

class _TudoDialogWidgetState extends State<TudoDialogWidget> {

  bool hasTimerStopped = false;
  String passcode;
  final changeNotifier = StreamController<Functions>.broadcast();
  bool hasError = false;
  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: widget.title ?? Text("Dialog Title"),
      content: SingleChildScrollView(
        child: ListBody(
          children: <Widget>[
            Container(
              height: widget.height ?? MediaQuery.of(context).size.height / 2.4,
              width: widget.width ?? MediaQuery.of(context).size.height,
              alignment: widget.alignment,
              child: ListView(
                children: <Widget>[
                  Padding(
                    padding: widget.padding ??
                        const EdgeInsets.symmetric(vertical: 8.0),
                    child: Text(
                      widget.subText ?? 'Enter Your Subtext Here!!',
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 15,
                          color: Colors.black),
                      textAlign: TextAlign.center,
                    ),
                  ),
                  Center(
                    child: CountDownTimer(
                      secondsRemaining: widget.secondsremaining ?? 300,
                      whenTimeExpires: () {
                        setState(() {
                          hasTimerStopped = widget.hasTimerStopped;
                        });
                      },
                      countDownTimerStyle: TextStyle(
                        fontSize: widget.timerfontsize ?? 25,
                        color: widget.timercolor ?? Colors.amber,
                        fontWeight: widget.timerfontWeight ?? FontWeight.bold,
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(
                        vertical: 8.0, horizontal: 30),
                    child: PinCodeTextField(
                      length: 6, // must be greater than 0
                      obsecureText: false,
                      shape: PinCodeFieldShape.underline,
                      onDone: widget.ondone,
                      // onDone: (String value) {
                      //   setState(() {
                      //     passcode = value;
                      //     print(value);
                      //   });
                      // },

                      textStyle: TextStyle(
                          fontWeight: FontWeight
                              .bold), //optinal, default is TextStyle(fontSize: 18, color: Colors.black, fontWeight: FontWeight.bold)
                      onErrorCheck: widget.onErrorcheck,
                      shouldTriggerFucntions:
                          changeNotifier.stream.asBroadcastStream(),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 30.0),
                    child: Text(
                      hasError
                          ? "*Please fill up all the cells and press VERIFY again"
                          : "",
                      style:
                          TextStyle(color: Colors.red.shade300, fontSize: 12),
                    ),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  RichText(
                    textAlign: TextAlign.center,
                    text: TextSpan(
                        text: "Didn't receive the code? ",
                        style: TextStyle(color: Colors.black54, fontSize: 15),
                        children: [
                          TextSpan(
                              text: " RESEND",
                              // recognizer: onTapRecognizer,
                              style: TextStyle(
                                  color: widget.color,
                                  fontWeight:
                                      widget.fontweight ?? FontWeight.bold,
                                  fontSize: widget.fontsize ?? 16))
                        ]),
                  ),
                  SizedBox(
                    height: 7,
                  ),
                  Container(
                    margin: const EdgeInsets.symmetric(
                        vertical: 16.0, horizontal: 30),
                    child: ButtonTheme(
                      height: 50,
                      child: FlatButton(
                        onPressed: widget.onPressed,
                        child: Center(
                            child: Text(
                          widget.buttontext ?? "VERIFY".toUpperCase(),
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 18,
                              fontWeight: FontWeight.bold),
                        )),
                      ),
                    ),
                    decoration: BoxDecoration(
                      color: widget.color ?? colorStyles["primary"],
                      borderRadius: BorderRadius.circular(5),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      actions: <Widget>[
        FlatButton(
          child: Text('Close'),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );
  }
}

class CountDownTimer extends StatefulWidget {
  const CountDownTimer({
    Key key,
    int secondsRemaining,
    this.countDownTimerStyle,
    this.whenTimeExpires,
    this.countDownFormatter,
  })  : secondsRemaining = secondsRemaining,
        super(key: key);

  final int secondsRemaining;
  final Function whenTimeExpires;
  final Function countDownFormatter;
  final TextStyle countDownTimerStyle;

  State createState() => new _CountDownTimerState();
}

class _CountDownTimerState extends State<CountDownTimer>
    with TickerProviderStateMixin {
  AnimationController _controller;
  Duration duration;
  String formatHHMMSS(int seconds) {
    int hours = (seconds / 3600).truncate();
    seconds = (seconds % 3600).truncate();
    int minutes = (seconds / 60).truncate();

    String hoursStr = (hours).toString().padLeft(2, '0');
    String minutesStr = (minutes).toString().padLeft(2, '0');
    String secondsStr = (seconds % 60).toString().padLeft(2, '0');

    if (hours == 0) {
      return "$minutesStr:$secondsStr";
    }

    return "$hoursStr:$minutesStr:$secondsStr";
  }

  String get timerDisplayString {
    Duration duration = _controller.duration * _controller.value;
    return widget.countDownFormatter != null
        ? widget.countDownFormatter(duration.inSeconds)
        : formatHHMMSS(duration.inSeconds);
    // In case user doesn't provide formatter use the default one
    // for that create a method which will be called formatHHMMSS or whatever you like
  }

  @override
  void initState() {
    super.initState();
    duration = new Duration(seconds: widget.secondsRemaining);
    _controller = new AnimationController(
      vsync: this,
      duration: duration,
    );
    _controller.reverse(from: widget.secondsRemaining.toDouble());
    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed ||
          status == AnimationStatus.dismissed) {
        widget.whenTimeExpires();
      }
    });
  }

  @override
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
        child: AnimatedBuilder(
            animation: _controller,
            builder: (_, Widget child) {
              return Text(
                timerDisplayString,
                style: widget.countDownTimerStyle,
              );
            }));
  }
}

Upvotes: 2

Views: 3197

Answers (1)

chunhunghan
chunhunghan

Reputation: 54417

Your code works fine. you can see picture, display text works.
you need to display your Tudo dialog like below code snippet
For onPress event you pass would need further clarify

 _showCustomDialog(BuildContext context) {
  showDialog(
      context: context,
      builder: (context) {
        return TudoDialogWidget(
            title: Text('Tudo dialog'),
            onPressed: () { print("click on press");    },
        );
      });
}


RaisedButton(
              onPressed: () {
                _showCustomDialog(context);
              },
              child: const Text("Custom Dialog"),
            ),

full code include other example code

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'dart:async';
import 'package:pin_code_fields/pin_code_fields.dart';

class TudoDialogWidget extends StatefulWidget {
  const TudoDialogWidget({
    this.onPressed,
    this.title,
    this.height,
    this.width,
    this.alignment,
    this.padding,
    this.subText,
    this.hasTimerStopped,
    this.secondsremaining,
    this.timerfontsize,
    this.timercolor,
    this.timerfontWeight,
    this.passcode,
    this.ondone,
    this.onErrorcheck,
    this.hasError,
    this.buttontext,
    this.color,
    this.fontweight,
    this.fontsize,
  });
  final GestureTapCallback onPressed;
  final Widget title;
  final double height;
  final double width;
  final AlignmentGeometry alignment;
  final EdgeInsetsGeometry padding;
  final String subText;
  final bool hasTimerStopped;
  final int secondsremaining;
  final double timerfontsize;
  final Color timercolor;
  final FontWeight timerfontWeight;
  final String passcode;
  final ValueChanged<String> ondone;
  final ValueChanged<bool> onErrorcheck;
  final bool hasError;
  final String buttontext;
  final Color color;
  final FontWeight fontweight;
  final double fontsize;

  @override
  _TudoDialogWidgetState createState() => _TudoDialogWidgetState();
}

class _TudoDialogWidgetState extends State<TudoDialogWidget> {

  bool hasTimerStopped = false;
  String passcode;
  final changeNotifier = StreamController<Functions>.broadcast();
  bool hasError = false;
  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: widget.title ?? Text("Dialog Title"),
      content: SingleChildScrollView(
        child: ListBody(
          children: <Widget>[
            Container(
              height: widget.height ?? MediaQuery.of(context).size.height / 2.4,
              width: widget.width ?? MediaQuery.of(context).size.height,
              alignment: widget.alignment,
              child: ListView(
                children: <Widget>[
                  Padding(
                    padding: widget.padding ??
                        const EdgeInsets.symmetric(vertical: 8.0),
                    child: Text(
                      widget.subText ?? 'Enter Your Subtext Here!!',
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 15,
                          color: Colors.black),
                      textAlign: TextAlign.center,
                    ),
                  ),
                  Center(
                    child: CountDownTimer(
                      secondsRemaining: widget.secondsremaining ?? 300,
                      whenTimeExpires: () {
                        setState(() {
                          hasTimerStopped = widget.hasTimerStopped;
                        });
                      },
                      countDownTimerStyle: TextStyle(
                        fontSize: widget.timerfontsize ?? 25,
                        color: widget.timercolor ?? Colors.amber,
                        fontWeight: widget.timerfontWeight ?? FontWeight.bold,
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(
                        vertical: 8.0, horizontal: 30),
                    child: PinCodeTextField(
                      length: 6, // must be greater than 0
                      obsecureText: false,
                      shape: PinCodeFieldShape.underline,
                      onDone: widget.ondone,
                      // onDone: (String value) {
                      //   setState(() {
                      //     passcode = value;
                      //     print(value);
                      //   });
                      // },

                      textStyle: TextStyle(
                          fontWeight: FontWeight
                              .bold), //optinal, default is TextStyle(fontSize: 18, color: Colors.black, fontWeight: FontWeight.bold)
                      onErrorCheck: widget.onErrorcheck,
                      shouldTriggerFucntions:
                      changeNotifier.stream.asBroadcastStream(),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 30.0),
                    child: Text(
                      hasError
                          ? "*Please fill up all the cells and press VERIFY again"
                          : "",
                      style:
                      TextStyle(color: Colors.red.shade300, fontSize: 12),
                    ),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  RichText(
                    textAlign: TextAlign.center,
                    text: TextSpan(
                        text: "Didn't receive the code? ",
                        style: TextStyle(color: Colors.black54, fontSize: 15),
                        children: [
                          TextSpan(
                              text: " RESEND",
                              // recognizer: onTapRecognizer,
                              style: TextStyle(
                                  color: widget.color,
                                  fontWeight:
                                  widget.fontweight ?? FontWeight.bold,
                                  fontSize: widget.fontsize ?? 16))
                        ]),
                  ),
                  SizedBox(
                    height: 7,
                  ),
                  Container(
                    margin: const EdgeInsets.symmetric(
                        vertical: 16.0, horizontal: 30),
                    child: ButtonTheme(
                      height: 50,
                      child: FlatButton(
                        onPressed: widget.onPressed,
                        child: Center(
                            child: Text(
                              widget.buttontext ?? "VERIFY".toUpperCase(),
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold),
                            )),
                      ),
                    ),
                    decoration: BoxDecoration(
                      color: widget.color ?? Colors.blue,
                      borderRadius: BorderRadius.circular(5),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      actions: <Widget>[
        FlatButton(
          child: Text('Close'),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );
  }
}

class CountDownTimer extends StatefulWidget {
  const CountDownTimer({
    Key key,
    int secondsRemaining,
    this.countDownTimerStyle,
    this.whenTimeExpires,
    this.countDownFormatter,
  })  : secondsRemaining = secondsRemaining,
        super(key: key);

  final int secondsRemaining;
  final Function whenTimeExpires;
  final Function countDownFormatter;
  final TextStyle countDownTimerStyle;

  State createState() =>  _CountDownTimerState();
}

class _CountDownTimerState extends State<CountDownTimer>
    with TickerProviderStateMixin {
  AnimationController _controller;
  Duration duration;
  String formatHHMMSS(int seconds) {
    int hours = (seconds / 3600).truncate();
    seconds = (seconds % 3600).truncate();
    int minutes = (seconds / 60).truncate();

    String hoursStr = (hours).toString().padLeft(2, '0');
    String minutesStr = (minutes).toString().padLeft(2, '0');
    String secondsStr = (seconds % 60).toString().padLeft(2, '0');

    if (hours == 0) {
      return "$minutesStr:$secondsStr";
    }

    return "$hoursStr:$minutesStr:$secondsStr";
  }

  String get timerDisplayString {
    Duration duration = _controller.duration * _controller.value;
    return widget.countDownFormatter != null
        ? widget.countDownFormatter(duration.inSeconds)
        : formatHHMMSS(duration.inSeconds);
    // In case user doesn't provide formatter use the default one
    // for that create a method which will be called formatHHMMSS or whatever you like
  }

  @override
  void initState() {
    super.initState();
    duration =  Duration(seconds: widget.secondsRemaining);
    _controller =  AnimationController(
      vsync: this,
      duration: duration,
    );
    _controller.reverse(from: widget.secondsRemaining.toDouble());
    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed ||
          status == AnimationStatus.dismissed) {
        widget.whenTimeExpires();
      }
    });
  }

  @override
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return  Center(
        child: AnimatedBuilder(
            animation: _controller,
            builder: (_, Widget child) {
              return Text(
                timerDisplayString,
                style: widget.countDownTimerStyle,
              );
            }));
  }
}

class UnicornAlertDialog extends StatelessWidget {
  const UnicornAlertDialog({
    Key key,
    @required this.gradient,
    this.title,
    this.titlePadding,
    this.titleTextStyle,
    this.content,
    this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
    this.contentTextStyle,
    this.actions,
    this.backgroundColor,
    this.elevation,
    this.semanticLabel,
    this.shape,
  })  : assert(contentPadding != null),
        super(key: key);

  final Gradient gradient;
  final Widget title;
  final EdgeInsetsGeometry titlePadding;
  final TextStyle titleTextStyle;
  final Widget content;
  final EdgeInsetsGeometry contentPadding;
  final TextStyle contentTextStyle;
  final List<Widget> actions;
  final Color backgroundColor;
  final double elevation;
  final String semanticLabel;
  final ShapeBorder shape;

  @override
  Widget build(BuildContext context) {
    assert(debugCheckHasMaterialLocalizations(context));
    final ThemeData theme = Theme.of(context);
    final DialogTheme dialogTheme = DialogTheme.of(context);
    final List<Widget> children = <Widget>[];
    String label = semanticLabel;

    if (title != null) {
      children.add(Padding(
        padding: titlePadding ?? EdgeInsets.fromLTRB(24.0, 24.0, 24.0, content == null ? 20.0 : 0.0),
        child: DefaultTextStyle(
          style: titleTextStyle ?? dialogTheme.titleTextStyle ?? theme.textTheme.title,
          child: Semantics(
            child: title,
            namesRoute: true,
            container: true,
          ),
        ),
      ));
    } else {
      switch (defaultTargetPlatform) {
        case TargetPlatform.iOS:
          label = semanticLabel;
          break;
        case TargetPlatform.android:
        case TargetPlatform.fuchsia:
          label = semanticLabel ?? MaterialLocalizations.of(context)?.alertDialogLabel;
      }
    }

    if (content != null) {
      children.add(Flexible(
        child: Padding(
          padding: contentPadding,
          child: DefaultTextStyle(
            style: contentTextStyle ?? dialogTheme.contentTextStyle ?? theme.textTheme.subhead,
            child: content,
          ),
        ),
      ));
    }

    if (actions != null) {
      children.add(ButtonTheme.bar(
        child: ButtonBar(
          children: actions,
        ),
      ));
    }

    Widget dialogChild = IntrinsicWidth(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: children,
      ),
    );

    if (label != null)
      dialogChild = Semantics(
        namesRoute: true,
        label: label,
        child: dialogChild,
      );

    return Dialog(
      backgroundColor: backgroundColor,
      gradient: gradient,
      elevation: elevation,
      shape: shape,
      child: dialogChild,
    );
  }
}

class Dialog extends StatelessWidget {
  const Dialog({
    Key key,
    this.gradient,
    this.backgroundColor,
    this.elevation,
    this.insetAnimationDuration = const Duration(milliseconds: 100),
    this.insetAnimationCurve = Curves.decelerate,
    this.shape,
    this.child,
  }) : super(key: key);

  final Color backgroundColor;
  final double elevation;
  final Duration insetAnimationDuration;
  final Curve insetAnimationCurve;
  final ShapeBorder shape;
  final Widget child;
  final Gradient gradient;

  static const RoundedRectangleBorder _defaultDialogShape =
  RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4.0)));
  static const double _defaultElevation = 24.0;

  @override
  Widget build(BuildContext context) {
    final DialogTheme dialogTheme = DialogTheme.of(context);
    return AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets + const EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
      duration: insetAnimationDuration,
      curve: insetAnimationCurve,
      child: MediaQuery.removeViewInsets(
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
        child: Center(
          child: ConstrainedBox(
            constraints: const BoxConstraints(minWidth: 280.0),
            child: Material(
              color: backgroundColor ?? dialogTheme.backgroundColor ?? Theme.of(context).dialogBackgroundColor,
              elevation: elevation ?? dialogTheme.elevation ?? _defaultElevation,
              shape: shape ?? dialogTheme.shape ?? _defaultDialogShape,
              type: MaterialType.card,
              child: ClipRRect(
                borderRadius: _defaultDialogShape.borderRadius,
                child: Container(
                  decoration: BoxDecoration(
                      gradient: gradient
                  ),
                  child: child,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}



_showDialog(BuildContext context) {
  showDialog(
      context: context,
      builder: (context) {
        return UnicornAlertDialog(
            title: GestureDetector(
              onTap: () { print("on tap title");},
              child: Column(
                children: <Widget>[
                  Container(
                    child: Image.asset('assets/images/background.jpg'),
                  ),
                  const SizedBox(height: 15.0),
                  Container(
                    child: Text(
                      'Verify',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 20.0,
                      ),
                    ),
                  )
                ],
              ),
            ),
            content: GestureDetector(
              onTap: () { print("on tap content");},
              child: Text('You have successfully verified your mobile number',
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white, fontSize: 15.0)),
            ),
            gradient: LinearGradient(
              colors: <Color>[
                Color(0xDD4a00e0),
                Color(0xFF8e2de2),
              ],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
            ),
            actions: <Widget>[


            ]
        );
      });
}

_showCustomDialog(BuildContext context) {
  showDialog(
      context: context,
      builder: (context) {
        return TudoDialogWidget(
            title: Text('Tudo dialog'),
            onPressed: () { print("click on press");    },
        );
      });
}

Future<void> _ackAlert(BuildContext context) {
  return showDialog<void>(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Not in stock'),
        content: const Text('This item is no longer available'),
        actions: <Widget>[
          FlatButton(
            child: Text('Ok'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}
enum ConfirmAction { CANCEL, ACCEPT }
Future<ConfirmAction> _asyncConfirmDialog(BuildContext context) async {
  return showDialog<ConfirmAction>(
    context: context,
    barrierDismissible: false, // user must tap button for close dialog!
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Reset settings?'),
        content: const Text(
            'This will reset your device to its default factory settings.'),
        actions: <Widget>[
          FlatButton(
            child: const Text('CANCEL'),
            onPressed: () {
              Navigator.of(context).pop(ConfirmAction.CANCEL);
            },
          ),
          FlatButton(
            child: const Text('ACCEPT'),
            onPressed: () {
              Navigator.of(context).pop(ConfirmAction.ACCEPT);
            },
          )
        ],
      );
    },
  );
}

Future<String> _asyncInputDialog(BuildContext context) async {
  String teamName = '';
  return showDialog<String>(
    context: context,
    barrierDismissible: false, // dialog is dismissible with a tap on the barrier
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Enter current team'),
        content:  Row(
          children: <Widget>[
             Expanded(
                child:  TextField(
                  autofocus: true,
                  decoration:  InputDecoration(
                      labelText: 'Team Name', hintText: 'eg. Juventus F.C.'),
                  onChanged: (value) {
                    teamName = value;
                  },
                ))
          ],
        ),
        actions: <Widget>[
          FlatButton(
            child: Text('Ok'),
            onPressed: () {
              Navigator.of(context).pop(teamName);
            },
          ),
        ],
      );
    },
  );
}

enum Departments { Production, Research, Purchasing, Marketing, Accounting }

Future<Departments> _asyncSimpleDialog(BuildContext context) async {
  return await showDialog<Departments>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext context) {
        return SimpleDialog(
          title: const Text('Select Departments '),
          children: <Widget>[
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, Departments.Production);
              },
              child: const Text('Production'),
            ),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, Departments.Research);
              },
              child: const Text('Research'),
            ),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, Departments.Purchasing);
              },
              child: const Text('Purchasing'),
            ),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, Departments.Marketing);
              },
              child: const Text('Marketing'),
            ),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, Departments.Accounting);
              },
              child: const Text('Accounting'),
            )
          ],
        );
      });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return  Scaffold(
      appBar: AppBar(
        title: Text("Dialog"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
             RaisedButton(
              onPressed: () {
                _showCustomDialog(context);
              },
              child: const Text("Custom Dialog"),
            ),
             RaisedButton(
              onPressed: () {
                _showDialog(context);
              },
              child: const Text("Unicon Dialog"),
            ),
             RaisedButton(
              onPressed: () {
                _ackAlert(context);
              },
              child: const Text("Ack Dialog"),
            ),
             RaisedButton(
              onPressed: () async {
                final ConfirmAction action = await _asyncConfirmDialog(context);
                print("Confirm Action $action" );
              },
              child: const Text("Confirm Dialog"),
            ),
             RaisedButton(
              onPressed: () async {
                final Departments deptName = await _asyncSimpleDialog(context);
                print("Selected Departement is $deptName");
              },
              child: const Text("Simple dialog"),
            ),
             RaisedButton(
              onPressed: () async {
                final String currentTeam = await _asyncInputDialog(context);
                print("Current team name is $currentTeam");
              },
              child: const Text("Input Dialog"),
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp( MaterialApp(home:  MyApp()));
}

enter image description here

Upvotes: 1

Related Questions