-
Notifications
You must be signed in to change notification settings - Fork 4
/
animation_controller_set_state_enhanced2_page.dart
118 lines (105 loc) · 3.12 KB
/
animation_controller_set_state_enhanced2_page.dart
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
import 'package:animations_app/pages/custom/widget/ghost_fade_tween.dart';
import 'package:animations_app/pages/custom/widget/switch_string_tween.dart';
import 'package:animations_app/widget/app_scaffold.dart';
import 'package:flutter/material.dart';
class AnimationControllerSetStateEnhanced2Page extends StatefulWidget {
const AnimationControllerSetStateEnhanced2Page({super.key});
static const routeName = 'AnimationControllerSetStateEnhanced2';
@override
State<AnimationControllerSetStateEnhanced2Page> createState() =>
_AnimationControllerSetStateEnhanced2PageState();
}
class _AnimationControllerSetStateEnhanced2PageState
extends State<AnimationControllerSetStateEnhanced2Page>
with SingleTickerProviderStateMixin {
static const _texts = [
'First',
'Second',
'Third',
];
static const _colors = [
Colors.black,
Colors.blue,
Colors.red,
];
var _index = 0;
String get _currentText => _texts[_index % 3];
Color get _currentColor => _colors[_index % 3];
late GhostFadeTween _colorTween = GhostFadeTween(
begin: _currentColor,
end: _currentColor,
);
late SwitchStringTween _stringTween = SwitchStringTween(
begin: _currentText,
end: _currentText,
);
@override
Widget build(BuildContext context) {
return AppScaffold(
title: 'AnimationController - SetState (Enhanced)',
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_index++;
_colorTween = GhostFadeTween(
begin: _colorTween.end,
end: _currentColor,
);
_stringTween = SwitchStringTween(
begin: _stringTween.end,
end: _currentText,
);
});
},
child: const Icon(Icons.refresh),
),
child: Center(
child: _MyAnimatedWidget(
stringTween: _stringTween,
colorTween: _colorTween,
),
),
);
}
}
class _MyAnimatedWidget extends StatefulWidget {
const _MyAnimatedWidget({
required this.stringTween,
required this.colorTween,
});
final SwitchStringTween stringTween;
final GhostFadeTween colorTween;
@override
State<_MyAnimatedWidget> createState() => __MyAnimatedWidgetState();
}
class __MyAnimatedWidgetState extends State<_MyAnimatedWidget>
with SingleTickerProviderStateMixin {
late final AnimationController _animation = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1000),
)..addListener(() {
setState(() {});
});
@override
void dispose() {
_animation.dispose();
super.dispose();
}
@override
void didUpdateWidget(_MyAnimatedWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.colorTween != widget.colorTween &&
oldWidget.stringTween != widget.stringTween) {
_animation.forward(from: 0);
}
}
@override
Widget build(BuildContext context) {
return Text(
widget.stringTween.evaluate(_animation),
style: Theme.of(context).textTheme.headline6!.copyWith(
color: widget.colorTween.evaluate(_animation),
),
);
}
}