full_screen_dialog_demo.dart 7.61 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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:intl/intl.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

// This demo is based on
// https://www.google.com/design/spec/components/dialogs.html#dialogs-full-screen-dialogs

enum DismissDialogAction {
  cancel,
  discard,
  save,
}

18
class DateTimeItem extends StatelessWidget {
Hans Muller's avatar
Hans Muller committed
19 20 21 22 23 24 25 26 27 28 29
  DateTimeItem({ Key key, DateTime dateTime, this.onChanged })
    : date = new DateTime(dateTime.year, dateTime.month, dateTime.day),
      time = new TimeOfDay(hour: dateTime.hour, minute: dateTime.minute),
      super(key: key) {
    assert(onChanged != null);
  }

  final DateTime date;
  final TimeOfDay time;
  final ValueChanged<DateTime> onChanged;

30
  @override
Hans Muller's avatar
Hans Muller committed
31 32 33
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);

34
    return new DefaultTextStyle(
35
      style: theme.textTheme.subhead,
Hans Muller's avatar
Hans Muller committed
36 37 38 39
      child: new Row(
        children: <Widget>[
          new Flexible(
            child: new Container(
40
              padding: const EdgeInsets.symmetric(vertical: 8.0),
Hans Muller's avatar
Hans Muller committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
              decoration: new BoxDecoration(
                border: new Border(bottom: new BorderSide(color: theme.dividerColor))
              ),
              child: new InkWell(
                onTap: () {
                  showDatePicker(
                    context: context,
                    initialDate: date,
                    firstDate: date.subtract(const Duration(days: 30)),
                    lastDate: date.add(const Duration(days: 30))
                  )
                  .then((DateTime value) {
                    onChanged(new DateTime(value.year, value.month, value.day, time.hour, time.minute));
                  });
                },
                child: new Row(
57
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
Hans Muller's avatar
Hans Muller committed
58 59
                  children: <Widget>[
                    new Text(new DateFormat('EEE, MMM d yyyy').format(date)),
Ian Hickson's avatar
Ian Hickson committed
60
                    new Icon(Icons.arrow_drop_down, color: Colors.black54),
Hans Muller's avatar
Hans Muller committed
61 62 63 64 65 66
                  ]
                )
              )
            )
          ),
          new Container(
67 68
            margin: const EdgeInsets.only(left: 8.0),
            padding: const EdgeInsets.symmetric(vertical: 8.0),
Hans Muller's avatar
Hans Muller committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
            decoration: new BoxDecoration(
              border: new Border(bottom: new BorderSide(color: theme.dividerColor))
            ),
            child: new InkWell(
              onTap: () {
                showTimePicker(
                  context: context,
                  initialTime: time
                )
                .then((TimeOfDay value) {
                  onChanged(new DateTime(date.year, date.month, date.day, value.hour, value.minute));
                });
              },
              child: new Row(
                children: <Widget>[
                  new Text('$time'),
Ian Hickson's avatar
Ian Hickson committed
85
                  new Icon(Icons.arrow_drop_down, color: Colors.black54),
Hans Muller's avatar
Hans Muller committed
86 87 88 89 90 91 92 93 94 95
                ]
              )
            )
          )
        ]
      )
    );
  }
}

96
class FullScreenDialogDemo extends StatefulWidget {
97
  @override
Hans Muller's avatar
Hans Muller committed
98 99 100 101
  FullScreenDialogDemoState createState() => new FullScreenDialogDemoState();
}

class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
102 103 104 105
  DateTime _fromDateTime = new DateTime.now();
  DateTime _toDateTime = new DateTime.now();
  bool _allDayValue = false;
  bool _saveNeeded = false;
Hans Muller's avatar
Hans Muller committed
106 107

  void handleDismissButton(BuildContext context) {
108
    if (!_saveNeeded) {
Hans Muller's avatar
Hans Muller committed
109 110 111 112 113
      Navigator.pop(context, null);
      return;
    }

    final ThemeData theme = Theme.of(context);
114
    final TextStyle dialogTextStyle = theme.textTheme.subhead.copyWith(color: theme.textTheme.caption.color);
Hans Muller's avatar
Hans Muller committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    showDialog(
      context: context,
      child: new Dialog(
        content: new Text(
          'Discard new event?',
          style: dialogTextStyle
        ),
        actions: <Widget>[
          new FlatButton(
            child: new Text('CANCEL'),
            onPressed: () { Navigator.pop(context, DismissDialogAction.cancel); }
          ),
          new FlatButton(
            child: new Text('DISCARD'),
            onPressed: () {
131 132 133
              Navigator.of(context)
                ..pop(DismissDialogAction.discard) // pop the cancel/discard dialog
                ..pop(); // pop this route
Hans Muller's avatar
Hans Muller committed
134 135 136 137 138 139 140
            }
          )
        ]
      )
    );
  }

141
  @override
Hans Muller's avatar
Hans Muller committed
142 143 144 145
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);

    return new Scaffold(
146 147
      appBar: new AppBar(
        leading: new IconButton(
Ian Hickson's avatar
Ian Hickson committed
148
          icon: new Icon(Icons.clear),
Hans Muller's avatar
Hans Muller committed
149 150
          onPressed: () { handleDismissButton(context); }
        ),
151
        title: new Text('New event'),
152
        actions: <Widget> [
Hans Muller's avatar
Hans Muller committed
153
          new FlatButton(
154
            child: new Text('SAVE', style: theme.textTheme.body1.copyWith(color: Colors.white)),
Hans Muller's avatar
Hans Muller committed
155 156 157 158 159 160
            onPressed: () {
              Navigator.pop(context, DismissDialogAction.save);
            }
          )
        ]
      ),
161
      body: new Block(
162
        padding: const EdgeInsets.all(16.0),
163 164 165 166 167 168
        children: <Widget>[
          new Container(
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            decoration: new BoxDecoration(
              border: new Border(bottom: new BorderSide(color: theme.dividerColor))
            ),
169 170
            align: FractionalOffset.bottomLeft,
            child: new Text('Event name', style: theme.textTheme.display2)
171 172 173 174 175 176
          ),
          new Container(
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            decoration: new BoxDecoration(
              border: new Border(bottom: new BorderSide(color: theme.dividerColor))
            ),
177 178
            align: FractionalOffset.bottomLeft,
            child: new Text('Location', style: theme.textTheme.title.copyWith(color: Colors.black54))
179 180 181
          ),
          new Column(
            crossAxisAlignment: CrossAxisAlignment.start,
Hans Muller's avatar
Hans Muller committed
182
            children: <Widget>[
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
              new Text('From', style: theme.textTheme.caption),
              new DateTimeItem(
                dateTime: _fromDateTime,
                onChanged: (DateTime value) {
                  setState(() {
                    _fromDateTime = value;
                    _saveNeeded = true;
                  });
                }
              )
            ]
          ),
          new Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              new Text('To', style: theme.textTheme.caption),
              new DateTimeItem(
                dateTime: _toDateTime,
                onChanged: (DateTime value) {
                  setState(() {
                    _toDateTime = value;
                    _saveNeeded = true;
                  });
                }
Hans Muller's avatar
Hans Muller committed
207 208
              )
            ]
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
          ),
          new Container(
            decoration: new BoxDecoration(
              border: new Border(bottom: new BorderSide(color: theme.dividerColor))
            ),
            child: new Row(
              children: <Widget> [
                new Checkbox(
                  value: _allDayValue,
                  onChanged: (bool value) {
                    setState(() {
                      _allDayValue = value;
                      _saveNeeded = true;
                    });
                  }
                ),
                new Text('All-day')
              ]
            )
Hans Muller's avatar
Hans Muller committed
228
          )
229 230 231 232 233 234 235 236 237
        ]
        .map((Widget child) {
          return new Container(
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            height: 96.0,
            child: child
          );
        })
        .toList()
Hans Muller's avatar
Hans Muller committed
238 239 240 241
      )
    );
  }
}