Skip to content

Commit 2a05bf5

Browse files
authored
Layout example app for gridview (#2308)
1 parent b22a60d commit 2a05bf5

38 files changed

+95
-163
lines changed

examples/layout/grid/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../README.md

examples/layout/grid/lib/main.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;
3+
4+
void main() {
5+
debugPaintSizeEnabled = false; // Set to true for visual layout
6+
runApp(MyApp());
7+
}
8+
9+
class MyApp extends StatelessWidget {
10+
@override
11+
Widget build(BuildContext context) {
12+
return MaterialApp(
13+
title: 'Flutter layout demo',
14+
home: Scaffold(
15+
appBar: AppBar(
16+
title: Text('Flutter layout demo'),
17+
),
18+
body: Center(child: _buildGrid()),
19+
),
20+
);
21+
}
22+
23+
// #docregion grid
24+
Widget _buildGrid() => GridView.extent(
25+
maxCrossAxisExtent: 150,
26+
padding: const EdgeInsets.all(4),
27+
mainAxisSpacing: 4,
28+
crossAxisSpacing: 4,
29+
children: _buildGridTileList(30));
30+
31+
// The images are saved with names pic0.jpg, pic1.jpg...pic29.jpg.
32+
// The List.generate() constructor allows an easy way to create
33+
// a list when objects have a predictable naming pattern.
34+
List<Container> _buildGridTileList(int count) => List.generate(
35+
count, (i) => Container(child: Image.asset('images/pic$i.jpg')));
36+
// #enddocregion grid
37+
38+
}

examples/layout/grid/pubspec.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: layout
2+
description: >
3+
Sample app from "Building Layouts", https://flutter.io/docs/development/ui/layout.
4+
version: 1.0.0
5+
6+
environment:
7+
sdk: '>=2.0.0-dev.68.0 <3.0.0'
8+
9+
dependencies:
10+
flutter:
11+
sdk: flutter
12+
cupertino_icons: ^0.1.2
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
18+
flutter:
19+
uses-material-design: true
20+
assets: [images/]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Basic Flutter widget test. Learn more at https://flutter.io/docs/testing.
2+
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:layout/main.dart';
5+
6+
void main() {
7+
testWidgets('Example app smoke test', (WidgetTester tester) async {
8+
// Build our app and trigger a frame.
9+
await tester.pumpWidget(MyApp());
10+
11+
expect(find.text('Flutter layout demo'), findsOneWidget);
12+
// TODO: test more app features.
13+
});
14+
}

src/_includes/code/layout/grid/.gitignore

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/_includes/code/layout/grid/main.dart

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/_includes/code/layout/grid/pubspec.yaml

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/docs/development/ui/layout/index.md

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,10 @@ Gallery][].
774774

775775
### GridView
776776

777-
Use [GridView]({{api}}/widgets/GridView-class.html)
778-
to lay widgets out as a two-dimensional list. GridView provides two
779-
pre-fabricated lists, or you can build your own custom grid.
780-
When a GridView detects that its contents are too long to fit the render box,
781-
it automatically scrolls.
777+
Use [GridView][] to lay widgets out as a two-dimensional list. `GridView`
778+
provides two pre-fabricated lists, or you can build your own custom grid. When a
779+
`GridView` detects that its contents are too long to fit the render box, it
780+
automatically scrolls.
782781

783782
#### Summary (GridView)
784783
{:.no_toc}
@@ -812,9 +811,7 @@ it automatically scrolls.
812811

813812
Uses `GridView.extent` to create a grid with tiles a maximum 150 pixels wide.
814813

815-
**Dart code:** [main.dart]({{code}}/layout/grid/main.dart), snippet below<br>
816-
**Images:** [images]({{code}}/layout/grid/images)<br>
817-
**Pubspec:** [pubspec.yaml]({{code}}/layout/grid/pubspec.yaml)
814+
**App source:** [container]({{examples}}/layout/container)
818815
</div>
819816
<div class="col-lg-6" markdown="1">
820817
{% asset ui/layout/gridview-count-flutter-gallery.png class="mw-100"
@@ -823,51 +820,28 @@ it automatically scrolls.
823820

824821
Uses `GridView.count` to create a grid that's 2 tiles wide in portrait mode,
825822
and 3 tiles wide in landscape mode. The titles are created by setting the
826-
`footer` property for each GridTile.
823+
`footer` property for each [GridTile][].
827824

828825
**Dart code:** [grid_list_demo.dart]({{demo}}/material/grid_list_demo.dart)
829826
from the [Flutter Gallery][]
830827
</div>
831828
</div>
832829

833-
<!-- code/layout/grid/main.dart -->
834-
<!-- skip -->
835-
{% prettify dart %}
836-
// The images are saved with names pic1.jpg, pic2.jpg...pic30.jpg.
837-
// The List.generate constructor allows an easy way to create
830+
<?code-excerpt "layout/grid/lib/main.dart (grid)" replace="/\GridView/[!$&!]/g;"?>
831+
```dart
832+
Widget _buildGrid() => [!GridView!].extent(
833+
maxCrossAxisExtent: 150,
834+
padding: const EdgeInsets.all(4),
835+
mainAxisSpacing: 4,
836+
crossAxisSpacing: 4,
837+
children: _buildGridTileList(30));
838+
839+
// The images are saved with names pic0.jpg, pic1.jpg...pic29.jpg.
840+
// The List.generate() constructor allows an easy way to create
838841
// a list when objects have a predictable naming pattern.
839-
List<Container> _buildGridTileList(int count) {
840-
841-
return List<Container>.generate(
842-
count,
843-
(int index) =>
844-
Container(child: Image.asset('images/pic${index+1}.jpg')));
845-
}
846-
847-
Widget buildGrid() {
848-
return GridView.extent(
849-
maxCrossAxisExtent: 150.0,
850-
padding: const EdgeInsets.all(4.0),
851-
mainAxisSpacing: 4.0,
852-
crossAxisSpacing: 4.0,
853-
children: _buildGridTileList(30));
854-
}
855-
856-
class _MyHomePageState extends State<MyHomePage> {
857-
@override
858-
Widget build(BuildContext context) {
859-
return Scaffold(
860-
appBar: AppBar(
861-
title: Text(widget.title),
862-
),
863-
body: Center(
864-
child: buildGrid(),
865-
),
866-
);
867-
}
868-
}
869-
{% endprettify %}
870-
842+
List<Container> _buildGridTileList(int count) => List.generate(
843+
count, (i) => Container(child: Image.asset('images/pic$i.jpg')));
844+
```
871845

872846
<hr>
873847

@@ -1210,6 +1184,8 @@ The following resources may help when writing layout code.
12101184
[Container]: {{api}}/widgets/Container-class.html
12111185
[Expanded]: {{api}}/widgets/Expanded-class.html
12121186
[Flutter Gallery]: {{site.repo.flutter}}/tree/master/examples/flutter_gallery
1187+
[GridView]: {{api}}/widgets/GridView-class.html
1188+
[GridTile]: {{api}}/material/GridTile-class.html
12131189
[Icon]: {{api}}/material/Icons-class.html
12141190
[Image]: {{api}}/widgets/Image-class.html
12151191
[layout widgets]: /docs/development/ui/widgets/layout

0 commit comments

Comments
 (0)