-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
codelab_rebuild.yaml
1184 lines (1142 loc) · 42.3 KB
/
codelab_rebuild.yaml
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
name: Your first Flutter app (updated for 2023)
steps:
- name: step_03
steps:
- name: Remove generated code
rmdir: step_03
- name: Create project
flutter: create namer_app
- name: Strip DEVELOPMENT_TEAM
strip-lines-containing: DEVELOPMENT_TEAM =
path: namer_app/ios/Runner.xcodeproj/project.pbxproj
- name: Replace pubspec.yaml
path: namer_app/pubspec.yaml
replace-contents: |
name: namer_app
description: A new Flutter project.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 0.0.1+1
environment:
sdk: ^3.5.1
dependencies:
flutter:
sdk: flutter
english_words: ^4.0.0
provider: ^6.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
flutter:
uses-material-design: true
- name: Update dependencies
path: namer_app
flutter: pub upgrade --major-versions
- name: Replace analysis_options.yaml
path: namer_app/analysis_options.yaml
replace-contents: |
include: package:flutter_lints/flutter.yaml
linter:
rules:
avoid_print: false
prefer_const_constructors_in_immutables: false
prefer_const_constructors: false
prefer_const_literals_to_create_immutables: false
prefer_final_fields: false
unnecessary_breaks: true
use_key_in_widget_constructors: false
- name: Replace lib/main.dart
path: namer_app/lib/main.dart
replace-contents: |
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => MyAppState(),
child: MaterialApp(
title: 'Namer App',
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
),
home: MyHomePage(),
),
);
}
}
class MyAppState extends ChangeNotifier {
var current = WordPair.random();
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
return Scaffold(
body: Column(
children: [
Text('A random idea:'),
Text(appState.current.asLowerCase),
],
),
);
}
}
- name: Remove README
rm: namer_app/README.md
- name: Replace test/widget_test.dart
path: namer_app/test/widget_test.dart
replace-contents: |
import 'package:flutter_test/flutter_test.dart';
import 'package:namer_app/main.dart';
void main() {
testWidgets('App starts', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
expect(find.text('A random idea:'), findsOneWidget);
});
}
- name: Build iOS simulator bundle
platforms: [ macos ]
path: namer_app
flutter: build ios --simulator
- name: Build macOS app
platforms: [ macos ]
path: namer_app
flutter: build macos
- name: Build Linux app
platforms: [ linux ]
path: namer_app
flutter: build linux
- name: Build Windows app
platforms: [ windows ]
path: namer_app
flutter: build windows
- name: Copy step_03
copydir:
from: namer_app
to: step_03
- name: Flutter clean
path: step_03
flutter: clean
- name: step_04_a_widget
steps:
- name: Remove generated code
rmdir: step_04_a_widget
- name: Add button code
path: namer_app/lib/main.dart
patch-u: |
--- b/namer/step_04_a_widget/lib/main.dart
+++ a/namer/step_04_a_widget/lib/main.dart
@@ -37,8 +37,14 @@ class MyHomePage extends StatelessWidget {
return Scaffold(
body: Column(
children: [
- Text('A random idea:'),
+ Text('A random AWESOME idea:'),
Text(appState.current.asLowerCase),
+ ElevatedButton(
+ onPressed: () {
+ print('button pressed!');
+ },
+ child: Text('Next'),
+ ),
],
),
);
- name: Patch test/widget_test.dart
path: namer_app/test/widget_test.dart
patch-u: |
--- b/namer/step_04_a_widget/test/widget_test.dart
+++ a/namer/step_04_a_widget/test/widget_test.dart
@@ -5,6 +5,6 @@ import 'package:namer_app/main.dart';
void main() {
testWidgets('App starts', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
- expect(find.text('A random idea:'), findsOneWidget);
+ expect(find.text('A random AWESOME idea:'), findsOneWidget);
});
}
- name: Copy step_04_a_widget
copydir:
from: namer_app
to: step_04_a_widget
- name: step_04_b_behavior
steps:
- name: Remove generated code
rmdir: step_04_b_behavior
- name: Add button behavior
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -27,6 +27,11 @@ class MyApp extends StatelessWidget {
class MyAppState extends ChangeNotifier {
var current = WordPair.random();
+
+ void getNext() {
+ current = WordPair.random();
+ notifyListeners();
+ }
}
class MyHomePage extends StatelessWidget {
@@ -41,7 +46,7 @@ class MyHomePage extends StatelessWidget {
Text(appState.current.asLowerCase),
ElevatedButton(
onPressed: () {
- print('button pressed!');
+ appState.getNext();
},
child: Text('Next'),
),
- name: Add additional test
path: namer_app/test/widget_test.dart
patch-u: |
--- a/namer_app/test/widget_test.dart
+++ b/namer_app/test/widget_test.dart
@@ -1,3 +1,4 @@
+import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:namer_app/main.dart';
@@ -6,5 +7,45 @@
testWidgets('App starts', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
expect(find.text('A random AWESOME idea:'), findsOneWidget);
+ });
+
+ testWidgets('Tapping button changes word pair', (WidgetTester tester) async {
+ await tester.pumpWidget(const MyApp());
+
+ String findWordPair() {
+ final wordPairTextWidget = tester
+ // Get all Text widgets...
+ .widgetList<Text>(find.byType(Text))
+ // ... skip one ('A random AWESOME idea:') ...
+ .skip(1)
+ // ... and take the first after it.
+ .first;
+ return wordPairTextWidget.data!;
+ }
+
+ // Tap several times and keep a list of word pair values.
+ const tryCount = 5;
+ final pairs = <String>[
+ findWordPair(),
+ ];
+ for (var i = 1; i < tryCount; i++) {
+ await tester.tap(find.text('Next'));
+ await tester.pumpAndSettle();
+ pairs.add(findWordPair());
+ }
+
+ expect(
+ // Converting the list to a set to remove duplicates.
+ pairs.toSet(),
+ // An occassional duplicate word pair is okay and expected.
+ // We only fail this test when there is zero variance - all the
+ // word pairs are the same, even though we clicked 'Next' several times.
+ hasLength(greaterThan(1)),
+ reason: 'After clicking $tryCount times, '
+ 'the app should have generated at least two different word pairs. '
+ 'Instead, the app showed these: $pairs. '
+ 'That almost certainly means that the word pair is not being '
+ 'randomly generated at all. The button does not work.',
+ );
});
}
- name: Copy step_04_b_behavior
copydir:
from: namer_app
to: step_04_b_behavior
- name: step_05_a_pair
steps:
- name: Remove generated code
rmdir: step_05_a_pair
- name: Introduce pair variable
path: namer_app/lib/main.dart
patch-u: |
--- b/namer/step_05_a_pair/lib/main.dart
+++ a/namer/step_05_a_pair/lib/main.dart
@@ -38,12 +38,13 @@ class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
+ var pair = appState.current;
return Scaffold(
body: Column(
children: [
Text('A random AWESOME idea:'),
- Text(appState.current.asLowerCase),
+ Text(pair.asLowerCase),
ElevatedButton(
onPressed: () {
appState.getNext();
- name: Copy step_05_a_pair
copydir:
from: namer_app
to: step_05_a_pair
- name: step_05_b_extract
steps:
- name: Remove generated code
rmdir: step_05_b_extract
- name: Extract BigCard
path: namer_app/lib/main.dart
patch-u: |
--- b/namer/step_05_b_extract/lib/main.dart
+++ a/namer/step_05_b_extract/lib/main.dart
@@ -44,7 +44,7 @@ class MyHomePage extends StatelessWidget {
body: Column(
children: [
Text('A random AWESOME idea:'),
- Text(pair.asLowerCase),
+ BigCard(pair: pair),
ElevatedButton(
onPressed: () {
appState.getNext();
@@ -56,3 +56,17 @@ class MyHomePage extends StatelessWidget {
);
}
}
+
+class BigCard extends StatelessWidget {
+ const BigCard({
+ super.key,
+ required this.pair,
+ });
+
+ final WordPair pair;
+
+ @override
+ Widget build(BuildContext context) {
+ return Text(pair.asLowerCase);
+ }
+}
- name: Update test to use BigCard
path: namer_app/test/widget_test.dart
patch-u: |
--- a/namer_app/test/widget_test.dart
+++ b/namer_app/test/widget_test.dart
@@ -13,13 +13,10 @@ void main() {
await tester.pumpWidget(const MyApp());
String findWordPair() {
- final wordPairTextWidget = tester
- // Get all Text widgets...
- .widgetList<Text>(find.byType(Text))
- // ... skip one ('A random AWESOME idea:') ...
- .skip(1)
- // ... and take the first after it.
- .first;
+ final wordPairTextWidget = tester.widget<Text>(find.descendant(
+ of: find.byType(BigCard),
+ matching: find.byType(Text),
+ ));
return wordPairTextWidget.data!;
}
- name: Copy step_05_b_extract
copydir:
from: namer_app
to: step_05_b_extract
- name: step_05_c_card_padding
steps:
- name: Remove generated code
rmdir: step_05_c_card_padding
- name: Add Card and Padding
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -67,6 +67,11 @@ class BigCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
- return Text(pair.asLowerCase);
+ return Card(
+ child: Padding(
+ padding: const EdgeInsets.all(20),
+ child: Text(pair.asLowerCase),
+ ),
+ );
}
}
- name: Copy step_05_c_card_padding
copydir:
from: namer_app
to: step_05_c_card_padding
- name: step_05_d_theme
steps:
- name: Remove generated code
rmdir: step_05_d_theme
- name: Add Theme
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -67,7 +67,10 @@ class BigCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
+ final theme = Theme.of(context);
+
return Card(
+ color: theme.colorScheme.primary,
child: Padding(
padding: const EdgeInsets.all(20),
child: Text(pair.asLowerCase),
- name: Copy step_05_d_theme
copydir:
from: namer_app
to: step_05_d_theme
- name: step_05_e_text_style
steps:
- name: Remove generated code
rmdir: step_05_e_text_style
- name: Add textStyle
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -68,12 +68,15 @@ class BigCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
+ final style = theme.textTheme.displayMedium!.copyWith(
+ color: theme.colorScheme.onPrimary,
+ );
return Card(
color: theme.colorScheme.primary,
child: Padding(
padding: const EdgeInsets.all(20),
- child: Text(pair.asLowerCase),
+ child: Text(pair.asLowerCase, style: style),
),
);
}
- name: Copy step_05_e_text_style
copydir:
from: namer_app
to: step_05_e_text_style
- name: step_05_f_accessibility
steps:
- name: Remove generated code
rmdir: step_05_f_accessibility
- name: Add semanticsLabel
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -76,7 +76,11 @@ class BigCard extends StatelessWidget {
color: theme.colorScheme.primary,
child: Padding(
padding: const EdgeInsets.all(20),
- child: Text(pair.asLowerCase, style: style),
+ child: Text(
+ pair.asLowerCase,
+ style: style,
+ semanticsLabel: "${pair.first} ${pair.second}",
+ ),
),
);
}
- name: Copy step_05_f_accessibility
copydir:
from: namer_app
to: step_05_f_accessibility
- name: step_05_g_center_vertical
steps:
- name: Remove generated code
rmdir: step_05_g_center_vertical
- name: Center column vertically
path: namer_app/lib/main.dart
patch-u: |
--- b/namer/step_05_g_center_vertical/lib/main.dart
+++ a/namer/step_05_g_center_vertical/lib/main.dart
@@ -42,6 +42,7 @@ class MyHomePage extends StatelessWidget {
return Scaffold(
body: Column(
+ mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('A random AWESOME idea:'),
BigCard(pair: pair),
- name: Copy step_05_g_center_vertical
copydir:
from: namer_app
to: step_05_g_center_vertical
- name: step_05_h_center_horizontal
steps:
- name: Remove generated code
rmdir: step_05_h_center_horizontal
- name: Center column horizontally
path: namer_app/lib/main.dart
patch-u: |
--- b/namer/step_05_h_center_horizontal/lib/main.dart
+++ a/namer/step_05_h_center_horizontal/lib/main.dart
@@ -41,18 +41,20 @@ class MyHomePage extends StatelessWidget {
var pair = appState.current;
return Scaffold(
- body: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Text('A random AWESOME idea:'),
- BigCard(pair: pair),
- ElevatedButton(
- onPressed: () {
- appState.getNext();
- },
- child: Text('Next'),
- ),
- ],
+ body: Center(
+ child: Column(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ Text('A random AWESOME idea:'),
+ BigCard(pair: pair),
+ ElevatedButton(
+ onPressed: () {
+ appState.getNext();
+ },
+ child: Text('Next'),
+ ),
+ ],
+ ),
),
);
}
- name: Copy step_05_h_center_horizontal
copydir:
from: namer_app
to: step_05_h_center_horizontal
- name: step_05_i_optional_changes
steps:
- name: Remove generated code
rmdir: step_05_i_optional_changes
- name: Remove text, add SizedBox
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -45,8 +45,8 @@ class MyHomePage extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
- Text('A random AWESOME idea:'),
BigCard(pair: pair),
+ SizedBox(height: 10),
ElevatedButton(
onPressed: () {
appState.getNext();
- name: Update test
path: namer_app/test/widget_test.dart
patch-u: |
--- a/namer_app/test/widget_test.dart
+++ b/namer_app/test/widget_test.dart
@@ -6,7 +6,7 @@ import 'package:namer_app/main.dart';
void main() {
testWidgets('App starts', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
- expect(find.text('A random AWESOME idea:'), findsOneWidget);
+ expect(find.text('Next'), findsOneWidget);
});
testWidgets('Tapping button changes word pair', (WidgetTester tester) async {
- name: Copy step_05_i_optional_changes
copydir:
from: namer_app
to: step_05_i_optional_changes
- name: step_06_a_business_logic
steps:
- name: Remove generated code
rmdir: step_06_a_business_logic
- name: Add 'Like' button business logic
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -32,6 +32,17 @@ class MyAppState extends ChangeNotifier {
current = WordPair.random();
notifyListeners();
}
+
+ var favorites = <WordPair>[];
+
+ void toggleFavorite() {
+ if (favorites.contains(current)) {
+ favorites.remove(current);
+ } else {
+ favorites.add(current);
+ }
+ notifyListeners();
+ }
}
class MyHomePage extends StatelessWidget {
- name: Copy step_06_a_business_logic
copydir:
from: namer_app
to: step_06_a_business_logic
- name: step_06_b_add_row
steps:
- name: Remove generated code
rmdir: step_06_b_add_row
- name: Add row for buttons
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -58,11 +58,16 @@ class MyHomePage extends StatelessWidget {
children: [
BigCard(pair: pair),
SizedBox(height: 10),
- ElevatedButton(
- onPressed: () {
- appState.getNext();
- },
- child: Text('Next'),
+ Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ ElevatedButton(
+ onPressed: () {
+ appState.getNext();
+ },
+ child: Text('Next'),
+ ),
+ ],
),
],
),
- name: Copy step_06_b_add_row
copydir:
from: namer_app
to: step_06_b_add_row
- name: step_06_c_add_like_button
steps:
- name: Remove generated code
rmdir: step_06_c_add_like_button
- name: Add 'Like' button to UI
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -51,6 +51,13 @@ class MyHomePage extends StatelessWidget {
var appState = context.watch<MyAppState>();
var pair = appState.current;
+ IconData icon;
+ if (appState.favorites.contains(pair)) {
+ icon = Icons.favorite;
+ } else {
+ icon = Icons.favorite_border;
+ }
+
return Scaffold(
body: Center(
child: Column(
@@ -61,6 +68,14 @@ class MyHomePage extends StatelessWidget {
Row(
mainAxisSize: MainAxisSize.min,
children: [
+ ElevatedButton.icon(
+ onPressed: () {
+ appState.toggleFavorite();
+ },
+ icon: Icon(icon),
+ label: Text('Like'),
+ ),
+ SizedBox(width: 10),
ElevatedButton(
onPressed: () {
appState.getNext();
- name: Add test to check 'Like' button
path: namer_app/test/widget_test.dart
patch-u: |
--- a/namer_app/test/widget_test.dart
+++ b/namer_app/test/widget_test.dart
@@ -45,4 +45,26 @@
'randomly generated at all. The button does not work.',
);
});
+
+ testWidgets('Tapping "Like" changes icon', (WidgetTester tester) async {
+ await tester.pumpWidget(const MyApp());
+
+ Finder findElevatedButtonByIcon(IconData icon) {
+ return find.descendant(
+ of: find.bySubtype<ElevatedButton>(),
+ matching: find.byIcon(icon),
+ );
+ }
+
+ // At start: an outlined heart icon.
+ expect(findElevatedButtonByIcon(Icons.favorite_border), findsOneWidget);
+ expect(findElevatedButtonByIcon(Icons.favorite), findsNothing);
+
+ await tester.tap(find.text('Like'));
+ await tester.pumpAndSettle();
+
+ // After tap: a full heart icon.
+ expect(findElevatedButtonByIcon(Icons.favorite_border), findsNothing);
+ expect(findElevatedButtonByIcon(Icons.favorite), findsOneWidget);
+ });
}
- name: Copy step_06_c_add_like_button
copydir:
from: namer_app
to: step_06_c_add_like_button
- name: step_07_a_split_my_home_page
steps:
- name: Remove generated code
rmdir: step_07_a_split_my_home_page
- name: Split MyHomePage into two widgets
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -48,6 +48,43 @@
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
+ return Scaffold(
+ body: Row(
+ children: [
+ SafeArea(
+ child: NavigationRail(
+ extended: false,
+ destinations: [
+ NavigationRailDestination(
+ icon: Icon(Icons.home),
+ label: Text('Home'),
+ ),
+ NavigationRailDestination(
+ icon: Icon(Icons.favorite),
+ label: Text('Favorites'),
+ ),
+ ],
+ selectedIndex: 0,
+ onDestinationSelected: (value) {
+ print('selected: $value');
+ },
+ ),
+ ),
+ Expanded(
+ child: Container(
+ color: Theme.of(context).colorScheme.primaryContainer,
+ child: GeneratorPage(),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+}
+
+class GeneratorPage extends StatelessWidget {
+ @override
+ Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
var pair = appState.current;
@@ -58,34 +95,32 @@
icon = Icons.favorite_border;
}
- return Scaffold(
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- BigCard(pair: pair),
- SizedBox(height: 10),
- Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- ElevatedButton.icon(
- onPressed: () {
- appState.toggleFavorite();
- },
- icon: Icon(icon),
- label: Text('Like'),
- ),
- SizedBox(width: 10),
- ElevatedButton(
- onPressed: () {
- appState.getNext();
- },
- child: Text('Next'),
- ),
- ],
- ),
- ],
- ),
+ return Center(
+ child: Column(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ BigCard(pair: pair),
+ SizedBox(height: 10),
+ Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ ElevatedButton.icon(
+ onPressed: () {
+ appState.toggleFavorite();
+ },
+ icon: Icon(icon),
+ label: Text('Like'),
+ ),
+ SizedBox(width: 10),
+ ElevatedButton(
+ onPressed: () {
+ appState.getNext();
+ },
+ child: Text('Next'),
+ ),
+ ],
+ ),
+ ],
),
);
}
- name: Copy step_07_a_split_my_home_page
copydir:
from: namer_app
to: step_07_a_split_my_home_page
- name: step_07_b_convert_to_stateful
steps:
- name: Remove generated code
rmdir: step_07_b_convert_to_stateful
- name: Convert MyHomePage to StatefulWidget
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -45,7 +45,12 @@ class MyAppState extends ChangeNotifier {
}
}
-class MyHomePage extends StatelessWidget {
+class MyHomePage extends StatefulWidget {
+ @override
+ State<MyHomePage> createState() => _MyHomePageState();
+}
+
+class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
- name: Copy step_07_b_convert_to_stateful
copydir:
from: namer_app
to: step_07_b_convert_to_stateful
- name: step_07_c_add_selectedindex
steps:
- name: Remove generated code
rmdir: step_07_c_add_selectedindex
- name: Add selectedIndex
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -51,6 +51,8 @@
}
class _MyHomePageState extends State<MyHomePage> {
+ var selectedIndex = 0;
+
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -69,9 +71,11 @@
label: Text('Favorites'),
),
],
- selectedIndex: 0,
+ selectedIndex: selectedIndex,
onDestinationSelected: (value) {
- print('selected: $value');
+ setState(() {
+ selectedIndex = value;
+ });
},
),
),
- name: Copy step_07_c_add_selectedindex
copydir:
from: namer_app
to: step_07_c_add_selectedindex
- name: step_07_d_use_selectedindex
steps:
- name: Remove generated code
rmdir: step_07_d_use_selectedindex
- name: Use selectedIndex
path: namer_app/lib/main.dart
patch-u: |
--- b/namer/step_07_d_use_selectedindex/lib/main.dart
+++ a/namer/step_07_d_use_selectedindex/lib/main.dart
@@ -55,6 +55,16 @@ class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
+ Widget page;
+ switch (selectedIndex) {
+ case 0:
+ page = GeneratorPage();
+ case 1:
+ page = Placeholder();
+ default:
+ throw UnimplementedError('no widget for $selectedIndex');
+ }
+
return Scaffold(
body: Row(
children: [
@@ -82,7 +92,7 @@ class _MyHomePageState extends State<MyHomePage> {
Expanded(
child: Container(
color: Theme.of(context).colorScheme.primaryContainer,
- child: GeneratorPage(),
+ child: page,
),
),
],
- name: Copy step_07_d_use_selectedindex
copydir:
from: namer_app
to: step_07_d_use_selectedindex
- name: step_07_e_add_layout_builder
steps:
- name: Remove generated code
rmdir: step_07_e_add_layout_builder
- name: Add LayoutBuilder
path: namer_app/lib/main.dart
patch-u: |
--- a/namer_app/lib/main.dart
+++ b/namer_app/lib/main.dart
@@ -65,39 +65,41 @@
throw UnimplementedError('no widget for $selectedIndex');
}
- return Scaffold(
- body: Row(
- children: [
- SafeArea(
- child: NavigationRail(
- extended: false,
- destinations: [
- NavigationRailDestination(
- icon: Icon(Icons.home),
- label: Text('Home'),
- ),
- NavigationRailDestination(
- icon: Icon(Icons.favorite),
- label: Text('Favorites'),
- ),
- ],
- selectedIndex: selectedIndex,
- onDestinationSelected: (value) {
- setState(() {
- selectedIndex = value;
- });
- },
+ return LayoutBuilder(builder: (context, constraints) {
+ return Scaffold(
+ body: Row(
+ children: [
+ SafeArea(
+ child: NavigationRail(
+ extended: constraints.maxWidth >= 600,
+ destinations: [
+ NavigationRailDestination(
+ icon: Icon(Icons.home),
+ label: Text('Home'),
+ ),
+ NavigationRailDestination(
+ icon: Icon(Icons.favorite),
+ label: Text('Favorites'),
+ ),
+ ],
+ selectedIndex: selectedIndex,
+ onDestinationSelected: (value) {
+ setState(() {
+ selectedIndex = value;
+ });
+ },
+ ),
),
- ),
- Expanded(
- child: Container(
- color: Theme.of(context).colorScheme.primaryContainer,