list_demo.dart 6.45 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 18
  static final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
  static final GlobalKey<ScrollableState> _scrollableKey = new GlobalKey<ScrollableState>();
Hans Muller's avatar
Hans Muller committed
19

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

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

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

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

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

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