pageable_list.dart 4 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:flutter/material.dart';
6 7 8 9 10 11 12

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

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

20
class PageableListAppState extends State<PageableListApp> {
21 22
  void initState() {
    super.initState();
23 24 25 26 27 28
    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
29
    cardModels = new List<CardModel>.generate(cardSizes.length, (int i) {
30
      Color color = Color.lerp(Colors.red[300], Colors.blue[900], i / cardSizes.length);
31 32 33 34
      return new CardModel(i, cardSizes[i], color);
    });
  }

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

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

43
  Widget buildCard(CardModel cardModel) {
44 45 46 47 48 49 50 51 52
    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))
      )
    );
53

54
    BoxConstraints constraints = (scrollDirection == Axis.vertical)
55 56 57
      ? new BoxConstraints.tightFor(height: pageSize.height)
      : new BoxConstraints.tightFor(width: pageSize.width);

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

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

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

79 80
  Widget _buildDrawer() {
    return new Drawer(
81
      child: new Block(children: <Widget>[
82 83
        new DrawerHeader(child: new Text('Options')),
        new DrawerItem(
84
          icon: Icons.more_horiz,
85
          selected: scrollDirection == Axis.horizontal,
86 87 88 89
          child: new Text('Horizontal Layout'),
          onPressed: switchScrollDirection
        ),
        new DrawerItem(
90
          icon: Icons.more_vert,
91
          selected: scrollDirection == Axis.vertical,
92 93
          child: new Text('Vertical Layout'),
          onPressed: switchScrollDirection
94 95 96
        ),
        new DrawerItem(
          onPressed: toggleItemsWrap,
97 98 99 100 101 102
          child: new Row(
            children: <Widget>[
              new Flexible(child: new Text('Scrolling wraps around')),
              new Checkbox(value: itemsWrap)
            ]
          )
103
        )
104
      ])
105 106 107
    );
  }

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

117
  Widget _buildBody(BuildContext context) {
118 119
    return new PageableList(
      children: cardModels.map(buildCard),
120
      itemsWrap: itemsWrap,
Hans Muller's avatar
Hans Muller committed
121
      scrollDirection: scrollDirection
122 123
    );
  }
124

125
  Widget build(BuildContext context) {
126
    return new IconTheme(
Adam Barth's avatar
Adam Barth committed
127
      data: const IconThemeData(color: Colors.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
}