如何让 Transform.translate 发挥作用 Flutter

Kdo*_*don 5 animation flutter

我使用动画控制器在Flutter中创建了一个交错动画来运行给定的小部件。属性(缩放、旋转)在每个间隔都完美地呈现动画。

\n

但是当我去添加翻译属性时,它会抛出“未实现的错误”。

\n

我已经用不同的值等进行了重新设计,但无法让它工作。

\n

我什至尝试单独运行 .translate 属性,但仍然失败。

\n

有谁知道我做错了什么?

\n

谢谢!

\n
class _AnimatedFlyInShakeState extends State<AnimatedFlyInShake>\n    with SingleTickerProviderStateMixin {\n  late AnimationController _controller;\n  late Animation<double> _rotate, _scale, _scale2;\n  late Animation<Offset> _move;\n\n  @override\n  void initState() {\n    _controller = AnimationController(\n      duration: Duration(milliseconds: 700),\n      vsync: this,\n    );\n\n    _rotate = Tween<double>(begin: 0.20, end: 1.0).animate(CurvedAnimation(\n        parent: _controller,\n        curve: Interval(0.0, 0.70, curve: Curves.easeInOut)));\n\n    _scale = Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(\n        parent: _controller,\n        curve: Interval(0.0, 0.60, curve: Curves.elasticInOut)));\n\n    _scale2 = Tween<double>(begin: 1.0, end: 0.5).animate(CurvedAnimation(\n        parent: _controller,\n        curve: Interval(0.60, 0.70, curve: Curves.easeOut)));\n\n    _move = Tween<Offset>(begin: Offset(0,0), end: Offset(0,0.50)).animate(CurvedAnimation(\n        parent: _controller,\n        curve: Interval(0.80, 1.0, curve: Curves.easeInOut)));\n\n_controller.forward();\n    super.initState();\n  }\n\n \n  void dispose() {\n    _controller.dispose();\n    super.dispose();\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    return AnimatedBuilder(\n        animation: _controller,\n        builder: (context, child) =>\n            Transform(\n              transform: Matrix4.identity()\n                ..scale(_scale.value)\n                ..scale(_scale2.value)\n                ..rotateZ(_rotate.value * pi * 2)\n                ..translate(_move.value), //THIS PROPERTY THROWS AN ERROR\n              child: widget._widget,\n              alignment: Alignment.center,\n            ));\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

错误信息:

\n
; ======== Exception caught by widgets library =======================================================\nThe following UnimplementedError was thrown building AnimatedBuilder(animation: AnimationController#cf616(\xe2\x8f\xae 0.000; paused), dirty, state: _AnimatedState#5c85d):\nUnimplementedError\n\n
Run Code Online (Sandbox Code Playgroud)\n

小智 8

Translate 仅适用于 double。

您需要确保 _move.value 是双精度的。或者使用 ..translate(_move.value.toDouble()),

我的例子:

transform: Matrix4.rotationZ(-8 * pi / 180)..translate(-10.0),  // Works just fine.
transform: Matrix4.rotationZ(-8 * pi / 180)..translate(-10),    // Throws unimplemented error.
Run Code Online (Sandbox Code Playgroud)