@@ -17,8 +17,8 @@ orientation.
17
17
18
18
This recipe uses the following steps:
19
19
20
- 1 . Create an app that updates the layout of the content, based on the
21
- orientation.
20
+ 1 . Create an app that updates the layout of the content,
21
+ based on the orientation.
22
22
1 . Create an orientation test group.
23
23
1 . Create a portrait orientation test.
24
24
1 . Create a landscape orientation test.
@@ -35,8 +35,8 @@ app is in portrait or landscape mode:
35
35
flutter create orientation_tests
36
36
```
37
37
38
- 2. Follow the steps in [Update the UI based on orientation][] to set up the
39
- project.
38
+ 2. Follow the steps in [Update the UI based on orientation][] to
39
+ set up the project.
40
40
41
41
## 2. Create an orientation test group
42
42
@@ -46,23 +46,24 @@ group your future orientation tests:
46
46
1. In your Flutter project, open `test/widget_test.dart`.
47
47
1. Replace the existing contents with the following:
48
48
49
+ <?code-excerpt "cookbook/testing/widget/orientation_tests/test/widget_test.dart (scaffolding)"?>
49
50
```dart title="widget_test.dart"
50
51
import 'package:flutter/material.dart';
51
52
import 'package:flutter_test/flutter_test.dart';
52
53
import 'package:orientation_tests/main.dart';
53
-
54
+
54
55
void main() {
55
56
group('Orientation', () {
56
- ...
57
+ // ···
57
58
});
58
59
}
59
60
```
60
61
61
62
## 3. Create a portrait orientation test
62
63
63
- Add the portrait orientation test to the `Orientation` group. This test makes
64
- sure that the orientation is `portrait` and that only `2` columns of data
65
- appear in the app:
64
+ Add the portrait orientation test to the `Orientation` group.
65
+ This test makes sure that the orientation is `portrait` and that
66
+ only `2` columns of data appear in the app:
66
67
67
68
1. In `test/widget_test.dart`, replace `...` inside of the `Orientation` group
68
69
with the following test:
@@ -83,25 +84,25 @@ appear in the app:
83
84
await tester.pump();
84
85
85
86
// Verify initial orientation is portrait.
86
- Orientation orientation =
87
+ final orientation =
87
88
MediaQuery.of(tester.element(find.byType(OrientationList)))
88
89
.orientation;
89
90
expect(orientation, Orientation.portrait);
90
91
91
92
// Verify there are only 2 columns in portrait mode.
92
93
final gridViewFinder = find.byType(GridView);
93
- GridView gridView = tester.widget<GridView>(gridViewFinder);
94
- SliverGridDelegateWithFixedCrossAxisCount delegate =
94
+ final gridView = tester.widget<GridView>(gridViewFinder);
95
+ final delegate =
95
96
gridView.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount;
96
97
expect(delegate.crossAxisCount, 2);
97
98
});
98
99
```
99
100
100
101
## 4. Create a landscape orientation test
101
102
102
- Add the landscape orientation test to the `Orientation` group. This test makes
103
- sure that the orientation is `landscape` and that only `3` columns of data
104
- appear in the app:
103
+ Add the landscape orientation test to the `Orientation` group.
104
+ This test makes sure that the orientation is `landscape` and that
105
+ only `3` columns of data appear in the app:
105
106
106
107
1. In `test/widget_test.dart`, inside of the `Orientation` group,
107
108
add the following test after the landscape test:
@@ -122,15 +123,15 @@ appear in the app:
122
123
await tester.pump();
123
124
124
125
// Verify initial orientation is landscape.
125
- Orientation orientation =
126
+ final orientation =
126
127
MediaQuery.of(tester.element(find.byType(OrientationList)))
127
128
.orientation;
128
129
expect(orientation, Orientation.landscape);
129
130
130
131
// Verify there are only 3 columns in landscape mode.
131
132
final gridViewFinder = find.byType(GridView);
132
- GridView gridView = tester.widget<GridView>(gridViewFinder);
133
- SliverGridDelegateWithFixedCrossAxisCount delegate =
133
+ final gridView = tester.widget<GridView>(gridViewFinder);
134
+ final delegate =
134
135
gridView.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount;
135
136
expect(delegate.crossAxisCount, 3);
136
137
});
@@ -148,7 +149,6 @@ flutter test test/widget_test.dart
148
149
149
150
<? code-excerpt "cookbook/testing/widget/orientation_tests/test/widget_test.dart"?>
150
151
``` dart title="widget_test.dart"
151
-
152
152
import 'package:flutter/material.dart';
153
153
import 'package:flutter_test/flutter_test.dart';
154
154
import 'package:orientation_tests/main.dart';
@@ -169,15 +169,15 @@ void main() {
169
169
await tester.pump();
170
170
171
171
// Verify initial orientation is portrait.
172
- Orientation orientation =
172
+ final orientation =
173
173
MediaQuery.of(tester.element(find.byType(OrientationList)))
174
174
.orientation;
175
175
expect(orientation, Orientation.portrait);
176
176
177
177
// Verify there are only 2 columns in portrait mode.
178
178
final gridViewFinder = find.byType(GridView);
179
- GridView gridView = tester.widget<GridView>(gridViewFinder);
180
- SliverGridDelegateWithFixedCrossAxisCount delegate =
179
+ final gridView = tester.widget<GridView>(gridViewFinder);
180
+ final delegate =
181
181
gridView.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount;
182
182
expect(delegate.crossAxisCount, 2);
183
183
});
@@ -196,15 +196,15 @@ void main() {
196
196
await tester.pump();
197
197
198
198
// Verify initial orientation is landscape.
199
- Orientation orientation =
199
+ final orientation =
200
200
MediaQuery.of(tester.element(find.byType(OrientationList)))
201
201
.orientation;
202
202
expect(orientation, Orientation.landscape);
203
203
204
204
// Verify there are only 3 columns in landscape mode.
205
205
final gridViewFinder = find.byType(GridView);
206
- GridView gridView = tester.widget<GridView>(gridViewFinder);
207
- SliverGridDelegateWithFixedCrossAxisCount delegate =
206
+ final gridView = tester.widget<GridView>(gridViewFinder);
207
+ final delegate =
208
208
gridView.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount;
209
209
expect(delegate.crossAxisCount, 3);
210
210
});
@@ -248,15 +248,15 @@ class OrientationList extends StatelessWidget {
248
248
body: OrientationBuilder(
249
249
builder: (context, orientation) {
250
250
return GridView.count(
251
- // Create a grid with 2 columns in portrait mode, or 3 columns in
252
- // landscape mode.
251
+ // Create a grid with 2 columns in portrait mode, or
252
+ // 3 columns in landscape mode.
253
253
crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
254
- // Generate 100 widgets that display their index in the List .
254
+ // Generate 100 widgets that display their index in the list .
255
255
children: List.generate(100, (index) {
256
256
return Center(
257
257
child: Text(
258
258
'Item $index',
259
- style: Theme .of(context).textTheme .displayLarge,
259
+ style: TextTheme .of(context).displayLarge,
260
260
),
261
261
);
262
262
}),
0 commit comments