-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAlertDialogAndToast.dart
More file actions
120 lines (108 loc) · 3.14 KB
/
AlertDialogAndToast.dart
File metadata and controls
120 lines (108 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import 'package:flutter/material.dart';
import 'model/Contact.dart';
import 'main.dart';
void main() {
runApp(const MessagesExample());
}
class MessagesExample extends StatelessWidget {
const MessagesExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AlertDialog and Toast',
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: const MessagesExamplePage(title: 'AlertDialog and Toast'),
);
}
}
class MessagesExamplePage extends StatefulWidget {
const MessagesExamplePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MessagesExamplePage> createState() => _MessagesExamplePageState();
}
class _MessagesExamplePageState extends State<MessagesExamplePage> {
Future<void> _showMyDialog() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: const Text('AlertDialog Title'),
content: SingleChildScrollView(
child: ListBody(
children: const <Widget>[
Text('Hi! It is an AlertDialog!'),
Text('Do you want to proceed?'),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('Confirm'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
void openAnotherWidget(){
Navigator.push( context, MaterialPageRoute(builder: (context) => MyApp()));
}
void backToPreviousWidget(){
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Messages:',
),
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
onPressed: () {
_showMyDialog();
},
child: Text('Dialog'),
),
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
onPressed: () {
final scaffold = ScaffoldMessenger.of(context);
scaffold.showSnackBar(
SnackBar(
content: const Text('This is a Toast message.'),
action: SnackBarAction(label: 'Close', onPressed: scaffold.hideCurrentSnackBar),
),
);
},
child: Text('Toast'),
)
],
),
),
);
}
}