dialog_demo.dart 6.73 KB
Newer Older
1 2 3 4 5 6 7
// 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';
import 'package:flutter/widgets.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 17 18 19 20 21 22
enum DialogDemoAction {
  cancel,
  discard,
  disagree,
  agree,
}

const String _alertWithoutTitleText = "Discard draft?";

const String _alertWithTitleText =
  "Let Google help apps determine location. This means sending anyonmous location "
  "data to Google, even when no apps are running.";

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

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 33 34 35
  Widget build(BuildContext context) {
    return new InkWell(
      onTap: onPressed,
      child: new Padding(
36
        padding: const EdgeInsets.symmetric(vertical: 8.0),
Hans Muller's avatar
Hans Muller committed
37
        child: new Row(
38 39
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
Hans Muller's avatar
Hans Muller committed
40 41
          children: <Widget>[
            new Icon(
Ian Hickson's avatar
Ian Hickson committed
42
              icon,
43
              size: 36.0,
Hans Muller's avatar
Hans Muller committed
44 45 46
              color: color
            ),
            new Padding(
47
              padding: const EdgeInsets.only(left: 16.0),
Hans Muller's avatar
Hans Muller committed
48 49 50 51 52 53 54 55 56
              child: new Text(text)
            )
          ]
        )
      )
    );
  }
}

57
class DialogDemo extends StatefulWidget {
58 59
  static const String routeName = '/dialog';

60
  @override
Hans Muller's avatar
Hans Muller committed
61 62 63 64
  DialogDemoState createState() => new DialogDemoState();
}

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

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

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

    return new Scaffold(
87
      key: _scaffoldKey,
88 89
      appBar: new AppBar(
        title: new Text('Dialogs')
90
      ),
91
      body: new Block(
92
        padding: const EdgeInsets.symmetric(vertical: 24.0, horizontal: 72.0),
93 94 95 96
        children: <Widget>[
          new RaisedButton(
            child: new Text('ALERT'),
            onPressed: () {
97
              showDemoDialog/*<DialogDemoAction>*/(
98 99 100 101
                context: context,
                dialog: new Dialog(
                  content: new Text(
                    _alertWithoutTitleText,
Hans Muller's avatar
Hans Muller committed
102 103
                    style: dialogTextStyle
                  ),
104 105 106 107 108 109 110 111
                  actions: <Widget>[
                    new FlatButton(
                      child: new Text('CANCEL'),
                      onPressed: () { Navigator.pop(context, DialogDemoAction.cancel); }
                    ),
                    new FlatButton(
                      child: new Text('DISCARD'),
                      onPressed: () { Navigator.pop(context, DialogDemoAction.discard); }
Hans Muller's avatar
Hans Muller committed
112
                    )
113
                  ]
Hans Muller's avatar
Hans Muller committed
114
                )
115 116 117 118 119 120
              );
            }
          ),
          new RaisedButton(
            child: new Text('ALERT WITH TITLE'),
            onPressed: () {
121
              showDemoDialog/*<DialogDemoAction>*/(
122 123
                context: context,
                dialog: new Dialog(
124
                  title: new Text('Use Google\'s location service?'),
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
                  content: new Text(
                    _alertWithTitleText,
                    style: dialogTextStyle
                  ),
                  actions: <Widget>[
                    new FlatButton(
                      child: new Text('DISAGREE'),
                      onPressed: () { Navigator.pop(context, DialogDemoAction.disagree); }
                    ),
                    new FlatButton(
                      child: new Text('AGREE'),
                      onPressed: () { Navigator.pop(context, DialogDemoAction.agree); }
                    )
                  ]
                )
              );
            }
          ),
          new RaisedButton(
            child: new Text('SIMPLE'),
            onPressed: () {
146
              showDemoDialog/*<String>*/(
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
                context: context,
                dialog: new Dialog(
                  title: new Text('Set backup account'),
                  content: new Column(
                    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
                      )
                    ]
                  )
                )
              );
            }
          ),
          new RaisedButton(
            child: new Text('CONFIRMATION'),
            onPressed: () {
              showTimePicker(
                context: context,
                initialTime: const TimeOfDay(hour: 15, minute: 30)
              )
182
              .then((TimeOfDay value) { // The value passed to Navigator.pop() or null.
183
                if (value != null) {
184
                  _scaffoldKey.currentState.showSnackBar(new SnackBar(
185 186 187 188 189 190 191 192 193
                    content: new Text('You selected: $value')
                  ));
                }
              });
            }
          ),
          new RaisedButton(
            child: new Text('FULLSCREEN'),
            onPressed: () {
Hans Muller's avatar
Hans Muller committed
194
              Navigator.push(context, new MaterialPageRoute<DismissDialogAction>(
195 196 197
                builder: (BuildContext context) => new FullScreenDialogDemo()
              ));
            }
Hans Muller's avatar
Hans Muller committed
198
          )
199
        ]
200 201 202 203 204 205 206 207
        // 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
208
      )
209 210 211
    );
  }
}