【问题标题】:Flutter useAnimationController hook doesn't rebuild widgetFlutter useAnimationController 钩子不会重建小部件
【发布时间】:2021-03-03 12:10:21
【问题描述】:

我正在使用颤振钩子包来为屏幕上的元素设置动画,但显然我做错了,因为元素没有重建为动画。这是我的代码。

class Ball extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final xController = useAnimationController(
      duration: Duration(seconds: 3),
      lowerBound: 0,
      upperBound: 2,
      initialValue: 1,
    );

    final yController = useAnimationController(
      duration: Duration(seconds: 3),
      lowerBound: 0,
      upperBound: 2,
      initialValue: 1,
    );

    useEffect(() {
      xController.repeat(reverse: true);
      yController.repeat(reverse: true);

      return () {};
    });

    return Align(
      alignment: Alignment(
        xController.value - 1,
        yController.value - 1,
      ),
      child: Container(
        width: 12,
        height: 12,
        decoration: BoxDecoration(
          color: Colors.grey[900],
          shape: BoxShape.circle,
        ),
      ),
    );
  }
}

渲染时,此小部件位于堆栈内。在运行时或构建时都不会引发错误。

【问题讨论】:

    标签: flutter dart flutter-dependencies flutter-animation flutter-hooks


    【解决方案1】:

    对于将来查看此内容的任何人,您必须这样做

    final xController = useAnimationController({ ... })
    useAnimation(xController)
    

    【讨论】:

      猜你喜欢
      • 2019-12-09
      • 2021-10-22
      • 2020-07-25
      • 2021-03-10
      • 2021-10-29
      • 2019-12-09
      • 2019-08-23
      • 2018-11-01
      • 2019-08-09
      相关资源
      最近更新 更多