1+ import 'package:flutter/material.dart' ;
2+
3+ class Index extends StatefulWidget {
4+ @override
5+ _IndexState createState () => _IndexState ();
6+ }
7+
8+ class _IndexState extends State <Index > {
9+
10+ List Action = ['Ok' , 'Cancel' ];
11+ String _choice = 'Nothing' ;
12+
13+ Future _openAlertDialog () async {
14+ final action = await showDialog (
15+ context: context,
16+ barrierDismissible: false , // user must tap button
17+ builder: (BuildContext context) {
18+ return AlertDialog (
19+ title: Text ('AlertDialog' ),
20+ content: Text ('Are you sure about this?' ),
21+ titlePadding: EdgeInsets .fromLTRB (24.0 , 24.0 , 24.0 , 0.0 ),
22+ contentPadding: EdgeInsets .fromLTRB (24.0 , 20.0 , 24.0 , 24.0 ),
23+ actions: List .generate (2 , (index) {
24+ return FlatButton (
25+ child: Text ('${Action [index ]}' ,style: TextStyle (color: Theme .of (context).primaryColor),),
26+ onPressed: (){
27+ Navigator .pop (context, Action [index]);
28+ },
29+ );
30+ }),
31+ );
32+ }
33+ );
34+ switch (action) {
35+ case 'Ok' :
36+ setState (() {
37+ _choice = 'Ok' ;
38+ });
39+ break ;
40+ case 'Cancel' :
41+ setState (() {
42+ _choice = 'Cancel' ;
43+ });
44+ break ;
45+ default :
46+ }
47+ }
48+
49+
50+ @override
51+ Widget build (BuildContext context) {
52+ return Scaffold (
53+ appBar: AppBar (title: Text ('AlertDialog' ),),
54+ body: Center (
55+ child: Text ('你的选择是:$_choice ' ),
56+ ),
57+ floatingActionButton: FloatingActionButton (
58+ onPressed: _openAlertDialog,
59+ child: Icon (Icons .camera_enhance, color: Colors .white, size: 30.0 ,),
60+ backgroundColor: Theme .of (context).primaryColor,
61+ ),
62+ );
63+ }
64+ }
0 commit comments