list_demo.dart 6.18 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 18 19 20 21
// 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';

enum ListDemoItemSize {
  oneLine,
  twoLine,
  threeLine
}

class ListDemo extends StatefulComponent {
  ListDemo({ Key key }) : super(key: key);

  ListDemoState createState() => new ListDemoState();
}

class ListDemoState extends State<ListDemo> {
  final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();

22
  PersistentBottomSheetController<Null> _bottomSheet;
Hans Muller's avatar
Hans Muller committed
23
  ListDemoItemSize _itemSize = ListDemoItemSize.threeLine;
Hans Muller's avatar
Hans Muller committed
24 25 26 27
  bool _dense = false;
  bool _showAvatars = true;
  bool _showIcons = false;
  bool _showDividers = false;
Hans Muller's avatar
Hans Muller committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  bool _reverseSort = false;
  List<String> items = <String>[
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'
  ];

  void changeItemSize(ListDemoItemSize size) {
    setState(() {
      _itemSize = size;
    });
    _bottomSheet?.setState(() { });
  }

  void showConfigurationSheet(BuildContext appContext) {
    _bottomSheet = scaffoldKey.currentState.showBottomSheet((BuildContext bottomSheetContext) {
      return new Container(
        decoration: new BoxDecoration(
          border: new Border(top: new BorderSide(color: Colors.black26, width: 1.0))
        ),
        child: new Column(
          justifyContent: FlexJustifyContent.collapse,
          alignItems: FlexAlignItems.stretch,
          children: <Widget>[
            new ListItem(
51
              dense: true,
Hans Muller's avatar
Hans Muller committed
52 53 54 55 56 57 58 59
              primary: new Text('One-line'),
              right: new Radio<ListDemoItemSize>(
                value: ListDemoItemSize.oneLine,
                groupValue: _itemSize,
                onChanged: changeItemSize
              )
            ),
            new ListItem(
60
              dense: true,
Hans Muller's avatar
Hans Muller committed
61 62 63 64 65 66 67 68
              primary: new Text('Two-line'),
              right: new Radio<ListDemoItemSize>(
                value: ListDemoItemSize.twoLine,
                groupValue: _itemSize,
                onChanged: changeItemSize
              )
            ),
            new ListItem(
69
              dense: true,
Hans Muller's avatar
Hans Muller committed
70 71 72 73 74 75 76 77
              primary: new Text('Three-line'),
              right: new Radio<ListDemoItemSize>(
                value: ListDemoItemSize.threeLine,
                groupValue: _itemSize,
                onChanged: changeItemSize
              )
            ),
            new ListItem(
78
              dense: true,
Hans Muller's avatar
Hans Muller committed
79 80
              primary: new Text('Show Avatar'),
              right: new Checkbox(
Hans Muller's avatar
Hans Muller committed
81
                value: _showAvatars,
Hans Muller's avatar
Hans Muller committed
82 83
                onChanged: (bool value) {
                  setState(() {
Hans Muller's avatar
Hans Muller committed
84
                    _showAvatars = value;
Hans Muller's avatar
Hans Muller committed
85 86 87 88 89 90
                  });
                  _bottomSheet?.setState(() { });
                }
              )
            ),
            new ListItem(
91
              dense: true,
Hans Muller's avatar
Hans Muller committed
92 93
              primary: new Text('Show Icon'),
              right: new Checkbox(
Hans Muller's avatar
Hans Muller committed
94
                value: _showIcons,
Hans Muller's avatar
Hans Muller committed
95 96
                onChanged: (bool value) {
                  setState(() {
Hans Muller's avatar
Hans Muller committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110
                    _showIcons = value;
                  });
                  _bottomSheet?.setState(() { });
                }
              )
            ),
            new ListItem(
              dense: true,
              primary: new Text('Show Dividers'),
              right: new Checkbox(
                value: _showDividers,
                onChanged: (bool value) {
                  setState(() {
                    _showDividers = value;
Hans Muller's avatar
Hans Muller committed
111 112 113 114 115 116
                  });
                  _bottomSheet?.setState(() { });
                }
              )
            ),
            new ListItem(
117
              dense: true,
Hans Muller's avatar
Hans Muller committed
118 119
              primary: new Text('Dense Layout'),
              right: new Checkbox(
120
                value: _dense,
Hans Muller's avatar
Hans Muller committed
121 122
                onChanged: (bool value) {
                  setState(() {
123
                    _dense = value;
Hans Muller's avatar
Hans Muller committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
                  });
                  _bottomSheet?.setState(() { });
                }
              )
            )
          ]
        )
      );
    });
  }

  Widget buildListItem(BuildContext context, String item) {
    Widget secondary;
    if (_itemSize == ListDemoItemSize.twoLine) {
      secondary = new Text(
        "Additional item information."
      );
    } else if (_itemSize == ListDemoItemSize.threeLine) {
      secondary = new Text(
        "Even more additional list item information appears on line three."
      );
    }
    return new ListItem(
      isThreeLine: _itemSize == ListDemoItemSize.threeLine,
148
      dense: _dense,
Hans Muller's avatar
Hans Muller committed
149
      left: _showAvatars ? new CircleAvatar(child: new Text(item)) : null,
Hans Muller's avatar
Hans Muller committed
150 151
      primary: new Text('This item represents $item'),
      secondary: secondary,
152
      right: _showIcons ? new Icon(icon: Icons.info, color: Theme.of(context).disabledColor) : null
Hans Muller's avatar
Hans Muller committed
153 154 155 156
    );
  }

  Widget build(BuildContext context) {
157
    final String layoutText = _dense ? " \u2013 Dense" : "";
Hans Muller's avatar
Hans Muller committed
158 159 160 161 162 163 164 165 166 167 168 169
    String  itemSizeText;
    switch(_itemSize) {
      case ListDemoItemSize.oneLine:
        itemSizeText = 'Single-Line';
        break;
      case ListDemoItemSize.twoLine:
        itemSizeText = 'Two-Line';
        break;
      case ListDemoItemSize.threeLine:
        itemSizeText = 'Three-Line';
        break;
    }
Hans Muller's avatar
Hans Muller committed
170 171 172 173 174

    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
175 176 177 178 179 180
    return new Scaffold(
      key: scaffoldKey,
      toolBar: new ToolBar(
        center: new Text('Scrolling List\n$itemSizeText$layoutText'),
        right: <Widget>[
          new IconButton(
181
            icon: Icons.sort_by_alpha,
Hans Muller's avatar
Hans Muller committed
182 183 184 185 186 187 188 189 190
            tooltip: 'Sort',
            onPressed: () {
              setState(() {
                _reverseSort = !_reverseSort;
                items.sort((String a, String b) => _reverseSort ? b.compareTo(a) : a.compareTo(b));
              });
            }
          ),
          new IconButton(
191
            icon: Icons.more_vert,
Hans Muller's avatar
Hans Muller committed
192 193 194 195 196
            tooltip: 'Show menu',
            onPressed: () { showConfigurationSheet(context); }
          )
        ]
      ),
Hans Muller's avatar
Hans Muller committed
197
      body: new Block(
198
        padding: new EdgeDims.all(_dense ? 4.0 : 8.0),
Hans Muller's avatar
Hans Muller committed
199
        children: listItems.toList()
Hans Muller's avatar
Hans Muller committed
200 201 202 203
      )
    );
  }
}