pageable_list.dart 4.93 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:sky/animation.dart';
6
import 'package:sky/material.dart';
7
import 'package:sky/widgets.dart';
8 9 10 11 12 13 14

class CardModel {
  CardModel(this.value, this.size, this.color);
  int value;
  Size size;
  Color color;
  String get label => "Card $value";
Hixie's avatar
Hixie committed
15
  Key get key => new ObjectKey(this);
16 17
}

18
class PageableListApp extends App {
19 20

  static const TextStyle cardLabelStyle =
21
    const TextStyle(color: Colors.white, fontSize: 18.0, fontWeight: bold);
22 23 24

  List<CardModel> cardModels;
  Size pageSize = new Size(200.0, 200.0);
25
  ScrollDirection scrollDirection = ScrollDirection.horizontal;
26
  bool itemsWrap = false;
27 28 29 30 31 32 33 34 35

  void initState() {
    List<Size> cardSizes = [
      [100.0, 300.0], [300.0, 100.0], [200.0, 400.0], [400.0, 400.0], [300.0, 400.0]
    ]
    .map((args) => new Size(args[0], args[1]))
    .toList();

    cardModels = new List.generate(cardSizes.length, (i) {
36
      Color color = Color.lerp(Colors.red[300], Colors.blue[900], i / cardSizes.length);
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
      return new CardModel(i, cardSizes[i], color);
    });

    super.initState();
  }

  void updatePageSize(Size newSize) {
    setState(() {
      pageSize = newSize;
    });
  }

  Widget buildCard(CardModel cardModel) {
    Widget card = new Card(
      color: cardModel.color,
      child: new Container(
        width: cardModel.size.width,
        height: cardModel.size.height,
        padding: const EdgeDims.all(8.0),
        child: new Center(child: new Text(cardModel.label, style: cardLabelStyle))
      )
    );
59 60 61 62 63

    BoxConstraints constraints = (scrollDirection == ScrollDirection.vertical)
      ? new BoxConstraints.tightFor(height: pageSize.height)
      : new BoxConstraints.tightFor(width: pageSize.width);

64 65
    return new Container(
      key: cardModel.key,
66
      constraints: constraints,
67 68 69 70
      child: new Center(child: card)
    );
  }

71
  void switchScrollDirection() {
72 73 74 75 76
    setState(() {
      scrollDirection = (scrollDirection == ScrollDirection.vertical)
        ? ScrollDirection.horizontal
        : ScrollDirection.vertical;
    });
77 78 79 80 81 82
  }

  void toggleItemsWrap() {
    setState(() {
      itemsWrap = !itemsWrap;
    });
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  }

  bool _drawerShowing = false;
  AnimationStatus _drawerStatus = AnimationStatus.dismissed;

  void _handleOpenDrawer() {
    setState(() {
      _drawerShowing = true;
      _drawerStatus = AnimationStatus.forward;
    });
  }

  void _handleDrawerDismissed() {
    setState(() {
      _drawerStatus = AnimationStatus.dismissed;
    });
  }

  Drawer buildDrawer() {
    if (_drawerStatus == AnimationStatus.dismissed)
      return null;

    return new Drawer(
      level: 3,
      showing: _drawerShowing,
      onDismissed: _handleDrawerDismissed,
      children: [
        new DrawerHeader(child: new Text('Options')),
        new DrawerItem(
          icon: 'navigation/more_horiz',
          selected: scrollDirection == ScrollDirection.horizontal,
          child: new Text('Horizontal Layout'),
          onPressed: switchScrollDirection
        ),
        new DrawerItem(
          icon: 'navigation/more_vert',
          selected: scrollDirection == ScrollDirection.vertical,
          child: new Text('Vertical Layout'),
          onPressed: switchScrollDirection
122 123 124 125 126 127 128
        ),
        new DrawerItem(
          onPressed: toggleItemsWrap,
          child: new Row([
            new Flexible(child: new Text('Scrolling wraps around')),
            new Checkbox(value: itemsWrap)
          ])
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        )
      ]
    );

  }

  Widget buildToolBar() {
    return new ToolBar(
      left: new IconButton(icon: "navigation/menu", onPressed: _handleOpenDrawer),
      center: new Text('PageableList'),
      right: [
        new Text(scrollDirection == ScrollDirection.horizontal ? "horizontal" : "vertical")
      ]
    );
  }

  Widget buildBody() {
146 147
    Widget list = new PageableList<CardModel>(
      items: cardModels,
148
      itemsWrap: itemsWrap,
149
      itemBuilder: buildCard,
150 151 152 153
      scrollDirection: scrollDirection,
      itemExtent: (scrollDirection == ScrollDirection.vertical)
          ? pageSize.height
          : pageSize.width
154
    );
155 156 157 158 159 160 161 162
    return new SizeObserver(
      callback: updatePageSize,
      child: new Container(
        child: list,
        decoration: new BoxDecoration(backgroundColor: Theme.of(this).primarySwatch[50])
      )
    );
  }
163

164
  Widget build() {
165 166 167 168 169
    return new IconTheme(
      data: const IconThemeData(color: IconThemeColor.white),
      child: new Theme(
        data: new ThemeData(
          brightness: ThemeBrightness.light,
170 171
          primarySwatch: Colors.blue,
          accentColor: Colors.redAccent[200]
172
        ),
173 174
        child: new Title(
          title: 'PageableList',
175
          child: new Scaffold(
176 177 178
            drawer: buildDrawer(),
            toolbar: buildToolBar(),
            body: buildBody()
179 180 181 182 183 184 185 186
          )
        )
      )
    );
  }
}

void main() {
187
  runApp(new PageableListApp());
188
}