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

enum Action { Ok, Cancel }

class ConfirmDialog01 extends StatefulWidget {
  ConfirmDialog01(
      {this.title = '标题', this.msg = '提示信息', this.okText = '确定', this.cancelText = '取消'});

  String title;
  String msg;
  String okText;
  String cancelText;

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

class _ConfirmDialog01State extends State<ConfirmDialog01> {
  String _choice = 'Nothing';

  Future _openAlertDialog() async {
    final action = await showDialog(
      context: context,
      barrierDismissible: false, //// user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(widget.title),
          content: Text(widget.msg),
          actions: <Widget>[
            FlatButton(
              child: Text(widget.cancelText),
              onPressed: () {
                Navigator.pop(context, Action.Cancel);
              },
            ),
            FlatButton(
              child: Text(widget.okText),
              onPressed: () {
                Navigator.pop(context, Action.Ok);
              },
            ),
          ],
        );
      },
    );

    switch (action) {
      case Action.Ok:
        setState(() {
          _choice = 'Ok';
        });
        break;
      case Action.Cancel:
        setState(() {
          _choice = 'Cancel';
        });
        break;
      default:
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ConfirmDialog01'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Your choice is: $_choice'),
            SizedBox(
              height: 16.0,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                  child: Text('Open AlertDialog'),
                  onPressed: _openAlertDialog,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
