pageable_list.dart 4.06 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

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

44
  Widget buildCard(CardModel cardModel) {
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
    BoxConstraints constraints = (scrollDirection == Axis.vertical)
56 57 58
      ? 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
    setState(() {
68 69 70
      scrollDirection = (scrollDirection == Axis.vertical)
        ? Axis.horizontal
        : Axis.vertical;
71
    });
72 73 74 75 76 77
  }

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

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

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

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

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

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