• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6
7class AdaptedListItem extends StatelessWidget {
8  const AdaptedListItem({ Key key, this.name }) : super(key: key);
9
10  final String name;
11
12  @override
13  Widget build(BuildContext context) {
14    return Row(
15      children: <Widget>[
16        Container(
17          width: 32.0,
18          height: 32.0,
19          margin: const EdgeInsets.all(8.0),
20          color: Colors.lightBlueAccent.shade100,
21        ),
22        Text(name),
23      ],
24    );
25  }
26}
27
28class AdaptedGridItem extends StatelessWidget {
29  const AdaptedGridItem({ Key key, this.name }) : super(key: key);
30
31  final String name;
32
33  @override
34  Widget build(BuildContext context) {
35    return Card(
36      child: Column(
37        children: <Widget>[
38          Expanded(
39            child: Container(
40              color: Colors.lightBlueAccent.shade100,
41            ),
42          ),
43          Container(
44            margin: const EdgeInsets.only(left: 8.0),
45            child: Row(
46              children: <Widget>[
47                Expanded(
48                  child: Text(name),
49                ),
50                const IconButton(
51                  icon: Icon(Icons.more_vert),
52                  onPressed: null,
53                ),
54              ],
55            ),
56          ),
57        ],
58      ),
59    );
60  }
61}
62
63const double _kListItemExtent = 50.0;
64const double _kMaxTileWidth = 150.0;
65const double _kGridViewBreakpoint = 450.0;
66
67class AdaptiveContainer extends StatelessWidget {
68  const AdaptiveContainer({ Key key, this.names }) : super(key: key);
69
70  final List<String> names;
71
72  @override
73  Widget build(BuildContext context) {
74    if (MediaQuery.of(context).size.width < _kGridViewBreakpoint) {
75      return ListView(
76        itemExtent: _kListItemExtent,
77        children: names.map<Widget>((String name) => AdaptedListItem(name: name)).toList(),
78      );
79    } else {
80      return GridView.extent(
81        maxCrossAxisExtent: _kMaxTileWidth,
82        children: names.map<Widget>((String name) => AdaptedGridItem(name: name)).toList(),
83      );
84    }
85  }
86}
87
88List<String> _initNames() {
89  final List<String> names = <String>[];
90  for (int i = 0; i < 30; i++)
91    names.add('Item $i');
92  return names;
93}
94
95final List<String> _kNames = _initNames();
96
97void main() {
98  runApp(MaterialApp(
99    title: 'Media Query Example',
100    home: Scaffold(
101      appBar: AppBar(
102        title: const Text('Media Query Example'),
103      ),
104      body: Material(child: AdaptiveContainer(names: _kNames)),
105    ),
106  ));
107}
108