flutter - Flutter:图像调整大小动画滞后
问题描述
我正在尝试为 AppBar 制作动画,并且图像正在缩小。
你可以在这个 gif 中看到我在说什么:
由于是gif,所以整个动画看起来有些卡顿,但实际上只有图像。
代码
class _AuthAppBarState extends State<AuthAppBar> with TickerProviderStateMixin {
double _leftPoint = 0.0;
double _rightPoint = 70.0;
String _lastTriggeredType = '';
late AnimationController _animationController;
late Animation<double> leftPointAnimation;
late Animation<double> rightPointAnimation;
late Animation<double> titleAnimation;
void triggerAnimation(bool trigger) {
final double prevLeft = _leftPoint;
final double prevRight = _rightPoint;
final double prevTitleHeight = _lastTriggeredType == 'equal' ? 40 : 70;
final double newLeft = leftPoints[widget.type];
final double newRight = rightPoints[widget.type];
final double newTitleHeight = widget.type == 'equal' ? 40 : 70;
setState(() {
leftPointAnimation = Tween<double>(begin: prevLeft, end: newLeft)
.animate(_animationController);
rightPointAnimation = Tween<double>(begin: prevRight, end: newRight)
.animate(_animationController);
titleAnimation =
Tween<double>(begin: prevTitleHeight, end: newTitleHeight)
.animate(_animationController);
_rightPoint = rightPoints[widget.type];
_leftPoint = leftPoints[widget.type];
_lastTriggeredType = widget.type;
});
if (trigger && _animationController != null)
_animationController.forward(from: 0);
}
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 300),
);
triggerAnimation(false);
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final size = MediaQuery.of(context).size;
if (_animationController == null || titleAnimation == null) {
return Container();
}
if (_lastTriggeredType != widget.type) triggerAnimation(true);
// * ANIMATED CENTERED TITLE * \\
final Center centeredTitle = Center(
child: AnimatedBuilder(
animation: _animationController,
builder: (ctx, _) {
return SvgPicture.asset(
images['title'],
height: titleAnimation.value,
);
},
),
);
// * MAIN CONTAINER - defines size and color of clip path * \\
final container = Container(
color: theme.primaryColor,
child: AnimatedSize(
curve: Curves.decelerate,
duration: const Duration(seconds: 1),
child: Container(
height: widget.type == 'equal' ? 50 : size.height / 3,
child: centeredTitle,
),
vsync: this,
),
);
return AnimatedBuilder(
builder: (context, anim) {
return ClipPath(
clipper: AuthAppBarPath(
leftPoint: leftPointAnimation.value,
rightPoint: rightPointAnimation.value,
),
child: container,
);
},
animation: _animationController,
);
}
}
我尝试使用 AnimatedSize 和 AnimatedContainer。它的工作原理相同。
非常感谢您一直以来的帮助和帮助!!
解决方案
推荐阅读
- html - Rails 和 Font Awesome 不起作用?图标不显示
- vue.js - 在 vue 中更新组件中的初始值
- python-3.x - EOFError:读取一行时出现EOF
- python - 使用 np.logical_and 后内核死机
- java - For循环正在打印出多个打印语句
- android - 如何将 Json 字符串填充到 Android 微调器中
- google-api - 用于组织的 Google 日历 API
- firebase - SwiftUI Firebase - 如何查询文档然后更新?
- sql - SQL Server 替换列值中的不同字符
- python - 插入数据库后,字符串 'ə' 成为 SQL Server 的问号