dialog_demo.dart 7.86 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 23 24 25 26 27 28 29 30
enum DialogDemoAction {
  cancel,
  discard,
  disagree,
  agree,
}

const String _introText =
  "Use dialogs sparingly because their sudden appearance forces users to stop their "
  "current task and focus on the dialog's content. Alternatives to dialogs include "
  "menus or inline expansion, both of which maintain the current context.";

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.";

class DialogDemoItem extends StatelessComponent {
  DialogDemoItem({ Key key, this.icon, this.color, this.text, this.onPressed }) : super(key: key);

31
  final IconData icon;
Hans Muller's avatar
Hans Muller committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45
  final Color color;
  final String text;
  final VoidCallback onPressed;

  Widget build(BuildContext context) {
    return new InkWell(
      onTap: onPressed,
      child: new Padding(
        padding: const EdgeDims.symmetric(vertical: 8.0),
        child: new Row(
          justifyContent: FlexJustifyContent.start,
          alignItems: FlexAlignItems.center,
          children: <Widget>[
            new Icon(
46
              size: 36.0,
Hans Muller's avatar
Hans Muller committed
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
              icon: icon,
              color: color
            ),
            new Padding(
              padding: const EdgeDims.only(left: 16.0),
              child: new Text(text)
            )
          ]
        )
      )
    );
  }
}

class DialogDemo extends StatefulComponent {
  DialogDemoState createState() => new DialogDemoState();
}

class DialogDemoState extends State<DialogDemo> {
  final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();

  void showDemoDialog({ BuildContext context, Dialog dialog }) {
    showDialog(
      context: context,
      child: dialog
    )
    .then((dynamic value) { // The value passed to Navigator.pop() or null.
      if (value != null) {
        scaffoldKey.currentState.showSnackBar(new SnackBar(
          content: new Text('You selected: $value')
        ));
      }
    });
  }
81 82 83

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

    return new Scaffold(
      key: scaffoldKey,
      toolBar: new ToolBar(
        center: new Text('Dialogs')
90
      ),
Hans Muller's avatar
Hans Muller committed
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      body: new ButtonTheme(
        color: ButtonColor.accent,
        child: new Padding(
          padding: const EdgeDims.all(24.0),
          child: new ScrollableViewport(
            child: new Column(
              alignItems: FlexAlignItems.stretch,
              children: <Widget>[
                new Container(
                  child: new Text(
                    _introText,
                    style: dialogTextStyle
                  ),
                  padding: const EdgeDims.only(top: 8.0, bottom: 24.0),
                  margin: const EdgeDims.only(bottom:16.0),
                  decoration: new BoxDecoration(
                    border: new Border(bottom: new BorderSide(color: theme.dividerColor))
                  )
                ),
                new FlatButton(
                  child: new Text('Alert without a title'),
                  onPressed: () {
                    showDemoDialog(
                      context: context,
                      dialog: new Dialog(
                        content: new Text(
                          _alertWithoutTitleText,
                          style: dialogTextStyle
                        ),
                        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); }
                          )
                        ]
                      )
                    );
                  }
                ),
                new FlatButton(
                  child: new Text('Alert with a title'),
                  onPressed: () {
                    showDemoDialog(
                      context: context,
                      dialog: new Dialog(
                        title: new Text("Use Google's location service?"),
                        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 FlatButton(
                  child: new Text('Simple Dialog'),
                  onPressed: () {
                    showDemoDialog(
                      context: context,
                      dialog: new Dialog(
                        title: new Text('Set backup account'),
                        content: new Column(
                          children: <Widget>[
                            new DialogDemoItem(
169
                              icon: Icons.account_circle,
Hans Muller's avatar
Hans Muller committed
170 171 172 173 174
                              color: theme.primaryColor,
                              text: 'username@gmail.com',
                              onPressed: () { Navigator.pop(context, 'username@gmail.com'); }
                            ),
                            new DialogDemoItem(
175
                              icon: Icons.account_circle,
Hans Muller's avatar
Hans Muller committed
176 177 178 179 180
                              color: theme.primaryColor,
                              text: 'user02@gmail.com',
                              onPressed: () { Navigator.pop(context, 'user02@gmail.com'); }
                            ),
                            new DialogDemoItem(
181
                              icon: Icons.add_circle,
Hans Muller's avatar
Hans Muller committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
                              text: 'add account',
                              color: theme.disabledColor
                            )
                          ]
                        )
                      )
                    );
                  }
                ),
                new FlatButton(
                  child: new Text('Confirmation Dialog'),
                  onPressed: () {
                    showTimePicker(
                      context: context,
                      initialTime: const TimeOfDay(hour: 15, minute: 30)
                    )
                    .then((value) { // The value passed to Navigator.pop() or null.
                      if (value != null) {
                        scaffoldKey.currentState.showSnackBar(new SnackBar(
                          content: new Text('You selected: $value')
                        ));
                      }
                    });
                  }
                ),
                new FlatButton(
                  child: new Text('Fullscreen Dialog'),
                  onPressed: () {
                    Navigator.push(context, new MaterialPageRoute(
                      builder: (BuildContext context) => new FullScreenDialogDemo()
                    ));
                  }
                )
              ]
            )
          )
218
        )
Hans Muller's avatar
Hans Muller committed
219
      )
220 221 222
    );
  }
}