grid_list_demo.dart 7.44 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 63 64
          body: new Hero(
            tag: photoHeroTag,
            child: new Image.asset(photo.assetName, fit: ImageFit.cover)
65 66 67 68 69 70
          )
        );
      }
    ));
  }

71
  @override
72 73 74
  Widget build(BuildContext context) {
    final Widget image = new GestureDetector(
      onTap: () { showPhoto(context); },
75 76 77
      child: new Hero(
        key: new Key(photo.assetName),
        tag: photoHeroTag,
78
        child: new Image.asset(photo.assetName, fit: ImageFit.cover)
79 80 81
      )
    );

82 83
    IconData icon = photo.isFavorite ? Icons.star : Icons.star_border;

84 85 86 87 88
    switch(tileStyle) {
      case GridDemoTileStyle.imageOnly:
        return image;

      case GridDemoTileStyle.oneLine:
Hans Muller's avatar
Hans Muller committed
89
        return new GridTile(
90 91 92 93 94 95
          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
96
                icon,
97 98 99
                color: Colors.white
              )
            )
Hans Muller's avatar
Hans Muller committed
100 101
          ),
          child: image
102 103 104
        );

      case GridDemoTileStyle.twoLine:
Hans Muller's avatar
Hans Muller committed
105
        return new GridTile(
106 107 108 109 110 111 112
          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
113
                icon,
114 115
                color: Colors.white
              )
116
            )
Hans Muller's avatar
Hans Muller committed
117 118
          ),
          child: image
119
        );
Hans Muller's avatar
Hans Muller committed
120
    }
pq's avatar
pq committed
121
    assert(tileStyle != null);
pq's avatar
pq committed
122
    return null;
123 124 125
  }
}

126
class GridListDemo extends StatefulWidget {
Hans Muller's avatar
Hans Muller committed
127 128
  GridListDemo({ Key key }) : super(key: key);

129 130
  static const String routeName = '/grid-list';

131
  @override
132 133 134 135 136 137
  GridListDemoState createState() => new GridListDemoState();
}

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

138 139 140 141 142 143 144 145 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
  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'
    ),
  ];

201 202 203
  void changeTileStyle(GridDemoTileStyle value) {
    setState(() {
      tileStyle = value;
204 205 206 207 208 209
    });
  }

  // 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.
210
  @override
211 212 213
  Widget build(BuildContext context) {
    final Orientation orientation = MediaQuery.of(context).orientation;
    return new Scaffold(
214
      appBar: new AppBar(
215
        title: new Text('Grid list'),
216
        actions: <Widget>[
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
          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')
              )
            ]
233 234 235
          )
        ]
      ),
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
      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,
251
                  onBannerTap: (Photo photo) {
252 253 254 255 256 257 258 259 260
                    setState(() {
                      photo.isFavorite = !photo.isFavorite;
                    });
                  }
                );
              })
            )
          )
        ]
261 262 263 264
      )
    );
  }
}