media_query.dart 2.75 KB
Newer Older
1 2 3 4 5 6
// 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.

import 'package:flutter/material.dart';

7
class AdaptedListItem extends StatelessWidget {
8 9 10 11
  AdaptedListItem({ Key key, this.name }) : super(key: key);

  final String name;

12
  @override
13 14 15 16 17 18
  Widget build(BuildContext context) {
    return new Row(
      children: <Widget>[
        new Container(
          width: 32.0,
          height: 32.0,
19
          margin: const EdgeInsets.all(8.0),
20 21 22 23 24 25 26 27 28 29
          decoration: new BoxDecoration(
            backgroundColor: Colors.lightBlueAccent[100]
          )
        ),
        new Text(name)
      ]
    );
  }
}

30
class AdaptedGridItem extends StatelessWidget {
31 32 33 34
  AdaptedGridItem({ Key key, this.name }) : super(key: key);

  final String name;

35
  @override
36 37 38 39
  Widget build(BuildContext context) {
    return new Card(
      child: new Column(
        children: <Widget>[
40
          new Expanded(
41 42 43 44 45 46 47
            child: new Container(
              decoration: new BoxDecoration(
                backgroundColor: Colors.lightBlueAccent[100]
              )
            )
          ),
          new Container(
48
            margin: const EdgeInsets.only(left: 8.0),
49 50
            child: new Row(
              children: <Widget>[
51
                new Expanded(
52 53 54
                  child: new Text(name)
                ),
                new IconButton(
Ian Hickson's avatar
Ian Hickson committed
55
                  icon: new Icon(Icons.more_vert),
56
                  onPressed: null
57 58 59 60 61 62 63 64 65 66 67 68 69 70
                )
              ]
            )
          )
        ]
      )
    );
  }
}

const double _kListItemExtent = 50.0;
const double _kMaxTileWidth = 150.0;
const double _kGridViewBreakpoint = 450.0;

71
class AdaptiveContainer extends StatelessWidget {
72 73 74 75
  AdaptiveContainer({ Key key, this.names }) : super(key: key);

  final List<String> names;

76
  @override
77 78
  Widget build(BuildContext context) {
    if (MediaQuery.of(context).size.width < _kGridViewBreakpoint) {
79
      return new ScrollView(
80
        itemExtent: _kListItemExtent,
81
        children: names.map((String name) => new AdaptedListItem(name: name)).toList(),
82 83
      );
    } else {
84 85 86
      return new ScrollGrid.extent(
        maxCrossAxisExtent: _kMaxTileWidth,
        children: names.map((String name) => new AdaptedGridItem(name: name)).toList(),
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      );
    }
  }
}

List<String> _initNames() {
  List<String> names = <String>[];
  for (int i = 0; i < 30; i++)
    names.add('Item $i');
  return names;
}

final List<String> _kNames = _initNames();

void main() {
  runApp(new MaterialApp(
    title: 'Media Query Example',
104 105 106 107 108 109
    home: new Scaffold(
      appBar: new AppBar(
        title: new Text('Media Query Example')
      ),
      body: new Material(child: new AdaptiveContainer(names: _kNames))
    )
110 111
  ));
}