Created
May 27, 2020 23:57
-
-
Save mingsai/2f04b6d7f29a2ba34d702c3f53dc0afe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
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: new Row( | |
children: <Widget>[ | |
new Expanded( | |
child: new TextField( | |
autofocus: true, | |
decoration: new 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 new Scaffold( | |
appBar: AppBar( | |
title: Text("Dialog"), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
new RaisedButton( | |
onPressed: () { | |
_ackAlert(context); | |
}, | |
child: const Text("Ack Dialog"), | |
), | |
new RaisedButton( | |
onPressed: () async { | |
final ConfirmAction action = await _asyncConfirmDialog(context); | |
print("Confirm Action $action" ); | |
}, | |
child: const Text("Confirm Dialog"), | |
), | |
new RaisedButton( | |
onPressed: () async { | |
final Departments deptName = await _asyncSimpleDialog(context); | |
print("Selected Departement is $deptName"); | |
}, | |
child: const Text("Simple dialog"), | |
), | |
new RaisedButton( | |
onPressed: () async { | |
final String currentTeam = await _asyncInputDialog(context); | |
print("Current team name is $currentTeam"); | |
}, | |
child: const Text("Input Dialog"), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
void main() { | |
runApp(new MaterialApp(home: new MyApp())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment