dialog_demo.dart 6.73 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';

Hans Muller's avatar
Hans Muller committed
7
import 'full_screen_dialog_demo.dart';
8

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

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

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

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

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

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

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

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

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

59 60 61 62 63 64 65 66 67
  TimeOfDay _selectedTime;

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

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

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

    return new Scaffold(
88
      key: _scaffoldKey,
89
      appBar: new AppBar(
90
        title: const Text('Dialogs')
91
      ),
92
      body: new ListView(
93
        padding: const EdgeInsets.symmetric(vertical: 24.0, horizontal: 72.0),
94 95
        children: <Widget>[
          new RaisedButton(
96
            child: const Text('ALERT'),
97
            onPressed: () {
98
              showDemoDialog<DialogDemoAction>(
99
                context: context,
100
                child: new AlertDialog(
101 102
                  content: new Text(
                    _alertWithoutTitleText,
Hans Muller's avatar
Hans Muller committed
103 104
                    style: dialogTextStyle
                  ),
105 106
                  actions: <Widget>[
                    new FlatButton(
107
                      child: const Text('CANCEL'),
108 109 110
                      onPressed: () { Navigator.pop(context, DialogDemoAction.cancel); }
                    ),
                    new FlatButton(
111
                      child: const Text('DISCARD'),
112
                      onPressed: () { Navigator.pop(context, DialogDemoAction.discard); }
Hans Muller's avatar
Hans Muller committed
113
                    )
114
                  ]
Hans Muller's avatar
Hans Muller committed
115
                )
116 117 118 119
              );
            }
          ),
          new RaisedButton(
120
            child: const Text('ALERT WITH TITLE'),
121
            onPressed: () {
122
              showDemoDialog<DialogDemoAction>(
123
                context: context,
124
                child: new AlertDialog(
125
                  title: const Text('Use Google\'s location service?'),
126 127 128 129 130 131
                  content: new Text(
                    _alertWithTitleText,
                    style: dialogTextStyle
                  ),
                  actions: <Widget>[
                    new FlatButton(
132
                      child: const Text('DISAGREE'),
133 134 135
                      onPressed: () { Navigator.pop(context, DialogDemoAction.disagree); }
                    ),
                    new FlatButton(
136
                      child: const Text('AGREE'),
137 138 139 140 141 142 143 144
                      onPressed: () { Navigator.pop(context, DialogDemoAction.agree); }
                    )
                  ]
                )
              );
            }
          ),
          new RaisedButton(
145
            child: const Text('SIMPLE'),
146
            onPressed: () {
147
              showDemoDialog<String>(
148
                context: context,
149
                child: new SimpleDialog(
150
                  title: const Text('Set backup account'),
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
                  children: <Widget>[
                    new DialogDemoItem(
                      icon: Icons.account_circle,
                      color: theme.primaryColor,
                      text: 'username@gmail.com',
                      onPressed: () { Navigator.pop(context, 'username@gmail.com'); }
                    ),
                    new DialogDemoItem(
                      icon: Icons.account_circle,
                      color: theme.primaryColor,
                      text: 'user02@gmail.com',
                      onPressed: () { Navigator.pop(context, 'user02@gmail.com'); }
                    ),
                    new DialogDemoItem(
                      icon: Icons.add_circle,
                      text: 'add account',
                      color: theme.disabledColor
                    )
                  ]
170 171 172 173 174
                )
              );
            }
          ),
          new RaisedButton(
175
            child: const Text('CONFIRMATION'),
176 177 178
            onPressed: () {
              showTimePicker(
                context: context,
179
                initialTime: _selectedTime
180
              )
181
              .then<Null>((TimeOfDay value) {
182
                if (value != null && value != _selectedTime) {
183
                  _selectedTime = value;
184
                  _scaffoldKey.currentState.showSnackBar(new SnackBar(
185
                    content: new Text('You selected: ${value.format(context)}')
186 187 188 189 190 191
                  ));
                }
              });
            }
          ),
          new RaisedButton(
192
            child: const Text('FULLSCREEN'),
193
            onPressed: () {
Hans Muller's avatar
Hans Muller committed
194
              Navigator.push(context, new MaterialPageRoute<DismissDialogAction>(
195 196
                builder: (BuildContext context) => new FullScreenDialogDemo(),
                fullscreenDialog: true,
197 198
              ));
            }
199
          ),
200
        ]
201 202 203 204 205 206 207 208
        // Add a little space between the buttons
        .map((Widget button) {
          return new Container(
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            child: button
          );
        })
        .toList()
Hans Muller's avatar
Hans Muller committed
209
      )
210 211 212
    );
  }
}