pageable_list.dart 4.17 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 6
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
7 8 9 10 11 12 13

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
14
  Key get key => new ObjectKey(this);
15 16
}

17
class PageableListApp extends StatefulComponent {
18
  PageableListAppState createState() => new PageableListAppState();
19
}
20

21
class PageableListAppState extends State<PageableListApp> {
22 23
  void initState() {
    super.initState();
24 25 26 27 28 29
    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();

Hixie's avatar
Hixie committed
30
    cardModels = new List<CardModel>.generate(cardSizes.length, (int i) {
31
      Color color = Color.lerp(Colors.red[300], Colors.blue[900], i / cardSizes.length);
32 33 34 35
      return new CardModel(i, cardSizes[i], color);
    });
  }

36
  static const TextStyle cardLabelStyle =
37
    const TextStyle(color: Colors.white, fontSize: 18.0, fontWeight: FontWeight.bold);
38 39 40 41 42 43

  List<CardModel> cardModels;
  Size pageSize = new Size(200.0, 200.0);
  ScrollDirection scrollDirection = ScrollDirection.horizontal;
  bool itemsWrap = false;

44
  Widget buildCard(BuildContext context, CardModel cardModel, int index) {
45 46 47 48 49 50 51 52 53
    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))
      )
    );
54 55 56 57 58

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

59 60
    return new Container(
      key: cardModel.key,
61
      constraints: constraints,
62 63 64 65
      child: new Center(child: card)
    );
  }

66
  void switchScrollDirection() {
67 68 69 70 71
    setState(() {
      scrollDirection = (scrollDirection == ScrollDirection.vertical)
        ? ScrollDirection.horizontal
        : ScrollDirection.vertical;
    });
72 73 74 75 76 77
  }

  void toggleItemsWrap() {
    setState(() {
      itemsWrap = !itemsWrap;
    });
78 79
  }

80 81
  Widget _buildDrawer() {
    return new Drawer(
Hixie's avatar
Hixie committed
82
      child: new Block(<Widget>[
83 84 85 86 87 88 89 90 91 92 93 94
        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
95 96 97
        ),
        new DrawerItem(
          onPressed: toggleItemsWrap,
Hixie's avatar
Hixie committed
98
          child: new Row(<Widget>[
99 100 101
            new Flexible(child: new Text('Scrolling wraps around')),
            new Checkbox(value: itemsWrap)
          ])
102
        )
103
      ])
104 105 106
    );
  }

107
  Widget _buildToolBar() {
108 109
    return new ToolBar(
      center: new Text('PageableList'),
Hixie's avatar
Hixie committed
110
      right: <Widget>[
111 112 113 114 115
        new Text(scrollDirection == ScrollDirection.horizontal ? "horizontal" : "vertical")
      ]
    );
  }

116
  Widget _buildBody(BuildContext context) {
Hans Muller's avatar
Hans Muller committed
117
    return new PageableList<CardModel>(
118
      items: cardModels,
119
      itemsWrap: itemsWrap,
120
      itemBuilder: buildCard,
Hans Muller's avatar
Hans Muller committed
121
      scrollDirection: scrollDirection
122 123
    );
  }
124

125
  Widget build(BuildContext context) {
126 127
    return new IconTheme(
      data: const IconThemeData(color: IconThemeColor.white),
128
      child: new Scaffold(
129 130 131
        toolBar: _buildToolBar(),
        drawer: _buildDrawer(),
        body: _buildBody(context)
132 133 134 135 136 137
      )
    );
  }
}

void main() {
Adam Barth's avatar
Adam Barth committed
138
  runApp(new MaterialApp(
139 140 141 142 143 144
    title: 'PageableList',
    theme: new ThemeData(
      brightness: ThemeBrightness.light,
      primarySwatch: Colors.blue,
      accentColor: Colors.redAccent[200]
    ),
Hixie's avatar
Hixie committed
145
    routes: <String, RouteBuilder>{
Adam Barth's avatar
Adam Barth committed
146
      '/': (RouteArguments args) => new PageableListApp(),
147 148
    }
  ));
149
}