Skip to content

Layout example app for GridView #2308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/layout/grid/README.md
38 changes: 38 additions & 0 deletions examples/layout/grid/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;

void main() {
debugPaintSizeEnabled = false; // Set to true for visual layout
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter layout demo',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter layout demo'),
),
body: Center(child: _buildGrid()),
),
);
}

// #docregion grid
Widget _buildGrid() => GridView.extent(
maxCrossAxisExtent: 150,
padding: const EdgeInsets.all(4),
mainAxisSpacing: 4,
crossAxisSpacing: 4,
children: _buildGridTileList(30));

// The images are saved with names pic0.jpg, pic1.jpg...pic29.jpg.
// The List.generate() constructor allows an easy way to create
// a list when objects have a predictable naming pattern.
List<Container> _buildGridTileList(int count) => List.generate(
count, (i) => Container(child: Image.asset('images/pic$i.jpg')));
// #enddocregion grid

}
20 changes: 20 additions & 0 deletions examples/layout/grid/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: layout
description: >
Sample app from "Building Layouts", https://flutter.io/docs/development/ui/layout.
version: 1.0.0

environment:
sdk: '>=2.0.0-dev.68.0 <3.0.0'

dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2

dev_dependencies:
flutter_test:
sdk: flutter

flutter:
uses-material-design: true
assets: [images/]
14 changes: 14 additions & 0 deletions examples/layout/grid/test/widget_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Basic Flutter widget test. Learn more at https://flutter.io/docs/testing.

import 'package:flutter_test/flutter_test.dart';
import 'package:layout/main.dart';

void main() {
testWidgets('Example app smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());

expect(find.text('Flutter layout demo'), findsOneWidget);
// TODO: test more app features.
});
}
9 changes: 0 additions & 9 deletions src/_includes/code/layout/grid/.gitignore

This file was deleted.

68 changes: 0 additions & 68 deletions src/_includes/code/layout/grid/main.dart

This file was deleted.

40 changes: 0 additions & 40 deletions src/_includes/code/layout/grid/pubspec.yaml

This file was deleted.

68 changes: 22 additions & 46 deletions src/docs/development/ui/layout/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -774,11 +774,10 @@ Gallery][].

### GridView

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

#### Summary (GridView)
{:.no_toc}
Expand Down Expand Up @@ -812,9 +811,7 @@ it automatically scrolls.

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

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

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

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

<!-- code/layout/grid/main.dart -->
<!-- skip -->
{% prettify dart %}
// The images are saved with names pic1.jpg, pic2.jpg...pic30.jpg.
// The List.generate constructor allows an easy way to create
<?code-excerpt "layout/grid/lib/main.dart (grid)" replace="/\GridView/[!$&!]/g;"?>
```dart
Widget _buildGrid() => [!GridView!].extent(
maxCrossAxisExtent: 150,
padding: const EdgeInsets.all(4),
mainAxisSpacing: 4,
crossAxisSpacing: 4,
children: _buildGridTileList(30));

// The images are saved with names pic0.jpg, pic1.jpg...pic29.jpg.
// The List.generate() constructor allows an easy way to create
// a list when objects have a predictable naming pattern.
List<Container> _buildGridTileList(int count) {

return List<Container>.generate(
count,
(int index) =>
Container(child: Image.asset('images/pic${index+1}.jpg')));
}

Widget buildGrid() {
return GridView.extent(
maxCrossAxisExtent: 150.0,
padding: const EdgeInsets.all(4.0),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
children: _buildGridTileList(30));
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: buildGrid(),
),
);
}
}
{% endprettify %}

List<Container> _buildGridTileList(int count) => List.generate(
count, (i) => Container(child: Image.asset('images/pic$i.jpg')));
```

<hr>

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