-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcodelab_rebuild.yaml
1368 lines (1273 loc) · 49.8 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: Add animation effects to your Flutter app
steps:
- name: step_01
steps:
- name: Remove step_01
rmdir: step_01
- name: Create project
flutter: create --empty quiz
- name: Strip DEVELOPMENT_TEAM
strip-lines-containing: DEVELOPMENT_TEAM =
path: quiz/ios/Runner.xcodeproj/project.pbxproj
- name: Remove README
rm: quiz/README.md
- name: Replace pubspec.yaml
path: quiz/pubspec.yaml
replace-contents: |
name: quiz
description: A quiz app for demonstrating animation effects in Flutter
publish_to: "none"
version: 1.0.0
environment:
sdk: ^3.7.0-0
dependencies:
animations: ^2.0.0
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
flutter:
uses-material-design: true
- name: Replace analysis_options.yaml
path: quiz/analysis_options.yaml
replace-contents: |
include: ../../analysis_options.yaml
analyzer:
errors:
unused_field: ignore
linter:
rules:
annotate_overrides: false
prefer_const_constructors: false
prefer_const_constructors_in_immutables: false
prefer_const_declarations: false
prefer_const_literals_to_create_immutables: false
- name: Add lib/home_screen.dart
path: quiz/lib/home_screen.dart
replace-contents: |
import 'package:flutter/material.dart';
import 'question_screen.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('✏️', style: Theme.of(context).textTheme.displayLarge),
Text(
'Flutter Quiz',
style: Theme.of(context).textTheme.displayLarge!.copyWith(
fontWeight: FontWeight.w700,
color: Theme.of(context).colorScheme.onPrimaryFixedVariant,
),
),
ElevatedButton(
onPressed: () {
// Show the question screen to start the game
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return QuestionScreen();
},
),
);
},
child: Text('New Game'),
),
],
),
),
);
}
}
- name: Add lib/main.dart
path: quiz/lib/main.dart
replace-contents: |
import 'package:flutter/material.dart';
import 'home_screen.dart';
void main() {
runApp(MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
),
home: HomeScreen(),
);
}
}
- name: Add lib/model.dart
path: quiz/lib/model.dart
replace-contents: |
import 'dart:math' as math;
class Question {
final String question;
final List<String> possibleAnswers;
final int correctAnswer;
Question(this.question, this.possibleAnswers, this.correctAnswer);
}
class QuestionBank {
final List<Question> _questions = _createQuestions();
bool get hasNextQuestion => _questions.isNotEmpty;
int get remainingQuestions => _questions.length;
Question? getRandomQuestion() {
if (_questions.isEmpty) {
return null;
}
var i = math.Random().nextInt(_questions.length);
var question = _questions[i];
_questions.removeAt(i);
return question;
}
}
List<Question> _createQuestions() {
return [
Question(
'What class used to create custom explicit animations in Flutter?',
['AnimationController', 'AnimatedWidget', 'AnimatedBuilder', 'Tween'],
0,
),
Question(
'Which widget is used to rebuild its child whenever an animation changes?',
[
'AnimatedContainer',
'AnimatedBuilder',
'AnimatedSwitcher',
'AnimatedOpacity',
],
1,
),
Question(
'What class is used to define the start and end values for an animation?',
['Tween', 'Curve', 'AnimationController', 'AnimatedWidget'],
0,
),
];
}
- name: Add lib/view_model.dart
path: quiz/lib/view_model.dart
replace-contents: |
import 'package:flutter/cupertino.dart';
import 'model.dart';
class QuizViewModel extends ChangeNotifier {
final QuestionBank _questionBank = QuestionBank();
final VoidCallback onGameOver;
late final int totalQuestions;
Question? currentQuestion;
int answeredQuestionCount = 0;
int score = 0;
bool didAnswerQuestion = false;
bool get hasNextQuestion => answeredQuestionCount < totalQuestions;
QuizViewModel({required this.onGameOver}) {
totalQuestions = _questionBank.remainingQuestions;
getNextQuestion();
}
void getNextQuestion() {
if (_questionBank.hasNextQuestion) {
currentQuestion = _questionBank.getRandomQuestion();
answeredQuestionCount++;
}
didAnswerQuestion = false;
notifyListeners();
}
void checkAnswer(int selectedIndex) {
if (!didAnswerQuestion && currentQuestion?.correctAnswer == selectedIndex) {
score++;
}
didAnswerQuestion = true;
if (!_questionBank.hasNextQuestion) {
onGameOver();
}
notifyListeners();
}
}
- name: Add lib/question_screen.dart
path: quiz/lib/question_screen.dart
replace-contents: |
import 'package:flutter/material.dart';
import 'view_model.dart';
class QuestionScreen extends StatefulWidget {
const QuestionScreen({super.key});
@override
State<QuestionScreen> createState() => _QuestionScreenState();
}
class _QuestionScreenState extends State<QuestionScreen> {
late final QuizViewModel viewModel = QuizViewModel(
onGameOver: _handleGameOver,
);
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: viewModel,
builder: (context, child) {
return Scaffold(
appBar: AppBar(
actions: [
TextButton(
onPressed:
viewModel.hasNextQuestion && viewModel.didAnswerQuestion
? () {
viewModel.getNextQuestion();
}
: null,
child: const Text('Next'),
),
],
),
body: Center(
child: Column(
children: [
QuestionCard(question: viewModel.currentQuestion?.question),
Spacer(),
AnswerCards(
onTapped: (index) {
viewModel.checkAnswer(index);
},
answers: viewModel.currentQuestion?.possibleAnswers ?? [],
correctAnswer:
viewModel.didAnswerQuestion
? viewModel.currentQuestion?.correctAnswer
: null,
),
StatusBar(viewModel: viewModel),
],
),
),
);
},
);
}
void _handleGameOver() {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return AlertDialog(
title: Text('Game Over!'),
content: Text('Score: ${viewModel.score}'),
actions: [
TextButton(
onPressed: () {
Navigator.popUntil(context, (route) => route.isFirst);
},
child: Text('OK'),
),
],
);
},
);
}
}
class QuestionCard extends StatelessWidget {
final String? question;
const QuestionCard({required this.question, super.key});
@override
Widget build(BuildContext context) {
return Card(
elevation: 4,
child: Padding(
padding: EdgeInsets.all(16.0),
child: Text(
question ?? '',
style: Theme.of(context).textTheme.displaySmall,
),
),
);
}
}
class AnswerCards extends StatelessWidget {
final List<String> answers;
final ValueChanged<int> onTapped;
final int? correctAnswer;
const AnswerCards({
required this.answers,
required this.onTapped,
required this.correctAnswer,
super.key,
});
@override
Widget build(BuildContext context) {
return GridView.count(
shrinkWrap: true,
crossAxisCount: 2,
childAspectRatio: 5 / 2,
children: List.generate(answers.length, (index) {
var color = Theme.of(context).colorScheme.primaryContainer;
if (correctAnswer == index) {
color = Theme.of(context).colorScheme.tertiaryContainer;
}
return Card.filled(
key: ValueKey(answers[index]),
color: color,
elevation: 2,
margin: EdgeInsets.all(8),
clipBehavior: Clip.hardEdge,
child: InkWell(
onTap: () => onTapped(index),
child: Padding(
padding: EdgeInsets.all(16.0),
child: Center(
child: Text(
answers.length > index ? answers[index] : '',
style: Theme.of(context).textTheme.titleMedium,
overflow: TextOverflow.clip,
),
),
),
),
);
}),
);
}
}
class StatusBar extends StatelessWidget {
final QuizViewModel viewModel;
const StatusBar({required this.viewModel, super.key});
@override
Widget build(BuildContext context) {
return Card(
elevation: 4,
child: Padding(
padding: EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
'Question ${viewModel.answeredQuestionCount} / ${viewModel.totalQuestions}',
style: Theme.of(context).textTheme.titleLarge,
),
Text(
'Score: ${viewModel.score}',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
),
);
}
}
- name: Fetch dependencies
path: quiz
dart: pub get
- name: Copy step_01
copydir:
from: quiz
to: step_01
- name: step_02_a
steps:
- name: Remove step_02_a
rmdir: step_02_a
- name: Add lib/scoreboard.dart
path: quiz/lib/scoreboard.dart
replace-contents: |
import 'package:flutter/material.dart';
class Scoreboard extends StatelessWidget {
final int score;
final int totalQuestions;
const Scoreboard({
super.key,
required this.score,
required this.totalQuestions,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var i = 0; i < totalQuestions; i++)
Icon(
Icons.star,
size: 50,
color:
score < i + 1 ? Colors.grey.shade400 : Colors.yellow.shade700,
),
],
),
);
}
}
- name: Copy step_02_a
copydir:
from: quiz
to: step_02_a
- name: step_02_b
steps:
- name: Remove step_02_b
rmdir: step_02_b
- name: Patch lib/question_screen.dart
path: quiz/lib/question_screen.dart
patch-u: |
--- b/animations/step_02_b/lib/question_screen.dart
+++ a/animations/step_02_b/lib/question_screen.dart
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
+import 'scoreboard.dart';
import 'view_model.dart';
class QuestionScreen extends StatefulWidget {
@@ -160,13 +161,9 @@ class StatusBar extends StatelessWidget {
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
- Text(
- 'Question ${viewModel.answeredQuestionCount} / ${viewModel.totalQuestions}',
- style: Theme.of(context).textTheme.titleLarge,
- ),
- Text(
- 'Score: ${viewModel.score}',
- style: Theme.of(context).textTheme.titleLarge,
+ Scoreboard(
+ score: viewModel.score,
+ totalQuestions: viewModel.totalQuestions,
),
],
),
- name: Copy step_02_b
copydir:
from: quiz
to: step_02_b
- name: step_02_c
steps:
- name: Remove step_02_c
rmdir: step_02_c
- name: Patch lib/scoreboard.dart
path: quiz/lib/scoreboard.dart
patch-u: |
--- b/animations/step_02_c/lib/scoreboard.dart
+++ a/animations/step_02_c/lib/scoreboard.dart
@@ -18,14 +18,31 @@ class Scoreboard extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var i = 0; i < totalQuestions; i++)
- Icon(
- Icons.star,
- size: 50,
- color:
- score < i + 1 ? Colors.grey.shade400 : Colors.yellow.shade700,
- ),
+ AnimatedStar(isActive: score > i),
],
),
);
}
}
+
+class AnimatedStar extends StatelessWidget {
+ final bool isActive;
+ final Duration _duration = const Duration(milliseconds: 1000);
+ final Color _deactivatedColor = Colors.grey.shade400;
+ final Color _activatedColor = Colors.yellow.shade700;
+
+ AnimatedStar({super.key, required this.isActive});
+
+ @override
+ Widget build(BuildContext context) {
+ return AnimatedScale(
+ scale: isActive ? 1.0 : 0.5,
+ duration: _duration,
+ child: Icon(
+ Icons.star,
+ size: 50,
+ color: isActive ? _activatedColor : _deactivatedColor,
+ ),
+ );
+ }
+}
- name: Copy step_02_c
copydir:
from: quiz
to: step_02_c
- name: step_02_d
steps:
- name: Remove step_02_d
rmdir: step_02_d
- name: Patch lib/scoreboard.dart
path: quiz/lib/scoreboard.dart
patch-u: |
--- b/animations/step_02_d/lib/scoreboard.dart
+++ a/animations/step_02_d/lib/scoreboard.dart
@@ -38,10 +38,15 @@ class AnimatedStar extends StatelessWidget {
return AnimatedScale(
scale: isActive ? 1.0 : 0.5,
duration: _duration,
- child: Icon(
- Icons.star,
- size: 50,
- color: isActive ? _activatedColor : _deactivatedColor,
+ child: TweenAnimationBuilder(
+ duration: _duration,
+ tween: ColorTween(
+ begin: _deactivatedColor,
+ end: isActive ? _activatedColor : _deactivatedColor,
+ ),
+ builder: (context, value, child) {
+ return Icon(Icons.star, size: 50, color: value);
+ },
),
);
}
- name: Copy step_02_d
copydir:
from: quiz
to: step_02_d
- name: step_02_e
steps:
- name: Remove step_02_e
rmdir: step_02_e
- name: Patch lib/scoreboard.dart
path: quiz/lib/scoreboard.dart
patch-u: |
--- b/animations/step_02_e/lib/scoreboard.dart
+++ a/animations/step_02_e/lib/scoreboard.dart
@@ -30,6 +30,7 @@ class AnimatedStar extends StatelessWidget {
final Duration _duration = const Duration(milliseconds: 1000);
final Color _deactivatedColor = Colors.grey.shade400;
final Color _activatedColor = Colors.yellow.shade700;
+ final Curve _curve = Curves.elasticOut;
AnimatedStar({super.key, required this.isActive});
@@ -37,8 +38,10 @@ class AnimatedStar extends StatelessWidget {
Widget build(BuildContext context) {
return AnimatedScale(
scale: isActive ? 1.0 : 0.5,
+ curve: _curve,
duration: _duration,
child: TweenAnimationBuilder(
+ curve: _curve,
duration: _duration,
tween: ColorTween(
begin: _deactivatedColor,
- name: Copy step_02_e
copydir:
from: quiz
to: step_02_e
- name: step_03_a
steps:
- name: Remove step_03_a
rmdir: step_03_a
- name: Patch lib/question_screen.dart
path: quiz/lib/question_screen.dart
patch-u: |
--- b/animations/step_03_a/lib/question_screen.dart
+++ a/animations/step_03_a/lib/question_screen.dart
@@ -86,13 +86,17 @@ class QuestionCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
- return Card(
- elevation: 4,
- child: Padding(
- padding: EdgeInsets.all(16.0),
- child: Text(
- question ?? '',
- style: Theme.of(context).textTheme.displaySmall,
+ return AnimatedSwitcher(
+ duration: const Duration(milliseconds: 300),
+ child: Card(
+ key: ValueKey(question),
+ elevation: 4,
+ child: Padding(
+ padding: const EdgeInsets.all(16.0),
+ child: Text(
+ question ?? '',
+ style: Theme.of(context).textTheme.displaySmall,
+ ),
),
),
);
- name: Copy step_03_a
copydir:
from: quiz
to: step_03_a
- name: step_03_b
steps:
- name: Remove step_03_b
rmdir: step_03_b
- name: Patch lib/question_screen.dart
path: quiz/lib/question_screen.dart
patch-u: |
--- b/animations/step_03_b/lib/question_screen.dart
+++ a/animations/step_03_b/lib/question_screen.dart
@@ -87,6 +87,16 @@ class QuestionCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AnimatedSwitcher(
+ transitionBuilder: (child, animation) {
+ final curveAnimation = CurveTween(
+ curve: Curves.easeInCubic,
+ ).animate(animation);
+ final offsetAnimation = Tween<Offset>(
+ begin: Offset(-0.1, 0.0),
+ end: Offset.zero,
+ ).animate(curveAnimation);
+ return SlideTransition(position: offsetAnimation, child: child);
+ },
duration: const Duration(milliseconds: 300),
child: Card(
key: ValueKey(question),
- name: Copy step_03_b
copydir:
from: quiz
to: step_03_b
- name: step_03_c
steps:
- name: Remove step_03_c
rmdir: step_03_c
- name: Patch lib/question_screen.dart
path: quiz/lib/question_screen.dart
patch-u: |
--- b/animations/step_03_c/lib/question_screen.dart
+++ a/animations/step_03_c/lib/question_screen.dart
@@ -95,7 +95,11 @@ class QuestionCard extends StatelessWidget {
begin: Offset(-0.1, 0.0),
end: Offset.zero,
).animate(curveAnimation);
- return SlideTransition(position: offsetAnimation, child: child);
+ final fadeInAnimation = curveAnimation;
+ return FadeTransition(
+ opacity: fadeInAnimation,
+ child: SlideTransition(position: offsetAnimation, child: child),
+ );
},
duration: const Duration(milliseconds: 300),
child: Card(
- name: Copy step_03_c
copydir:
from: quiz
to: step_03_c
- name: step_03_d
steps:
- name: Remove step_03_d
rmdir: step_03_d
- name: Patch lib/question_screen.dart
path: quiz/lib/question_screen.dart
patch-u: |
--- b/animations/step_03_d/lib/question_screen.dart
+++ a/animations/step_03_d/lib/question_screen.dart
@@ -87,6 +87,15 @@ class QuestionCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AnimatedSwitcher(
+ layoutBuilder: (currentChild, previousChildren) {
+ return Stack(
+ alignment: Alignment.topCenter,
+ children: <Widget>[
+ ...previousChildren,
+ if (currentChild != null) currentChild,
+ ],
+ );
+ },
transitionBuilder: (child, animation) {
final curveAnimation = CurveTween(
curve: Curves.easeInCubic,
- name: Copy step_03_d
copydir:
from: quiz
to: step_03_d
- name: step_04_a
steps:
- name: Remove step_04_a
rmdir: step_04_a
- name: Add lib/flip_effect.dart
path: quiz/lib/flip_effect.dart
replace-contents: |
import 'dart:math' as math;
import 'package:flutter/widgets.dart';
class CardFlipEffect extends StatefulWidget {
final Widget child;
final Duration duration;
const CardFlipEffect({
super.key,
required this.child,
required this.duration,
});
@override
State<CardFlipEffect> createState() => _CardFlipEffectState();
}
class _CardFlipEffectState extends State<CardFlipEffect>
with SingleTickerProviderStateMixin {
late final AnimationController _animationController;
Widget? _previousChild;
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
duration: widget.duration,
);
_animationController.addListener(() {
if (_animationController.value == 1) {
_animationController.reset();
}
});
}
@override
void didUpdateWidget(covariant CardFlipEffect oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.child.key != oldWidget.child.key) {
_handleChildChanged(widget.child, oldWidget.child);
}
}
void _handleChildChanged(Widget newChild, Widget previousChild) {
_previousChild = previousChild;
_animationController.forward();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animationController,
builder: (context, child) {
return Transform(
alignment: Alignment.center,
transform:
Matrix4.identity()..rotateX(_animationController.value * math.pi),
child:
_animationController.isAnimating
? _animationController.value < 0.5
? _previousChild
: Transform.flip(flipY: true, child: child)
: child,
);
},
child: widget.child,
);
}
}
- name: Patch lib/question_screen.dart
path: quiz/lib/question_screen.dart
patch-u: |
--- b/animations/step_04_a/lib/question_screen.dart
+++ a/animations/step_04_a/lib/question_screen.dart
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
+import 'flip_effect.dart';
import 'scoreboard.dart';
import 'view_model.dart';
@@ -149,21 +150,24 @@ class AnswerCards extends StatelessWidget {
if (correctAnswer == index) {
color = Theme.of(context).colorScheme.tertiaryContainer;
}
- return Card.filled(
- key: ValueKey(answers[index]),
- color: color,
- elevation: 2,
- margin: EdgeInsets.all(8),
- clipBehavior: Clip.hardEdge,
- child: InkWell(
- onTap: () => onTapped(index),
- child: Padding(
- padding: EdgeInsets.all(16.0),
- child: Center(
- child: Text(
- answers.length > index ? answers[index] : '',
- style: Theme.of(context).textTheme.titleMedium,
- overflow: TextOverflow.clip,
+ return CardFlipEffect(
+ duration: const Duration(milliseconds: 300),
+ child: Card.filled(
+ key: ValueKey(answers[index]),
+ color: color,
+ elevation: 2,
+ margin: EdgeInsets.all(8),
+ clipBehavior: Clip.hardEdge,
+ child: InkWell(
+ onTap: () => onTapped(index),
+ child: Padding(
+ padding: EdgeInsets.all(16.0),
+ child: Center(
+ child: Text(
+ answers.length > index ? answers[index] : '',
+ style: Theme.of(context).textTheme.titleMedium,
+ overflow: TextOverflow.clip,
+ ),
),
),
),
- name: Copy step_04_a
copydir:
from: quiz
to: step_04_a
- name: step_04_b
steps:
- name: Remove step_04_b
rmdir: step_04_b
- name: Patch lib/flp_effect.dart
path: quiz/lib/flip_effect.dart
patch-u: |
--- b/animations/step_04_b/lib/flip_effect.dart
+++ a/animations/step_04_b/lib/flip_effect.dart
@@ -5,11 +5,13 @@ import 'package:flutter/widgets.dart';
class CardFlipEffect extends StatefulWidget {
final Widget child;
final Duration duration;
+ final double delayAmount;
const CardFlipEffect({
super.key,
required this.child,
required this.duration,
+ required this.delayAmount,
});
@override
- name: Patch lib/question_screen.dart
path: quiz/lib/question_screen.dart
patch-u: |
--- b/animations/step_04_b/lib/question_screen.dart
+++ a/animations/step_04_b/lib/question_screen.dart
@@ -151,6 +151,7 @@ class AnswerCards extends StatelessWidget {
color = Theme.of(context).colorScheme.tertiaryContainer;
}
return CardFlipEffect(
+ delayAmount: index.toDouble() / 2,
duration: const Duration(milliseconds: 300),
child: Card.filled(
key: ValueKey(answers[index]),
- name: Copy step_04_b
copydir:
from: quiz
to: step_04_b
- name: step_04_c
steps:
- name: Remove step_04_c
rmdir: step_04_c
- name: Patch lib/flip_effect.dart
path: quiz/lib/flip_effect.dart
patch-u: |
--- b/animations/step_04_c/lib/flip_effect.dart
+++ a/animations/step_04_c/lib/flip_effect.dart
@@ -22,6 +22,7 @@ class _CardFlipEffectState extends State<CardFlipEffect>
with SingleTickerProviderStateMixin {
late final AnimationController _animationController;
Widget? _previousChild;
+ late final Animation<double> _animationWithDelay;
@override
void initState() {
@@ -29,7 +30,7 @@ class _CardFlipEffectState extends State<CardFlipEffect>
_animationController = AnimationController(
vsync: this,
- duration: widget.duration,
+ duration: widget.duration * (widget.delayAmount + 1),
);
_animationController.addListener(() {
@@ -37,6 +38,15 @@ class _CardFlipEffectState extends State<CardFlipEffect>
_animationController.reset();
}
});
+
+ _animationWithDelay = TweenSequence<double>([
+ if (widget.delayAmount > 0)
+ TweenSequenceItem(
+ tween: ConstantTween<double>(0.0),
+ weight: widget.delayAmount,
+ ),
+ TweenSequenceItem(tween: Tween(begin: 0.0, end: 1.0), weight: 1.0),
+ ]).animate(_animationController);
}
@override
- name: Copy step_04_c
copydir:
from: quiz
to: step_04_c
- name: step_04_d
steps:
- name: Remove step_04_d
rmdir: step_04_d
- name: Patch lib/flip_effect.dart
path: quiz/lib/flip_effect.dart
patch-u: |
--- b/animations/step_04_d/lib/flip_effect.dart
+++ a/animations/step_04_d/lib/flip_effect.dart
@@ -66,15 +66,15 @@ class _CardFlipEffectState extends State<CardFlipEffect>
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
- animation: _animationController,
+ animation: _animationWithDelay,
builder: (context, child) {
return Transform(
alignment: Alignment.center,
transform:
- Matrix4.identity()..rotateX(_animationController.value * math.pi),
+ Matrix4.identity()..rotateX(_animationWithDelay.value * math.pi),
child:
_animationController.isAnimating
- ? _animationController.value < 0.5
+ ? _animationWithDelay.value < 0.5
? _previousChild
: Transform.flip(flipY: true, child: child)
: child,
- name: Copy step_04_d
copydir:
from: quiz
to: step_04_d
- name: step_05_a
steps:
- name: Remove step_05_a
rmdir: step_05_a
- name: Patch lib/home_screen.dart
path: quiz/lib/home_screen.dart