page_view.dart 4.46 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 6 7
import 'dart:io';

import 'package:flutter/foundation.dart';
8
import 'package:flutter/material.dart';
9 10 11 12 13 14

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

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

24
class PageViewAppState extends State<PageViewApp> {
25
  @override
26 27
  void initState() {
    super.initState();
28 29 30 31 32 33
    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),
34
    ];
35

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

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

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

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

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

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

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

  void toggleItemsWrap() {
    setState(() {
      itemsWrap = !itemsWrap;
    });
84 85
  }

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

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

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

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

144 145 146 147 148
// Sets a platform override for desktop to avoid exceptions. See
// https://flutter.dev/desktop#target-platform-override for more info.
// TODO(gspencergoog): Remove once TargetPlatform includes all desktop platforms.
void _enablePlatformOverrideForDesktop() {
  if (!kIsWeb && (Platform.isWindows || Platform.isLinux)) {
149 150
    debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
  }
151 152 153 154
}

void main() {
  _enablePlatformOverrideForDesktop();
155
  runApp(MaterialApp(
156
    title: 'PageView',
157
    theme: ThemeData(
158
      brightness: Brightness.light,
159
      primarySwatch: Colors.blue,
160
      accentColor: Colors.redAccent,
161
    ),
162
    home: PageViewApp(),
163
  ));
164
}