list_demo.dart 6.28 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4 5 6
// 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';

7
class ListDemo extends StatefulWidget {
Hans Muller's avatar
Hans Muller committed
8 9
  ListDemo({ Key key }) : super(key: key);

10 11
  static const String routeName = '/list';

12
  @override
Hans Muller's avatar
Hans Muller committed
13 14 15 16
  ListDemoState createState() => new ListDemoState();
}

class ListDemoState extends State<ListDemo> {
17
  static final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
Hans Muller's avatar
Hans Muller committed
18

19
  PersistentBottomSheetController<Null> _bottomSheet;
20
  MaterialListType _itemType = MaterialListType.threeLine;
Hans Muller's avatar
Hans Muller committed
21 22 23 24
  bool _dense = false;
  bool _showAvatars = true;
  bool _showIcons = false;
  bool _showDividers = false;
Hans Muller's avatar
Hans Muller committed
25 26 27 28 29
  bool _reverseSort = false;
  List<String> items = <String>[
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'
  ];

30
  void changeItemType(MaterialListType type) {
Hans Muller's avatar
Hans Muller committed
31
    setState(() {
32
      _itemType = type;
Hans Muller's avatar
Hans Muller committed
33 34 35 36 37 38 39 40
    });
    _bottomSheet?.setState(() { });
  }

  void showConfigurationSheet(BuildContext appContext) {
    _bottomSheet = scaffoldKey.currentState.showBottomSheet((BuildContext bottomSheetContext) {
      return new Container(
        decoration: new BoxDecoration(
Hixie's avatar
Hixie committed
41
          border: new Border(top: new BorderSide(color: Colors.black26))
Hans Muller's avatar
Hans Muller committed
42
        ),
43
        child: new Block(
Hans Muller's avatar
Hans Muller committed
44 45
          children: <Widget>[
            new ListItem(
46
              dense: true,
47
              title: new Text('One-line'),
48 49 50 51
              trailing: new Radio<MaterialListType>(
                value: _showAvatars ? MaterialListType.oneLineWithAvatar : MaterialListType.oneLine,
                groupValue: _itemType,
                onChanged: changeItemType
Hans Muller's avatar
Hans Muller committed
52 53 54
              )
            ),
            new ListItem(
55
              dense: true,
56
              title: new Text('Two-line'),
57 58 59 60
              trailing: new Radio<MaterialListType>(
                value: MaterialListType.twoLine,
                groupValue: _itemType,
                onChanged: changeItemType
Hans Muller's avatar
Hans Muller committed
61 62 63
              )
            ),
            new ListItem(
64
              dense: true,
65
              title: new Text('Three-line'),
66 67 68 69
              trailing: new Radio<MaterialListType>(
                value: MaterialListType.threeLine,
                groupValue: _itemType,
                onChanged: changeItemType
Hans Muller's avatar
Hans Muller committed
70 71 72
              )
            ),
            new ListItem(
73
              dense: true,
74
              title: new Text('Show avatar'),
75
              trailing: new Checkbox(
Hans Muller's avatar
Hans Muller committed
76
                value: _showAvatars,
Hans Muller's avatar
Hans Muller committed
77 78
                onChanged: (bool value) {
                  setState(() {
Hans Muller's avatar
Hans Muller committed
79
                    _showAvatars = value;
Hans Muller's avatar
Hans Muller committed
80 81 82 83 84 85
                  });
                  _bottomSheet?.setState(() { });
                }
              )
            ),
            new ListItem(
86
              dense: true,
87
              title: new Text('Show icon'),
88
              trailing: new Checkbox(
Hans Muller's avatar
Hans Muller committed
89
                value: _showIcons,
Hans Muller's avatar
Hans Muller committed
90 91
                onChanged: (bool value) {
                  setState(() {
Hans Muller's avatar
Hans Muller committed
92 93 94 95 96 97 98 99
                    _showIcons = value;
                  });
                  _bottomSheet?.setState(() { });
                }
              )
            ),
            new ListItem(
              dense: true,
100
              title: new Text('Show dividers'),
101
              trailing: new Checkbox(
Hans Muller's avatar
Hans Muller committed
102 103 104 105
                value: _showDividers,
                onChanged: (bool value) {
                  setState(() {
                    _showDividers = value;
Hans Muller's avatar
Hans Muller committed
106 107 108 109 110 111
                  });
                  _bottomSheet?.setState(() { });
                }
              )
            ),
            new ListItem(
112
              dense: true,
113
              title: new Text('Dense layout'),
114
              trailing: new Checkbox(
115
                value: _dense,
Hans Muller's avatar
Hans Muller committed
116 117
                onChanged: (bool value) {
                  setState(() {
118
                    _dense = value;
Hans Muller's avatar
Hans Muller committed
119 120 121 122 123 124 125 126 127 128 129 130 131
                  });
                  _bottomSheet?.setState(() { });
                }
              )
            )
          ]
        )
      );
    });
  }

  Widget buildListItem(BuildContext context, String item) {
    Widget secondary;
132
    if (_itemType == MaterialListType.twoLine) {
Hans Muller's avatar
Hans Muller committed
133 134 135
      secondary = new Text(
        "Additional item information."
      );
136
    } else if (_itemType == MaterialListType.threeLine) {
Hans Muller's avatar
Hans Muller committed
137 138 139 140 141
      secondary = new Text(
        "Even more additional list item information appears on line three."
      );
    }
    return new ListItem(
142
      isThreeLine: _itemType == MaterialListType.threeLine,
143
      dense: _dense,
144
      leading: _showAvatars ? new CircleAvatar(child: new Text(item)) : null,
145
      title: new Text('This item represents $item.'),
146
      subtitle: secondary,
Ian Hickson's avatar
Ian Hickson committed
147
      trailing: _showIcons ? new Icon(Icons.info, color: Theme.of(context).disabledColor) : null
Hans Muller's avatar
Hans Muller committed
148 149 150
    );
  }

151
  @override
Hans Muller's avatar
Hans Muller committed
152
  Widget build(BuildContext context) {
153
    final String layoutText = _dense ? " \u2013 Dense" : "";
154 155 156 157 158
    String  itemTypeText;
    switch(_itemType) {
      case MaterialListType.oneLine:
      case MaterialListType.oneLineWithAvatar:
        itemTypeText = 'Single-line';
Hans Muller's avatar
Hans Muller committed
159
        break;
160 161
      case MaterialListType.twoLine:
        itemTypeText = 'Two-line';
Hans Muller's avatar
Hans Muller committed
162
        break;
163 164
      case MaterialListType.threeLine:
        itemTypeText = 'Three-line';
Hans Muller's avatar
Hans Muller committed
165 166
        break;
    }
Hans Muller's avatar
Hans Muller committed
167 168 169 170 171

    Iterable<Widget> listItems = items.map((String item) => buildListItem(context, item));
    if (_showDividers)
      listItems = ListItem.divideItems(context: context, items: listItems);

Hans Muller's avatar
Hans Muller committed
172 173
    return new Scaffold(
      key: scaffoldKey,
174
      appBar: new AppBar(
175
        title: new Text('Scrolling list\n$itemTypeText$layoutText'),
176
        actions: <Widget>[
Hans Muller's avatar
Hans Muller committed
177
          new IconButton(
Ian Hickson's avatar
Ian Hickson committed
178
            icon: new Icon(Icons.sort_by_alpha),
Hans Muller's avatar
Hans Muller committed
179 180 181 182 183 184 185 186 187
            tooltip: 'Sort',
            onPressed: () {
              setState(() {
                _reverseSort = !_reverseSort;
                items.sort((String a, String b) => _reverseSort ? b.compareTo(a) : a.compareTo(b));
              });
            }
          ),
          new IconButton(
Ian Hickson's avatar
Ian Hickson committed
188
            icon: new Icon(Icons.more_vert),
Hans Muller's avatar
Hans Muller committed
189 190 191 192 193
            tooltip: 'Show menu',
            onPressed: () { showConfigurationSheet(context); }
          )
        ]
      ),
194 195 196 197 198
      body: new Scrollbar(
        child: new MaterialList(
          type: _itemType,
          padding: new EdgeInsets.symmetric(vertical: _dense ? 4.0 : 8.0),
          children: listItems
199
        )
Hans Muller's avatar
Hans Muller committed
200 201 202 203
      )
    );
  }
}