Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Created April 24, 2018 04:02
Show Gist options
  • Save branflake2267/f777a5e23454713f5f1c916d66b5b842 to your computer and use it in GitHub Desktop.
Save branflake2267/f777a5e23454713f5f1c916d66b5b842 to your computer and use it in GitHub Desktop.
Flutter - Alert Dialogs
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.purple,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Home"),
),
body: new ListTile(
title: new Text("Some item"),
trailing: new RaisedButton(
child: new Text("Delete"),
onPressed: _handlePressed,
),
),
);
}
void _handlePressed() {
var onYes = () {
print("~~~ onYes");
};
var onNo = () {
print("~~~ onYes");
};
confirmDialog3(context, onYes, onNo).then((_) {
print("done");
});
}
}
Future<bool> confirmDialog1(BuildContext context) {
return showDialog<bool>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return new AlertDialog(
title: new Text('Are you sure?'),
actions: <Widget>[
new FlatButton(
child: const Text('YES'),
onPressed: () {
Navigator.of(context).pop(true);
},
),
new FlatButton(
child: const Text('NO'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
],
);
});
}
Future<Map> confirmDialog2(BuildContext context) {
return showDialog<Map>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return new AlertDialog(
title: new Text('Are you sure?'),
actions: <Widget>[
new FlatButton(
child: const Text('YES'),
onPressed: () {
var sendBack = <String, dynamic>{
'from': 'Brandon',
'value': true,
};
Navigator.of(context).pop(sendBack);
},
),
new FlatButton(
child: const Text('NO'),
onPressed: () {
var sendBack = <String, dynamic>{
'from': 'Brandon',
'value': false,
};
Navigator.of(context).pop(sendBack);
},
),
],
);
});
}
Future<Null> confirmDialog3(BuildContext context, void onNo(), void onYes()) {
return showDialog<Null>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return new AlertDialog(
title: new Text('Are you sure?'),
actions: <Widget>[
new FlatButton(
child: const Text('YES'),
onPressed: () {
Navigator.of(context).pop();
onYes();
},
),
new FlatButton(
child: const Text('NO'),
onPressed: () {
Navigator.of(context).pop();
onNo();
},
),
],
);
});
}
@branflake2267
Copy link
Author

https://www.youtube.com/watch?v=760HtgNHvLo - Youtube video covering this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment