-
-
Notifications
You must be signed in to change notification settings - Fork 646
Expand file tree
/
Copy pathappbar_test.dart
More file actions
365 lines (310 loc) · 11.5 KB
/
appbar_test.dart
File metadata and controls
365 lines (310 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:getwidget/getwidget.dart';
void main() {
const title = Text('Appbar');
final leading = GFIconButton(
icon: const Icon(
Icons.wb_sunny,
color: GFColors.WHITE,
),
onPressed: () {},
type: GFButtonType.transparent,
);
final actionButton = GFIconButton(
icon: const Icon(
Icons.favorite,
color: GFColors.WHITE,
),
onPressed: () {},
type: GFButtonType.transparent,
);
final flexibleSpace = Container(color: Colors.amber);
final shape = RoundedRectangleBorder(
side: const BorderSide(
color: Colors.tealAccent, width: 1, style: BorderStyle.solid),
borderRadius: BorderRadius.circular(25));
final bottom = PreferredSize(
child: Container(
color: Colors.orange,
height: 4,
),
preferredSize: const Size.fromHeight(4));
const textTheme =
TextTheme(headlineSmall: TextStyle(color: Colors.tealAccent));
const iconTheme = IconThemeData(color: Colors.red);
const actionsIconTheme = IconThemeData(color: Colors.amber);
testWidgets('GF AppBar can be constructed', (tester) async {
final GFAppBar appbar = GFAppBar();
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
});
testWidgets('GF AppBar can be constructed with title, leading and actions',
(tester) async {
final GFAppBar appbar = GFAppBar(
leading: leading,
title: title,
actions: <Widget>[actionButton],
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(find.byIcon(Icons.wb_sunny), findsOneWidget);
expect(find.byIcon(Icons.favorite), findsOneWidget);
expect(app.appbar.leading, leading);
expect(app.appbar.title, title);
expect(app.appbar.actions!.length, 1);
});
testWidgets('GF AppBar with title with no titleSpace', (tester) async {
// when GFAppBar.titleSpacing is 0, title takes all the space available
// with or without leading and actions
final GFAppBar appbar = GFAppBar(
leading: leading,
title: title,
titleSpacing: 0,
actions: <Widget>[actionButton],
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(find.byIcon(Icons.wb_sunny), findsOneWidget);
expect(find.byIcon(Icons.favorite), findsOneWidget);
expect(app.appbar.leading, leading);
expect(app.appbar.title, title);
expect(app.appbar.actions!.length, 1);
expect(app.appbar.titleSpacing, 0);
});
testWidgets('GF AppBar with centerTitle', (tester) async {
// when GFAppBar.centerTitle is true, title will align at center.
final GFAppBar appbar = GFAppBar(
leading: leading,
title: title,
actions: <Widget>[actionButton],
centerTitle: true,
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(find.byIcon(Icons.wb_sunny), findsOneWidget);
expect(find.byIcon(Icons.favorite), findsOneWidget);
expect(app.appbar.leading, leading);
expect(app.appbar.title, title);
expect(app.appbar.actions!.length, 1);
expect(app.appbar.centerTitle, true);
});
testWidgets('GF AppBar with automaticallyImplyLeading', (tester) async {
// when GFAppBar.automaticallyImplyLeading is true and [leading] is null, automatically try to deduce what the leading
// widget should be. If false and [leading] is null, leading space is given to [title].
// If leading widget is not null, this parameter has no effect.
final GFAppBar appbar = GFAppBar(
title: title,
actions: <Widget>[actionButton],
automaticallyImplyLeading: true,
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(find.byIcon(Icons.favorite), findsOneWidget);
expect(app.appbar.title, title);
expect(app.appbar.actions!.length, 1);
expect(app.appbar.automaticallyImplyLeading, true);
});
testWidgets('GF AppBar with flexibleSpace', (tester) async {
// GFAppbar.flexibleSpace stacked behind the toolbar and the tab bar. It's height will
// be the same as the app bar's overall height. A flexible space isn't actually
// flexible unless the [GFAppBar]'s container changes the [GFAppBar]'s size.
final GFAppBar appbar = GFAppBar(
title: title,
actions: <Widget>[actionButton],
flexibleSpace: flexibleSpace,
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(find.byIcon(Icons.favorite), findsOneWidget);
expect(find.byWidget(flexibleSpace), findsOneWidget);
expect(app.appbar.title, title);
expect(app.appbar.actions!.length, 1);
expect(app.appbar.flexibleSpace, flexibleSpace);
});
testWidgets('GF AppBar with primary and brightness', (tester) async {
// GFAppbar.primary on false displays title at the top of the screen.
// If true, the app bar's toolbar elements and [bottom] widget will be
// padded on top by the height of the system status bar.
final GFAppBar appbar = GFAppBar(
title: title,
primary: false,
brightness: Brightness.dark,
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(app.appbar.title, title);
expect(app.appbar.primary, false);
expect(app.appbar.brightness, Brightness.dark);
});
testWidgets('GF AppBar with backgroundColor and shape', (tester) async {
// GFAppbar.shape to customize shape of the appbar
final GFAppBar appbar = GFAppBar(
title: title,
actions: <Widget>[actionButton],
backgroundColor: Colors.teal,
shape: shape,
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(find.byIcon(Icons.favorite), findsOneWidget);
expect(app.appbar.title, title);
expect(app.appbar.actions!.length, 1);
expect(app.appbar.backgroundColor, Colors.teal);
expect(app.appbar.shape, shape);
});
testWidgets('GF AppBar with bottom and elevation', (tester) async {
final GFAppBar appbar = GFAppBar(
title: title,
actions: <Widget>[actionButton],
bottom: bottom,
elevation: 1,
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(find.byIcon(Icons.favorite), findsOneWidget);
expect(find.byWidget(bottom), findsOneWidget);
expect(app.appbar.title, title);
expect(app.appbar.actions!.length, 1);
expect(app.appbar.bottom, bottom);
expect(app.appbar.elevation, 1);
});
testWidgets('GF AppBar with textTheme, iconTheme, actionsIconTheme',
(tester) async {
const leadingIcon = Icon(Icons.wb_sunny);
final GFAppBar appbar = GFAppBar(
leading: leadingIcon,
title: title,
actions: const <Widget>[Icon(Icons.favorite)],
textTheme: textTheme,
actionsIconTheme: actionsIconTheme,
iconTheme: iconTheme,
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(find.byIcon(Icons.wb_sunny), findsOneWidget);
expect(find.byIcon(Icons.favorite), findsOneWidget);
expect(app.appbar.leading, leadingIcon);
expect(app.appbar.title, title);
expect(app.appbar.actions!.length, 1);
expect(app.appbar.textTheme, textTheme);
expect(app.appbar.actionsIconTheme, actionsIconTheme);
expect(app.appbar.iconTheme, iconTheme);
});
testWidgets(
'GF AppBar with bottomOpacity, toolbarOpacity and test searchBar with default searchBar value',
(tester) async {
final GFAppBar appbar = GFAppBar(
title: title,
actions: <Widget>[actionButton],
bottomOpacity: 0.5,
toolbarOpacity: 0.6,
bottom: bottom,
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
expect(find.text('Appbar'), findsOneWidget);
expect(find.byIcon(Icons.favorite), findsOneWidget);
expect(find.byWidget(bottom), findsOneWidget);
expect(app.appbar.title, title);
expect(app.appbar.actions!.length, 1);
expect(app.appbar.bottom, bottom);
expect(app.appbar.bottomOpacity, 0.5);
expect(app.appbar.toolbarOpacity, 0.6);
// set appbar.searchBar = false state to disable search bar
expect(app.appbar.searchBar, isFalse);
// try to find search icon when appbar.searchBar = false
expect(find.byIcon(Icons.search), findsNothing);
});
testWidgets('GF AppBar with searchBar', (tester) async {
final Key appbarKey = UniqueKey();
final TextEditingController _searchController = TextEditingController();
final GFAppBar appbar = GFAppBar(
key: appbarKey,
title: title,
actions: <Widget>[actionButton],
searchBar: true,
searchController: _searchController,
searchHintText: 'Search',
searchHintStyle: const TextStyle(fontSize: 14, color: Colors.white70),
searchTextStyle: const TextStyle(fontSize: 16, color: Colors.white),
searchBarColorTheme: Colors.white,
onChanged: (value) {
print('on change $value');
},
onSubmitted: (value) {
print('submitted $value');
},
onTap: () {
print('tapped');
},
);
final TestApp app = TestApp(appbar);
await tester.pumpWidget(app);
// find appbar by key
expect(find.byKey(appbarKey), findsOneWidget);
// find appbar title text
expect(find.text('Appbar'), findsOneWidget);
// find appbar action button icon
expect(find.byIcon(Icons.favorite), findsOneWidget);
// set appbar.searchBar = true state to enable search bar
expect(app.appbar.searchBar, isTrue);
// find appbar search icon
expect(find.byIcon(Icons.search), findsOneWidget);
// tap the search icon
await tester.tap(find.byIcon(Icons.search));
// rebuild the widget
await tester.pump();
// enter 'flutter' to the textField
await tester.enterText(find.byWidget(appbar), 'flutter');
// find the text 'flutter' to test onChanged
expect(find.text('flutter'), findsOneWidget);
// to test onSubmitted when TextInputAction.done
await tester.testTextInput.receiveAction(TextInputAction.done);
// rebuild the widget
await tester.pump();
// find the text 'flutter' in textField
expect(find.text('flutter'), findsOneWidget);
// tap the close icon to close the searchBar
await tester.tap(find.byIcon(Icons.close));
// rebuild the widget
await tester.pump();
// try to find textField
expect(find.byType(TextField), findsNothing);
// tap the search icon to reopen the searchBar
await tester.tap(find.byIcon(Icons.search));
// rebuild the widget
await tester.pump();
// find the text 'flutter' in textField
expect(find.text('flutter'), findsNothing);
// tap the textField to test onTap
await tester.tap(find.byType(TextField));
// rebuild the widget
await tester.pump();
});
}
class TestApp extends StatefulWidget {
const TestApp(this.appbar);
final GFAppBar appbar;
@override
_TestAppState createState() => _TestAppState();
}
class _TestAppState extends State<TestApp> {
@override
Widget build(BuildContext context) => MaterialApp(
home: Scaffold(
appBar: widget.appbar,
body: Container(
child: const Text('body'),
)),
);
}