full_screen_dialog_demo.dart 8.47 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4
// 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.

5 6
import 'dart:async';

Hans Muller's avatar
Hans Muller committed
7
import 'package:flutter/material.dart';
8
import 'package:intl/intl.dart';
Hans Muller's avatar
Hans Muller committed
9 10

// This demo is based on
11
// https://material.google.com/components/dialogs.html#dialogs-full-screen-dialogs
Hans Muller's avatar
Hans Muller committed
12 13 14 15 16 17 18

enum DismissDialogAction {
  cancel,
  discard,
  save,
}

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

  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
      child: new Row(
        children: <Widget>[
38
          new Expanded(
Hans Muller's avatar
Hans Muller committed
39
            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
              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))
                  )
52
                  .then<Null>((DateTime value) {
53 54
                    if (value != null)
                      onChanged(new DateTime(value.year, value.month, value.day, time.hour, time.minute));
Hans Muller's avatar
Hans Muller committed
55 56 57
                  });
                },
                child: new Row(
58
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
Hans Muller's avatar
Hans Muller committed
59 60
                  children: <Widget>[
                    new Text(new DateFormat('EEE, MMM d yyyy').format(date)),
61
                    const Icon(Icons.arrow_drop_down, color: Colors.black54),
Hans Muller's avatar
Hans Muller committed
62 63 64 65 66 67
                  ]
                )
              )
            )
          ),
          new Container(
68 69
            margin: const EdgeInsets.only(left: 8.0),
            padding: const EdgeInsets.symmetric(vertical: 8.0),
Hans Muller's avatar
Hans Muller committed
70 71 72 73 74 75 76 77 78
            decoration: new BoxDecoration(
              border: new Border(bottom: new BorderSide(color: theme.dividerColor))
            ),
            child: new InkWell(
              onTap: () {
                showTimePicker(
                  context: context,
                  initialTime: time
                )
79
                .then<Null>((TimeOfDay value) {
80 81
                  if (value != null)
                    onChanged(new DateTime(date.year, date.month, date.day, value.hour, value.minute));
Hans Muller's avatar
Hans Muller committed
82 83 84 85
                });
              },
              child: new Row(
                children: <Widget>[
86
                  new Text('${time.format(context)}'),
87
                  const Icon(Icons.arrow_drop_down, color: Colors.black54),
Hans Muller's avatar
Hans Muller committed
88 89 90 91 92 93 94 95 96 97
                ]
              )
            )
          )
        ]
      )
    );
  }
}

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

class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
104 105 106 107
  DateTime _fromDateTime = new DateTime.now();
  DateTime _toDateTime = new DateTime.now();
  bool _allDayValue = false;
  bool _saveNeeded = false;
108 109 110
  bool _hasLocation = false;
  bool _hasName = false;
  String _eventName;
Hans Muller's avatar
Hans Muller committed
111

112
  Future<bool> _onWillPop() async {
113
    _saveNeeded = _hasLocation || _hasName || _saveNeeded;
114 115
    if (!_saveNeeded)
      return true;
Hans Muller's avatar
Hans Muller committed
116 117

    final ThemeData theme = Theme.of(context);
118
    final TextStyle dialogTextStyle = theme.textTheme.subhead.copyWith(color: theme.textTheme.caption.color);
Hans Muller's avatar
Hans Muller committed
119

120
    return await showDialog<bool>(
Hans Muller's avatar
Hans Muller committed
121
      context: context,
122 123 124 125 126
      builder: (BuildContext context) {
        return new AlertDialog(
          content: new Text(
            'Discard new event?',
            style: dialogTextStyle
Hans Muller's avatar
Hans Muller committed
127
          ),
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
          actions: <Widget>[
            new FlatButton(
              child: const Text('CANCEL'),
              onPressed: () {
                Navigator.of(context).pop(false); // Pops the confirmation dialog but not the page.
              }
            ),
            new FlatButton(
              child: const Text('DISCARD'),
              onPressed: () {
                Navigator.of(context).pop(true); // Returning true to _onWillPop will pop again.
              }
            )
          ],
        );
      },
144
    ) ?? false;
Hans Muller's avatar
Hans Muller committed
145 146
  }

147
  @override
Hans Muller's avatar
Hans Muller committed
148 149 150 151
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);

    return new Scaffold(
152
      appBar: new AppBar(
153
        title: new Text(_hasName ? _eventName : 'Event Name TBD'),
154
        actions: <Widget> [
Hans Muller's avatar
Hans Muller committed
155
          new FlatButton(
156
            child: new Text('SAVE', style: theme.textTheme.body1.copyWith(color: Colors.white)),
Hans Muller's avatar
Hans Muller committed
157 158 159 160 161 162
            onPressed: () {
              Navigator.pop(context, DismissDialogAction.save);
            }
          )
        ]
      ),
163 164 165 166 167 168 169
      body: new Form(
        onWillPop: _onWillPop,
        child: new ListView(
          padding: const EdgeInsets.all(16.0),
          children: <Widget>[
            new Container(
              padding: const EdgeInsets.symmetric(vertical: 8.0),
170
              alignment: Alignment.bottomLeft,
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
              child: new TextField(
                decoration: const InputDecoration(
                  labelText: 'Event name',
                  filled: true
                ),
                style: theme.textTheme.headline,
                onChanged: (String value) {
                  setState(() {
                    _hasName = value.isNotEmpty;
                    if (_hasName) {
                      _eventName = value;
                    }
                  });
                }
              )
186
            ),
187 188
            new Container(
              padding: const EdgeInsets.symmetric(vertical: 8.0),
189
              alignment: Alignment.bottomLeft,
190 191 192 193 194 195 196 197 198 199 200 201
              child: new TextField(
                decoration: const InputDecoration(
                  labelText: 'Location',
                  hintText: 'Where is the event?',
                  filled: true
                ),
                onChanged: (String value) {
                  setState(() {
                    _hasLocation = value.isNotEmpty;
                  });
                }
              )
202
            ),
203 204 205 206 207 208 209 210 211 212 213 214 215 216
            new Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Text('From', style: theme.textTheme.caption),
                new DateTimeItem(
                  dateTime: _fromDateTime,
                  onChanged: (DateTime value) {
                    setState(() {
                      _fromDateTime = value;
                      _saveNeeded = true;
                    });
                  }
                )
              ]
217
            ),
218 219 220 221 222 223 224
            new Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Text('To', style: theme.textTheme.caption),
                new DateTimeItem(
                  dateTime: _toDateTime,
                  onChanged: (DateTime value) {
225
                    setState(() {
226
                      _toDateTime = value;
227 228 229 230
                      _saveNeeded = true;
                    });
                  }
                ),
xster's avatar
xster committed
231
                const Text('All-day'),
232
              ]
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            ),
            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;
                      });
                    }
                  ),
xster's avatar
xster committed
249
                  const Text('All-day'),
250 251
                ]
              )
252
            )
253 254 255 256 257 258 259 260 261 262 263
          ]
          .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
264 265 266
    );
  }
}