initState method
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).
If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:
- In initState, subscribe to the object.
- In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
- In dispose, unsubscribe from the object.
You should not use BuildContext.dependOnInheritedWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.dependOnInheritedWidgetOfExactType can be used there.
Implementations of this method should start with a call to the inherited
method, as in super.initState().
Implementation
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 500),
vsync: this,
);
switch (widget.type) {
case 0:
// 无动画,不需要初始化任何动画
break;
case 1:
// 弹跳动画 - 垂直方向
_translateAnimation =
TweenSequence<Offset>([
TweenSequenceItem(
tween: Tween<Offset>(
begin: Offset.zero,
end: const Offset(0, -10),
),
weight: 1.0,
),
TweenSequenceItem(
tween: Tween<Offset>(
begin: const Offset(0, -10),
end: Offset.zero,
),
weight: 1.0,
),
]).animate(
CurvedAnimation(parent: _controller, curve: Curves.bounceOut),
);
break;
case 2:
// 摇摆动画 - 水平方向
// 使用sin函数实现平滑的左右摇摆
break;
case 3:
// 脉冲动画 - 缩放效果
_pulseAnimation =
TweenSequence<double>([
TweenSequenceItem(
tween: Tween<double>(begin: 1.0, end: 1.2),
weight: 1.0,
),
TweenSequenceItem(
tween: Tween<double>(begin: 1.2, end: 0.8),
weight: 1.0,
),
TweenSequenceItem(
tween: Tween<double>(begin: 0.8, end: 1.0),
weight: 1.0,
),
]).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
);
break;
case 4:
// 抖动动画 - 旋转抖动
_shakeAnimation =
TweenSequence<double>([
TweenSequenceItem(
tween: Tween<double>(begin: 0, end: 0.1),
weight: 1.0,
),
TweenSequenceItem(
tween: Tween<double>(begin: 0.1, end: -0.1),
weight: 2.0,
),
TweenSequenceItem(
tween: Tween<double>(begin: -0.1, end: 0.1),
weight: 2.0,
),
TweenSequenceItem(
tween: Tween<double>(begin: 0.1, end: 0),
weight: 1.0,
),
]).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
);
break;
case 5:
// 缩放动画
_scaleAnimation =
TweenSequence<double>([
TweenSequenceItem(
tween: Tween<double>(begin: 1.0, end: 0.8),
weight: 1.0,
),
TweenSequenceItem(
tween: Tween<double>(begin: 0.8, end: 1.0),
weight: 1.0,
),
]).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
);
break;
case 6:
// 组合动画 - 缩放+旋转
_scaleAnimation =
TweenSequence<double>([
TweenSequenceItem(
tween: Tween<double>(begin: 1.0, end: 0.8),
weight: 1.0,
),
TweenSequenceItem(
tween: Tween<double>(begin: 0.8, end: 1.0),
weight: 1.0,
),
]).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
);
_rotateAnimation =
TweenSequence<double>([
TweenSequenceItem(
tween: Tween<double>(begin: 0, end: 0.1),
weight: 1.0,
),
TweenSequenceItem(
tween: Tween<double>(begin: 0.1, end: 0),
weight: 1.0,
),
]).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
);
break;
}
}