dialog_demo.dart 6.77 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

7
import '../../gallery/demo.dart';
Hans Muller's avatar
Hans Muller committed
8
import 'full_screen_dialog_demo.dart';
9

Hans Muller's avatar
Hans Muller committed
10 11 12 13 14 15 16
enum DialogDemoAction {
  cancel,
  discard,
  disagree,
  agree,
}

17
const String _alertWithoutTitleText = 'Discard draft?';
Hans Muller's avatar
Hans Muller committed
18 19

const String _alertWithTitleText =
Josh Soref's avatar
Josh Soref committed
20
  'Let Google help apps determine location. This means sending anonymous location '
21
  'data to Google, even when no apps are running.';
Hans Muller's avatar
Hans Muller committed
22

23
class DialogDemoItem extends StatelessWidget {
24
  const DialogDemoItem({ Key key, this.icon, this.color, this.text, this.onPressed }) : super(key: key);
Hans Muller's avatar
Hans Muller committed
25

26
  final IconData icon;
Hans Muller's avatar
Hans Muller committed
27 28 29 30
  final Color color;
  final String text;
  final VoidCallback onPressed;

31
  @override
Hans Muller's avatar
Hans Muller committed
32
  Widget build(BuildContext context) {
33
    return SimpleDialogOption(
34
      onPressed: onPressed,
35
      child: Row(
36 37 38
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
39 40
          Icon(icon, size: 36.0, color: color),
          Padding(
41
            padding: const EdgeInsets.only(left: 16.0),
42
            child: Text(text),
43 44 45
          ),
        ],
      ),
Hans Muller's avatar
Hans Muller committed
46 47 48 49
    );
  }
}

50
class DialogDemo extends StatefulWidget {
51
  static const String routeName = '/material/dialog';
52

53
  @override
54
  DialogDemoState createState() => DialogDemoState();
Hans Muller's avatar
Hans Muller committed
55 56 57
}

class DialogDemoState extends State<DialogDemo> {
58
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
Hans Muller's avatar
Hans Muller committed
59

60 61 62 63 64
  TimeOfDay _selectedTime;

  @override
  void initState() {
    super.initState();
65 66
    final DateTime now = DateTime.now();
    _selectedTime = TimeOfDay(hour: now.hour, minute: now.minute);
67 68
  }

69 70
  void showDemoDialog<T>({ BuildContext context, Widget child }) {
    showDialog<T>(
Hans Muller's avatar
Hans Muller committed
71
      context: context,
72
      builder: (BuildContext context) => child,
Hans Muller's avatar
Hans Muller committed
73
    )
74
    .then<void>((T value) { // The value passed to Navigator.pop() or null.
Hans Muller's avatar
Hans Muller committed
75
      if (value != null) {
76
        _scaffoldKey.currentState.showSnackBar(SnackBar(
77
          content: Text('You selected: $value'),
Hans Muller's avatar
Hans Muller committed
78 79 80 81
        ));
      }
    });
  }
82

83
  @override
84 85
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);
86
    final TextStyle dialogTextStyle = theme.textTheme.subtitle1.copyWith(color: theme.textTheme.caption.color);
Hans Muller's avatar
Hans Muller committed
87

88
    return Scaffold(
89
      key: _scaffoldKey,
90
      appBar: AppBar(
91 92
        title: const Text('Dialogs'),
        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 103
                child: AlertDialog(
                  content: Text(
104
                    _alertWithoutTitleText,
105
                    style: dialogTextStyle,
Hans Muller's avatar
Hans Muller committed
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 115 116 117
                      onPressed: () { Navigator.pop(context, DialogDemoAction.discard); },
                    ),
                  ],
                ),
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 140 141 142
                      onPressed: () { Navigator.pop(context, DialogDemoAction.agree); },
                    ),
                  ],
                ),
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 156 157
                      icon: Icons.account_circle,
                      color: theme.primaryColor,
                      text: 'username@gmail.com',
158
                      onPressed: () { Navigator.pop(context, 'username@gmail.com'); },
159
                    ),
160
                    DialogDemoItem(
161 162 163
                      icon: Icons.account_circle,
                      color: theme.primaryColor,
                      text: 'user02@gmail.com',
164
                      onPressed: () { Navigator.pop(context, 'user02@gmail.com'); },
165
                    ),
166
                    DialogDemoItem(
167 168
                      icon: Icons.add_circle,
                      text: 'add account',
169 170 171 172
                      color: theme.disabledColor,
                    ),
                  ],
                ),
173
              );
174
            },
175
          ),
176
          RaisedButton(
177
            child: const Text('CONFIRMATION'),
178 179 180
            onPressed: () {
              showTimePicker(
                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 197
              Navigator.push(context, MaterialPageRoute<DismissDialogAction>(
                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 211
        .toList(),
      ),
212 213 214
    );
  }
}