grid_list_demo.dart 7.6 KB
Newer Older
1 2 3 4
// Copyright 2016 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 'dart:collection';
6 7 8 9 10 11 12 13 14 15 16

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

enum GridDemoTileStyle {
  imageOnly,
  oneLine,
  twoLine
}

class Photo {
17
  Photo({ this.assetName, this.title, this.caption, this.isFavorite: false });
18 19

  final String assetName;
20 21
  final String title;
  final String caption;
22

23
  bool isFavorite;
24

25 26
  bool get isValid => assetName != null && title != null && caption != null && isFavorite != null;
}
27 28

const String photoHeroTag = 'Photo';
29

30
typedef void BannerTapCallback(Photo photo);
31

32
class GridDemoPhotoItem extends StatelessWidget {
33 34 35 36
  GridDemoPhotoItem({
    Key key,
    this.photo,
    this.tileStyle,
37
    this.onBannerTap
38
  }) : super(key: key) {
39 40
    assert(photo != null && photo.isValid);
    assert(tileStyle != null);
41
    assert(onBannerTap != null);
42 43 44
  }

  final Photo photo;
pq's avatar
pq committed
45
  final GridDemoTileStyle tileStyle;
46
  final BannerTapCallback onBannerTap; // User taps on the photo's header or footer.
47 48

  void showPhoto(BuildContext context) {
49 50 51 52
    Key photoKey = new Key(photo.assetName);
    Set<Key> mostValuableKeys = new HashSet<Key>();
    mostValuableKeys.add(photoKey);

53
    Navigator.push(context, new MaterialPageRoute<Null>(
54 55 56
      settings: new RouteSettings(
        mostValuableKeys: mostValuableKeys
      ),
57 58
      builder: (BuildContext context) {
        return new Scaffold(
59 60
          appBar: new AppBar(
            title: new Text(photo.title)
61 62
          ),
          body: new Material(
63 64
            child: new Hero(
              tag: photoHeroTag,
65 66
              child: new Image(
                image: new AssetImage(photo.assetName),
67 68
                fit: ImageFit.cover
              )
69 70 71 72 73 74 75
            )
          )
        );
      }
    ));
  }

76
  @override
77 78 79
  Widget build(BuildContext context) {
    final Widget image = new GestureDetector(
      onTap: () { showPhoto(context); },
80 81 82
      child: new Hero(
        key: new Key(photo.assetName),
        tag: photoHeroTag,
83 84
        child: new Image(
          image: new AssetImage(photo.assetName),
85 86
          fit: ImageFit.cover
        )
87 88 89
      )
    );

90 91
    IconData icon = photo.isFavorite ? Icons.star : Icons.star_border;

92 93 94 95 96
    switch(tileStyle) {
      case GridDemoTileStyle.imageOnly:
        return image;

      case GridDemoTileStyle.oneLine:
Hans Muller's avatar
Hans Muller committed
97
        return new GridTile(
98 99 100 101 102 103
          header: new GestureDetector(
            onTap: () { onBannerTap(photo); },
            child: new GridTileBar(
              title: new Text(photo.title),
              backgroundColor: Colors.black45,
              leading: new Icon(
Ian Hickson's avatar
Ian Hickson committed
104
                icon,
105 106 107
                color: Colors.white
              )
            )
Hans Muller's avatar
Hans Muller committed
108 109
          ),
          child: image
110 111 112
        );

      case GridDemoTileStyle.twoLine:
Hans Muller's avatar
Hans Muller committed
113
        return new GridTile(
114 115 116 117 118 119 120
          footer: new GestureDetector(
            onTap: () { onBannerTap(photo); },
            child: new GridTileBar(
              backgroundColor: Colors.black45,
              title: new Text(photo.title),
              subtitle: new Text(photo.caption),
              trailing: new Icon(
Ian Hickson's avatar
Ian Hickson committed
121
                icon,
122 123
                color: Colors.white
              )
124
            )
Hans Muller's avatar
Hans Muller committed
125 126
          ),
          child: image
127
        );
Hans Muller's avatar
Hans Muller committed
128
    }
pq's avatar
pq committed
129
    assert(tileStyle != null);
pq's avatar
pq committed
130
    return null;
131 132 133
  }
}

134
class GridListDemo extends StatefulWidget {
Hans Muller's avatar
Hans Muller committed
135 136
  GridListDemo({ Key key }) : super(key: key);

137 138
  static const String routeName = '/grid-list';

139
  @override
140 141 142 143 144 145
  GridListDemoState createState() => new GridListDemoState();
}

class GridListDemoState extends State<GridListDemo> {
  GridDemoTileStyle tileStyle = GridDemoTileStyle.twoLine;

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  List<Photo> photos = <Photo>[
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_0.jpg',
      title: 'Philippines',
      caption: 'Batad rice terraces'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_1.jpg',
      title: 'Italy',
      caption: 'Ceresole Reale'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_2.jpg',
      title: 'Somewhere',
      caption: 'Beautiful mountains'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_3.jpg',
      title: 'A place',
      caption: 'Beautiful hills'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_4.jpg',
      title: 'New Zealand',
      caption: 'View from the van'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_5.jpg',
      title: 'Autumn',
      caption: 'The golden season'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_6.jpg',
      title: 'Germany',
      caption: 'Englischer Garten'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_7.jpg',
      title: 'A country',
      caption: 'Grass fields'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_8.jpg',
      title: 'Mountain country',
      caption: 'River forest'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_9.jpg',
      title: 'Alpine place',
      caption: 'Green hills'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_10.jpg',
      title: 'Desert land',
      caption: 'Blue skies'
    ),
    new Photo(
      assetName: 'packages/flutter_gallery_assets/landscape_11.jpg',
      title: 'Narnia',
      caption: 'Rocks and rivers'
    ),
  ];

209 210 211
  void changeTileStyle(GridDemoTileStyle value) {
    setState(() {
      tileStyle = value;
212 213 214 215 216 217
    });
  }

  // When the ScrollableGrid first appears we want the last row to only be
  // partially visible, to help the user recognize that the grid is scrollable.
  // The GridListDemoGridDelegate's tileHeightFactor is used for this.
218
  @override
219 220 221
  Widget build(BuildContext context) {
    final Orientation orientation = MediaQuery.of(context).orientation;
    return new Scaffold(
222
      appBar: new AppBar(
223
        title: new Text('Grid list'),
224
        actions: <Widget>[
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
          new PopupMenuButton<GridDemoTileStyle>(
            onSelected: changeTileStyle,
            itemBuilder: (BuildContext context) => <PopupMenuItem<GridDemoTileStyle>>[
              new PopupMenuItem<GridDemoTileStyle>(
                value: GridDemoTileStyle.imageOnly,
                child: new Text('Image only')
              ),
              new PopupMenuItem<GridDemoTileStyle>(
                value: GridDemoTileStyle.oneLine,
                child: new Text('One line')
              ),
              new PopupMenuItem<GridDemoTileStyle>(
                value: GridDemoTileStyle.twoLine,
                child: new Text('Two line')
              )
            ]
241 242 243
          )
        ]
      ),
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
      body: new Column(
        children: <Widget>[
          new Flexible(
            child: new ScrollableGrid(
              delegate: new FixedColumnCountGridDelegate(
                columnCount: (orientation == Orientation.portrait) ? 2 : 3,
                rowSpacing: 4.0,
                columnSpacing: 4.0,
                padding: const EdgeInsets.all(4.0),
                tileAspectRatio: (orientation == Orientation.portrait) ? 1.0 : 1.3
              ),
              children: photos.map((Photo photo) {
                return new GridDemoPhotoItem(
                  photo: photo,
                  tileStyle: tileStyle,
259
                  onBannerTap: (Photo photo) {
260 261 262 263 264 265 266 267 268
                    setState(() {
                      photo.isFavorite = !photo.isFavorite;
                    });
                  }
                );
              })
            )
          )
        ]
269 270 271 272
      )
    );
  }
}