contacts_demo.dart 12.4 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 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';

7
class _ContactCategory extends StatelessWidget {
8
  const _ContactCategory({ Key key, this.icon, this.children }) : super(key: key);
9

10
  final IconData icon;
11 12
  final List<Widget> children;

13
  @override
14
  Widget build(BuildContext context) {
15
    final ThemeData themeData = Theme.of(context);
16
    return new Container(
17
      padding: const EdgeInsets.symmetric(vertical: 16.0),
18
      decoration: new BoxDecoration(
19
        border: new Border(bottom: new BorderSide(color: themeData.dividerColor))
20
      ),
21
      child: new DefaultTextStyle(
22
        style: Theme.of(context).textTheme.subhead,
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
        child: new SafeArea(
          top: false,
          bottom: false,
          child: new Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              new Container(
                padding: const EdgeInsets.symmetric(vertical: 24.0),
                width: 72.0,
                child: new Icon(icon, color: themeData.primaryColor)
              ),
              new Expanded(child: new Column(children: children))
            ],
          ),
        ),
      ),
39 40 41 42
    );
  }
}

43
class _ContactItem extends StatelessWidget {
44 45 46
  _ContactItem({ Key key, this.icon, this.lines, this.tooltip, this.onPressed })
    : assert(lines.length > 1),
      super(key: key);
47

48
  final IconData icon;
49
  final List<String> lines;
50 51
  final String tooltip;
  final VoidCallback onPressed;
52

53
  @override
54
  Widget build(BuildContext context) {
55
    final ThemeData themeData = Theme.of(context);
56
    final List<Widget> columnChildren = lines.sublist(0, lines.length - 1).map((String line) => new Text(line)).toList();
57
    columnChildren.add(new Text(lines.last, style: themeData.textTheme.caption));
58

59
    final List<Widget> rowChildren = <Widget>[
60
      new Expanded(
Hans Muller's avatar
Hans Muller committed
61 62 63 64
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: columnChildren
        )
65 66 67 68 69
      )
    ];
    if (icon != null) {
      rowChildren.add(new SizedBox(
        width: 72.0,
70 71 72 73 74
        child: new IconButton(
          icon: new Icon(icon),
          color: themeData.primaryColor,
          onPressed: onPressed
        )
75 76
      ));
    }
77 78 79 80 81 82 83 84
    return new MergeSemantics(
      child: new Padding(
        padding: const EdgeInsets.symmetric(vertical: 16.0),
        child: new Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: rowChildren
        )
      ),
85 86 87 88
    );
  }
}

89 90
class ContactsDemo extends StatefulWidget {
  static const String routeName = '/contacts';
91

92
  @override
93
  ContactsDemoState createState() => new ContactsDemoState();
94 95
}

96
enum AppBarBehavior { normal, pinned, floating, snapping }
97

98
class ContactsDemoState extends State<ContactsDemo> {
99
  static final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
100
  final double _appBarHeight = 256.0;
101

102
  AppBarBehavior _appBarBehavior = AppBarBehavior.pinned;
103

104
  @override
105 106 107
  Widget build(BuildContext context) {
    return new Theme(
      data: new ThemeData(
108
        brightness: Brightness.light,
109 110
        primarySwatch: Colors.indigo,
        platform: Theme.of(context).platform,
111 112
      ),
      child: new Scaffold(
113
        key: _scaffoldKey,
114 115 116 117 118
        body: new CustomScrollView(
          slivers: <Widget>[
            new SliverAppBar(
              expandedHeight: _appBarHeight,
              pinned: _appBarBehavior == AppBarBehavior.pinned,
119 120
              floating: _appBarBehavior == AppBarBehavior.floating || _appBarBehavior == AppBarBehavior.snapping,
              snap: _appBarBehavior == AppBarBehavior.snapping,
121 122
              actions: <Widget>[
                new IconButton(
123
                  icon: const Icon(Icons.create),
124
                  tooltip: 'Edit',
125
                  onPressed: () {
126
                    _scaffoldKey.currentState.showSnackBar(const SnackBar(
127
                      content: const Text("Editing isn't supported in this screen.")
128 129
                    ));
                  },
130
                ),
131 132 133 134 135
                new PopupMenuButton<AppBarBehavior>(
                  onSelected: (AppBarBehavior value) {
                    setState(() {
                      _appBarBehavior = value;
                    });
136
                  },
137
                  itemBuilder: (BuildContext context) => <PopupMenuItem<AppBarBehavior>>[
138
                    const PopupMenuItem<AppBarBehavior>(
139
                      value: AppBarBehavior.normal,
140
                      child: const Text('App bar scrolls away')
141
                    ),
142
                    const PopupMenuItem<AppBarBehavior>(
143
                      value: AppBarBehavior.pinned,
144
                      child: const Text('App bar stays put')
145
                    ),
146
                    const PopupMenuItem<AppBarBehavior>(
147
                      value: AppBarBehavior.floating,
148
                      child: const Text('App bar floats')
149
                    ),
150 151 152 153
                    const PopupMenuItem<AppBarBehavior>(
                      value: AppBarBehavior.snapping,
                      child: const Text('App bar snaps')
                    ),
154
                  ],
155
                ),
156 157
              ],
              flexibleSpace: new FlexibleSpaceBar(
158
                title: const Text('Ali Connors'),
159
                background: new Stack(
160
                  fit: StackFit.expand,
161 162
                  children: <Widget>[
                    new Image.asset(
163 164
                      'ali_connors.jpg',
                      package: 'flutter_gallery_assets',
165
                      fit: BoxFit.cover,
166 167 168 169
                      height: _appBarHeight,
                    ),
                    // This gradient ensures that the toolbar icons are distinct
                    // against the background image.
170
                    const DecoratedBox(
171 172
                      decoration: const BoxDecoration(
                        gradient: const LinearGradient(
173 174
                          begin: const Alignment(0.0, -1.0),
                          end: const Alignment(0.0, -0.4),
175 176 177 178 179
                          colors: const <Color>[const Color(0x60000000), const Color(0x00000000)],
                        ),
                      ),
                    ),
                  ],
180
                ),
181
              ),
182
            ),
183 184 185 186 187 188 189 190 191
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                new _ContactCategory(
                  icon: Icons.call,
                  children: <Widget>[
                    new _ContactItem(
                      icon: Icons.message,
                      tooltip: 'Send message',
                      onPressed: () {
192
                        _scaffoldKey.currentState.showSnackBar(const SnackBar(
193
                          content: const Text('Pretend that this opened your SMS application.')
194 195
                        ));
                      },
196
                      lines: const <String>[
197 198 199 200 201 202 203 204
                        '(650) 555-1234',
                        'Mobile',
                      ],
                    ),
                    new _ContactItem(
                      icon: Icons.message,
                      tooltip: 'Send message',
                      onPressed: () {
205
                        _scaffoldKey.currentState.showSnackBar(const SnackBar(
206
                          content: const Text('A messaging app appears.')
207 208
                        ));
                      },
209
                      lines: const <String>[
210 211 212 213 214 215 216 217
                        '(323) 555-6789',
                        'Work',
                      ],
                    ),
                    new _ContactItem(
                      icon: Icons.message,
                      tooltip: 'Send message',
                      onPressed: () {
218
                        _scaffoldKey.currentState.showSnackBar(const SnackBar(
219
                          content: const Text('Imagine if you will, a messaging application.')
220 221
                        ));
                      },
222
                      lines: const <String>[
223 224 225 226 227
                        '(650) 555-6789',
                        'Home',
                      ],
                    ),
                  ],
228
                ),
229 230 231 232 233 234 235
                new _ContactCategory(
                  icon: Icons.contact_mail,
                  children: <Widget>[
                    new _ContactItem(
                      icon: Icons.email,
                      tooltip: 'Send personal e-mail',
                      onPressed: () {
236
                        _scaffoldKey.currentState.showSnackBar(const SnackBar(
237
                          content: const Text('Here, your e-mail application would open.')
238 239
                        ));
                      },
240
                      lines: const <String>[
241 242 243 244 245 246 247 248
                        'ali_connors@example.com',
                        'Personal',
                      ],
                    ),
                    new _ContactItem(
                      icon: Icons.email,
                      tooltip: 'Send work e-mail',
                      onPressed: () {
249
                        _scaffoldKey.currentState.showSnackBar(const SnackBar(
250
                          content: const Text('Summon your favorite e-mail application here.')
251 252
                        ));
                      },
253
                      lines: const <String>[
254 255 256 257 258
                        'aliconnors@example.com',
                        'Work',
                      ],
                    ),
                  ],
259
                ),
260 261 262 263 264 265 266
                new _ContactCategory(
                  icon: Icons.location_on,
                  children: <Widget>[
                    new _ContactItem(
                      icon: Icons.map,
                      tooltip: 'Open map',
                      onPressed: () {
267
                        _scaffoldKey.currentState.showSnackBar(const SnackBar(
268
                          content: const Text('This would show a map of San Francisco.')
269 270
                        ));
                      },
271
                      lines: const <String>[
272 273 274 275 276 277 278 279 280
                        '2000 Main Street',
                        'San Francisco, CA',
                        'Home',
                      ],
                    ),
                    new _ContactItem(
                      icon: Icons.map,
                      tooltip: 'Open map',
                      onPressed: () {
281
                        _scaffoldKey.currentState.showSnackBar(const SnackBar(
282
                          content: const Text('This would show a map of Mountain View.')
283 284
                        ));
                      },
285
                      lines: const <String>[
286 287 288 289 290 291 292 293 294
                        '1600 Amphitheater Parkway',
                        'Mountain View, CA',
                        'Work',
                      ],
                    ),
                    new _ContactItem(
                      icon: Icons.map,
                      tooltip: 'Open map',
                      onPressed: () {
295
                        _scaffoldKey.currentState.showSnackBar(const SnackBar(
296
                          content: const Text('This would also show a map, if this was not a demo.')
297 298
                        ));
                      },
299
                      lines: const <String>[
300 301 302 303 304 305
                        '126 Severyns Ave',
                        'Mountain View, CA',
                        'Jet Travel',
                      ],
                    ),
                  ],
306
                ),
307 308 309 310
                new _ContactCategory(
                  icon: Icons.today,
                  children: <Widget>[
                    new _ContactItem(
311
                      lines: const <String>[
312 313 314 315 316
                        'Birthday',
                        'January 9th, 1989',
                      ],
                    ),
                    new _ContactItem(
317
                      lines: const <String>[
318 319 320 321 322
                        'Wedding anniversary',
                        'June 21st, 2014',
                      ],
                    ),
                    new _ContactItem(
323
                      lines: const <String>[
324 325 326 327 328
                        'First day in office',
                        'January 20th, 2015',
                      ],
                    ),
                    new _ContactItem(
329
                      lines: const <String>[
330 331 332 333 334
                        'Last day in office',
                        'August 9th, 2015',
                      ],
                    ),
                  ],
335
                ),
336 337 338 339 340
              ]),
            ),
          ],
        ),
      ),
341 342 343
    );
  }
}