page_view.dart 4.02 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/material.dart';
7 8 9 10 11 12

class CardModel {
  CardModel(this.value, this.size, this.color);
  int value;
  Size size;
  Color color;
13
  String get label => 'Card $value';
14
  Key get key => ObjectKey(this);
15 16
}

17
class PageViewApp extends StatefulWidget {
18
  @override
19
  PageViewAppState createState() => PageViewAppState();
20
}
21

22
class PageViewAppState extends State<PageViewApp> {
23
  @override
24 25
  void initState() {
    super.initState();
26 27 28 29 30 31
    const List<Size> cardSizes = <Size>[
      Size(100.0, 300.0),
      Size(300.0, 100.0),
      Size(200.0, 400.0),
      Size(400.0, 400.0),
      Size(300.0, 400.0),
32
    ];
33

34
    cardModels = List<CardModel>.generate(cardSizes.length, (int i) {
35
      final Color color = Color.lerp(Colors.red.shade300, Colors.blue.shade900, i / cardSizes.length);
36
      return CardModel(i, cardSizes[i], color);
37 38 39
    });
  }

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

  List<CardModel> cardModels;
44
  Size pageSize = const Size(200.0, 200.0);
45
  Axis scrollDirection = Axis.horizontal;
46 47
  bool itemsWrap = false;

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

59
    final BoxConstraints constraints = (scrollDirection == Axis.vertical)
60 61
      ? BoxConstraints.tightFor(height: pageSize.height)
      : BoxConstraints.tightFor(width: pageSize.width);
62

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

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

  void toggleItemsWrap() {
    setState(() {
      itemsWrap = !itemsWrap;
    });
82 83
  }

84
  Widget _buildDrawer() {
85 86
    return Drawer(
      child: ListView(
87
        children: <Widget>[
88
          const DrawerHeader(child: Center(child: Text('Options'))),
89
          ListTile(
90
            leading: const Icon(Icons.more_horiz),
91
            selected: scrollDirection == Axis.horizontal,
92
            trailing: const Text('Horizontal Layout'),
93
            onTap: switchScrollDirection,
94
          ),
95
          ListTile(
96
            leading: const Icon(Icons.more_vert),
97
            selected: scrollDirection == Axis.vertical,
98
            trailing: const Text('Vertical Layout'),
99
            onTap: switchScrollDirection,
100
          ),
101
          ListTile(
102
            onTap: toggleItemsWrap,
103
            title: const Text('Scrolling wraps around'),
104
            // TODO(abarth): Actually make this checkbox change this value.
105
            trailing: Checkbox(value: itemsWrap, onChanged: null),
106 107 108
          ),
        ],
      ),
109 110 111
    );
  }

112
  AppBar _buildAppBar() {
113
    return AppBar(
114
      title: const Text('PageView'),
115
      actions: <Widget>[
116
        Text(scrollDirection == Axis.horizontal ? 'horizontal' : 'vertical'),
117
      ],
118 119 120
    );
  }

121
  Widget _buildBody(BuildContext context) {
122
    return PageView(
123
      children: cardModels.map<Widget>(buildCard).toList(),
Adam Barth's avatar
Adam Barth committed
124
      // TODO(abarth): itemsWrap: itemsWrap,
125
      scrollDirection: scrollDirection,
126 127
    );
  }
128

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

142
void main() {
143
  runApp(MaterialApp(
144
    title: 'PageView',
145
    theme: ThemeData(
146
      brightness: Brightness.light,
147
      primarySwatch: Colors.blue,
148
      accentColor: Colors.redAccent,
149
    ),
150
    home: PageViewApp(),
151
  ));
152
}