blob: 39c996a25be9eeaf0b150b5de2437a127ce21be0 [file] [log] [blame]
Ian Hickson449f4a62019-11-27 15:04:02 -08001// Copyright 2014 The Flutter Authors. All rights reserved.
Adam Barth948ae152016-02-13 00:52:56 -08002// 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
Adam Barth95fc5ae2016-03-12 12:13:16 -08007class AdaptedListItem extends StatelessWidget {
Michael Goderbauer91bcf422022-03-29 12:53:08 -07008 const AdaptedListItem({ super.key, required this.name });
Adam Barth948ae152016-02-13 00:52:56 -08009
10 final String name;
11
Hixie797e27e2016-03-14 13:31:43 -070012 @override
Adam Barth948ae152016-02-13 00:52:56 -080013 Widget build(BuildContext context) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020014 return Row(
Adam Barth948ae152016-02-13 00:52:56 -080015 children: <Widget>[
Alexandre Ardhuind927c932018-09-12 08:29:29 +020016 Container(
Adam Barth948ae152016-02-13 00:52:56 -080017 width: 32.0,
18 height: 32.0,
Adam Barthe71bd772016-03-12 11:43:18 -080019 margin: const EdgeInsets.all(8.0),
Ian Hickson36052e62017-04-27 14:19:01 -070020 color: Colors.lightBlueAccent.shade100,
Adam Barth948ae152016-02-13 00:52:56 -080021 ),
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010022 Text(name),
23 ],
Adam Barth948ae152016-02-13 00:52:56 -080024 );
25 }
26}
27
Adam Barth95fc5ae2016-03-12 12:13:16 -080028class AdaptedGridItem extends StatelessWidget {
Michael Goderbauer91bcf422022-03-29 12:53:08 -070029 const AdaptedGridItem({ super.key, required this.name });
Adam Barth948ae152016-02-13 00:52:56 -080030
31 final String name;
32
Hixie797e27e2016-03-14 13:31:43 -070033 @override
Adam Barth948ae152016-02-13 00:52:56 -080034 Widget build(BuildContext context) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020035 return Card(
36 child: Column(
Adam Barth948ae152016-02-13 00:52:56 -080037 children: <Widget>[
Alexandre Ardhuind927c932018-09-12 08:29:29 +020038 Expanded(
39 child: Container(
Ian Hickson36052e62017-04-27 14:19:01 -070040 color: Colors.lightBlueAccent.shade100,
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010041 ),
Adam Barth948ae152016-02-13 00:52:56 -080042 ),
Alexandre Ardhuind927c932018-09-12 08:29:29 +020043 Container(
Adam Barthe71bd772016-03-12 11:43:18 -080044 margin: const EdgeInsets.only(left: 8.0),
Alexandre Ardhuind927c932018-09-12 08:29:29 +020045 child: Row(
Adam Barth948ae152016-02-13 00:52:56 -080046 children: <Widget>[
Alexandre Ardhuind927c932018-09-12 08:29:29 +020047 Expanded(
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010048 child: Text(name),
Adam Barth948ae152016-02-13 00:52:56 -080049 ),
Ian Hickson3eb87832017-04-07 12:24:32 -070050 const IconButton(
Alexandre Ardhuineda03e22018-08-02 12:02:32 +020051 icon: Icon(Icons.more_vert),
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010052 onPressed: null,
53 ),
54 ],
55 ),
56 ),
57 ],
58 ),
Adam Barth948ae152016-02-13 00:52:56 -080059 );
60 }
61}
62
63const double _kListItemExtent = 50.0;
64const double _kMaxTileWidth = 150.0;
65const double _kGridViewBreakpoint = 450.0;
66
Adam Barth95fc5ae2016-03-12 12:13:16 -080067class AdaptiveContainer extends StatelessWidget {
Michael Goderbauer91bcf422022-03-29 12:53:08 -070068 const AdaptiveContainer({ super.key, required this.names });
Adam Barth948ae152016-02-13 00:52:56 -080069
70 final List<String> names;
71
Hixie797e27e2016-03-14 13:31:43 -070072 @override
Adam Barth948ae152016-02-13 00:52:56 -080073 Widget build(BuildContext context) {
Greg Spencer55289322020-10-28 07:56:41 -070074 if (MediaQuery.of(context).size.width < _kGridViewBreakpoint) {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020075 return ListView(
Adam Barth948ae152016-02-13 00:52:56 -080076 itemExtent: _kListItemExtent,
Alexandre Ardhuinf62afdc2018-10-01 21:29:08 +020077 children: names.map<Widget>((String name) => AdaptedListItem(name: name)).toList(),
Adam Barth948ae152016-02-13 00:52:56 -080078 );
79 } else {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020080 return GridView.extent(
Adam Barthfe01c712017-02-01 11:01:02 -080081 maxCrossAxisExtent: _kMaxTileWidth,
Alexandre Ardhuinf62afdc2018-10-01 21:29:08 +020082 children: names.map<Widget>((String name) => AdaptedGridItem(name: name)).toList(),
Adam Barth948ae152016-02-13 00:52:56 -080083 );
84 }
85 }
86}
87
Alexandre Ardhuindf4bf452019-09-17 16:23:44 +020088List<String> _initNames() => List<String>.generate(30, (int i) => 'Item $i');
Adam Barth948ae152016-02-13 00:52:56 -080089
90final List<String> _kNames = _initNames();
91
92void main() {
Alexandre Ardhuind927c932018-09-12 08:29:29 +020093 runApp(MaterialApp(
Adam Barth948ae152016-02-13 00:52:56 -080094 title: 'Media Query Example',
Alexandre Ardhuind927c932018-09-12 08:29:29 +020095 home: Scaffold(
96 appBar: AppBar(
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010097 title: const Text('Media Query Example'),
Ian Hickson1b9476c2016-04-20 09:33:28 -070098 ),
Alexandre Ardhuin387f8852019-03-01 08:17:55 +010099 body: Material(child: AdaptiveContainer(names: _kNames)),
100 ),
Adam Barth948ae152016-02-13 00:52:56 -0800101 ));
102}