pageable_list.dart 3.98 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 StatefulWidget {
17
  @override
18
  PageableListAppState createState() => new PageableListAppState();
19
}
20

21
class PageableListAppState extends State<PageableListApp> {
22
  @override
23 24
  void initState() {
    super.initState();
25 26 27
    List<Size> cardSizes = [
      [100.0, 300.0], [300.0, 100.0], [200.0, 400.0], [400.0, 400.0], [300.0, 400.0]
    ]
28
    .map((List<double> args) => new Size(args[0], args[1]))
29 30
    .toList();

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

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

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

45
  Widget buildCard(CardModel cardModel) {
46 47 48 49 50
    Widget card = new Card(
      color: cardModel.color,
      child: new Container(
        width: cardModel.size.width,
        height: cardModel.size.height,
51
        padding: const EdgeInsets.all(8.0),
52 53 54
        child: new Center(child: new Text(cardModel.label, style: cardLabelStyle))
      )
    );
55

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

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

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

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

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

110 111 112 113
  Widget _buildAppBar() {
    return new AppBar(
      title: new Text('PageableList'),
      actions: <Widget>[
114
        new Text(scrollDirection == Axis.horizontal ? "horizontal" : "vertical")
115 116 117 118
      ]
    );
  }

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

127
  @override
128
  Widget build(BuildContext context) {
129
    return new IconTheme(
Adam Barth's avatar
Adam Barth committed
130
      data: const IconThemeData(color: Colors.white),
131
      child: new Scaffold(
132
        appBar: _buildAppBar(),
133 134
        drawer: _buildDrawer(),
        body: _buildBody(context)
135 136 137 138 139 140
      )
    );
  }
}

void main() {
Adam Barth's avatar
Adam Barth committed
141
  runApp(new MaterialApp(
142 143 144 145 146 147
    title: 'PageableList',
    theme: new ThemeData(
      brightness: ThemeBrightness.light,
      primarySwatch: Colors.blue,
      accentColor: Colors.redAccent[200]
    ),
148
    home: new PageableListApp()
149
  ));
150
}