dialog_demo.dart 6.74 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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 77
        _scaffoldKey.currentState.showSnackBar(SnackBar(
          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.subhead.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,
Hans Muller's avatar
Hans Muller committed
105 106
                    style: dialogTextStyle
                  ),
107
                  actions: <Widget>[
108
                    FlatButton(
109
                      child: const Text('CANCEL'),
110 111
                      onPressed: () { Navigator.pop(context, DialogDemoAction.cancel); }
                    ),
112
                    FlatButton(
113
                      child: const Text('DISCARD'),
114
                      onPressed: () { Navigator.pop(context, DialogDemoAction.discard); }
Hans Muller's avatar
Hans Muller committed
115
                    )
116
                  ]
Hans Muller's avatar
Hans Muller committed
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 130 131 132
                    _alertWithTitleText,
                    style: dialogTextStyle
                  ),
                  actions: <Widget>[
133
                    FlatButton(
134
                      child: const Text('DISAGREE'),
135 136
                      onPressed: () { Navigator.pop(context, DialogDemoAction.disagree); }
                    ),
137
                    FlatButton(
138
                      child: const Text('AGREE'),
139 140 141 142 143 144 145
                      onPressed: () { Navigator.pop(context, DialogDemoAction.agree); }
                    )
                  ]
                )
              );
            }
          ),
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 158 159
                      icon: Icons.account_circle,
                      color: theme.primaryColor,
                      text: 'username@gmail.com',
                      onPressed: () { Navigator.pop(context, 'username@gmail.com'); }
                    ),
160
                    DialogDemoItem(
161 162 163 164 165
                      icon: Icons.account_circle,
                      color: theme.primaryColor,
                      text: 'user02@gmail.com',
                      onPressed: () { Navigator.pop(context, 'user02@gmail.com'); }
                    ),
166
                    DialogDemoItem(
167 168 169 170 171
                      icon: Icons.add_circle,
                      text: 'add account',
                      color: theme.disabledColor
                    )
                  ]
172 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 187
                  _scaffoldKey.currentState.showSnackBar(SnackBar(
                    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 207 208 209 210
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            child: button
          );
        })
        .toList()
Hans Muller's avatar
Hans Muller committed
211
      )
212 213 214
    );
  }
}