media_query.dart 2.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import 'package:flutter/material.dart';

void main() {
  runApp(
    new MaterialApp(
      title: "Media Query Example",
      routes: <String, RouteBuilder>{
        '/': (RouteArguments args) => new MediaQueryExample()
      }
    )
  );
}

class AdaptiveItem {
  AdaptiveItem(this.name);
  String name;

  Widget toListItem() {
    return new Row(
20
      children: <Widget>[
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
        new Container(
          width: 32.0,
          height: 32.0,
          margin: const EdgeDims.all(8.0),
          decoration: new BoxDecoration(
            backgroundColor: Colors.lightBlueAccent[100]
          )
        ),
        new Text(name)
      ]
    );
  }

  Widget toCard() {
    return new Card(
      child: new Column(
37
        children: <Widget>[
38 39 40 41 42 43 44 45 46 47
          new Flexible(
            child: new Container(
              decoration: new BoxDecoration(
                backgroundColor: Colors.lightBlueAccent[100]
              )
            )
          ),
          new Container(
            margin: const EdgeDims.only(left: 8.0),
            child: new Row(
48
              children: <Widget>[
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
                new Flexible(
                  child: new Text(name)
                ),
                new IconButton(
                  icon: "navigation/more_vert"
                )
              ]
            )
          )
        ]
      )
    );
  }
}

class MediaQueryExample extends StatelessComponent {
Adam Barth's avatar
Adam Barth committed
65
  static const double _maxTileWidth = 150.0;
66 67 68 69 70 71 72 73 74
  static const double _gridViewBreakpoint = 450.0;

  Widget _buildBody(BuildContext context) {
    List<AdaptiveItem> items = <AdaptiveItem>[];

    for (int i = 0; i < 30; i++)
      items.add(new AdaptiveItem("Item $i"));

    if (MediaQuery.of(context).size.width < _gridViewBreakpoint) {
75
      return new ScrollableList(
Adam Barth's avatar
Adam Barth committed
76
        itemExtent: 50.0,
77
        children: items.map((AdaptiveItem item) => item.toListItem())
78 79
      );
    } else {
80
      return new ScrollableGrid(
Adam Barth's avatar
Adam Barth committed
81
        delegate: new MaxTileWidthGridDelegate(maxTileWidth: _maxTileWidth),
82
        children: items.map((AdaptiveItem item) => item.toCard())
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      );
    }
  }

  Widget build(BuildContext context)  {
    return new Scaffold(
      toolBar: new ToolBar(
        center: new Text("Media Query Example")
      ),
      body: new Material(
        child: _buildBody(context)
      )
    );
  }
}