1// Copyright 2016 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'package:flutter/material.dart'; 6 7import '../../gallery/demo.dart'; 8import 'full_screen_dialog_demo.dart'; 9 10enum DialogDemoAction { 11 cancel, 12 discard, 13 disagree, 14 agree, 15} 16 17const String _alertWithoutTitleText = 'Discard draft?'; 18 19const String _alertWithTitleText = 20 'Let Google help apps determine location. This means sending anonymous location ' 21 'data to Google, even when no apps are running.'; 22 23class DialogDemoItem extends StatelessWidget { 24 const DialogDemoItem({ Key key, this.icon, this.color, this.text, this.onPressed }) : super(key: key); 25 26 final IconData icon; 27 final Color color; 28 final String text; 29 final VoidCallback onPressed; 30 31 @override 32 Widget build(BuildContext context) { 33 return SimpleDialogOption( 34 onPressed: onPressed, 35 child: Row( 36 mainAxisAlignment: MainAxisAlignment.start, 37 crossAxisAlignment: CrossAxisAlignment.center, 38 children: <Widget>[ 39 Icon(icon, size: 36.0, color: color), 40 Padding( 41 padding: const EdgeInsets.only(left: 16.0), 42 child: Text(text), 43 ), 44 ], 45 ), 46 ); 47 } 48} 49 50class DialogDemo extends StatefulWidget { 51 static const String routeName = '/material/dialog'; 52 53 @override 54 DialogDemoState createState() => DialogDemoState(); 55} 56 57class DialogDemoState extends State<DialogDemo> { 58 final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>(); 59 60 TimeOfDay _selectedTime; 61 62 @override 63 void initState() { 64 super.initState(); 65 final DateTime now = DateTime.now(); 66 _selectedTime = TimeOfDay(hour: now.hour, minute: now.minute); 67 } 68 69 void showDemoDialog<T>({ BuildContext context, Widget child }) { 70 showDialog<T>( 71 context: context, 72 builder: (BuildContext context) => child, 73 ) 74 .then<void>((T value) { // The value passed to Navigator.pop() or null. 75 if (value != null) { 76 _scaffoldKey.currentState.showSnackBar(SnackBar( 77 content: Text('You selected: $value'), 78 )); 79 } 80 }); 81 } 82 83 @override 84 Widget build(BuildContext context) { 85 final ThemeData theme = Theme.of(context); 86 final TextStyle dialogTextStyle = theme.textTheme.subhead.copyWith(color: theme.textTheme.caption.color); 87 88 return Scaffold( 89 key: _scaffoldKey, 90 appBar: AppBar( 91 title: const Text('Dialogs'), 92 actions: <Widget>[MaterialDemoDocumentationButton(DialogDemo.routeName)], 93 ), 94 body: ListView( 95 padding: const EdgeInsets.symmetric(vertical: 24.0, horizontal: 72.0), 96 children: <Widget>[ 97 RaisedButton( 98 child: const Text('ALERT'), 99 onPressed: () { 100 showDemoDialog<DialogDemoAction>( 101 context: context, 102 child: AlertDialog( 103 content: Text( 104 _alertWithoutTitleText, 105 style: dialogTextStyle, 106 ), 107 actions: <Widget>[ 108 FlatButton( 109 child: const Text('CANCEL'), 110 onPressed: () { Navigator.pop(context, DialogDemoAction.cancel); }, 111 ), 112 FlatButton( 113 child: const Text('DISCARD'), 114 onPressed: () { Navigator.pop(context, DialogDemoAction.discard); }, 115 ), 116 ], 117 ), 118 ); 119 }, 120 ), 121 RaisedButton( 122 child: const Text('ALERT WITH TITLE'), 123 onPressed: () { 124 showDemoDialog<DialogDemoAction>( 125 context: context, 126 child: AlertDialog( 127 title: const Text('Use Google\'s location service?'), 128 content: Text( 129 _alertWithTitleText, 130 style: dialogTextStyle, 131 ), 132 actions: <Widget>[ 133 FlatButton( 134 child: const Text('DISAGREE'), 135 onPressed: () { Navigator.pop(context, DialogDemoAction.disagree); }, 136 ), 137 FlatButton( 138 child: const Text('AGREE'), 139 onPressed: () { Navigator.pop(context, DialogDemoAction.agree); }, 140 ), 141 ], 142 ), 143 ); 144 }, 145 ), 146 RaisedButton( 147 child: const Text('SIMPLE'), 148 onPressed: () { 149 showDemoDialog<String>( 150 context: context, 151 child: SimpleDialog( 152 title: const Text('Set backup account'), 153 children: <Widget>[ 154 DialogDemoItem( 155 icon: Icons.account_circle, 156 color: theme.primaryColor, 157 text: 'username@gmail.com', 158 onPressed: () { Navigator.pop(context, 'username@gmail.com'); }, 159 ), 160 DialogDemoItem( 161 icon: Icons.account_circle, 162 color: theme.primaryColor, 163 text: 'user02@gmail.com', 164 onPressed: () { Navigator.pop(context, 'user02@gmail.com'); }, 165 ), 166 DialogDemoItem( 167 icon: Icons.add_circle, 168 text: 'add account', 169 color: theme.disabledColor, 170 ), 171 ], 172 ), 173 ); 174 }, 175 ), 176 RaisedButton( 177 child: const Text('CONFIRMATION'), 178 onPressed: () { 179 showTimePicker( 180 context: context, 181 initialTime: _selectedTime, 182 ) 183 .then<void>((TimeOfDay value) { 184 if (value != null && value != _selectedTime) { 185 _selectedTime = value; 186 _scaffoldKey.currentState.showSnackBar(SnackBar( 187 content: Text('You selected: ${value.format(context)}'), 188 )); 189 } 190 }); 191 }, 192 ), 193 RaisedButton( 194 child: const Text('FULLSCREEN'), 195 onPressed: () { 196 Navigator.push(context, MaterialPageRoute<DismissDialogAction>( 197 builder: (BuildContext context) => FullScreenDialogDemo(), 198 fullscreenDialog: true, 199 )); 200 }, 201 ), 202 ] 203 // Add a little space between the buttons 204 .map<Widget>((Widget button) { 205 return Container( 206 padding: const EdgeInsets.symmetric(vertical: 8.0), 207 child: button, 208 ); 209 }) 210 .toList(), 211 ), 212 ); 213 } 214} 215