欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Flutter The argument type ‘Listenable‘ can‘t be assigned to the parameter type ‘Animation<double>

程序员文章站 2024-01-19 13:38:52
...

在该build方法的第一行,生成此分析器警告

A value of type 'Listenable' can't be assigned to a variable of type 'Animation<double>'.
Try changing the type of the variable, or casting the right-hand type to 'Animation<double>'.
dart(invalid_assignment)

应该修改为:

final Animation<double> animation = listenable as Animation<double>;

有bug的代码:

class AnimatedLogo extends AnimatedWidget {
   AnimatedLogo({Key? key, required Animation<double> animation})
      : super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    return new Center(
        child: new Container(
          margin: new EdgeInsets.symmetric(vertical: 10.0),
          height: animation.value,
          width: animation.value,
          child: new FlutterLogo(),
        )
    );
  }
}

修改后的代码:

class AnimatedLogo extends AnimatedWidget {
   AnimatedLogo({Key? key, required Animation<double> animation})
      : super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable as Animation<double>;
    return new Center(
        child: new Container(
          margin: new EdgeInsets.symmetric(vertical: 10.0),
          height: animation.value,
          width: animation.value,
          child: new FlutterLogo(),
        )
    );
  }
}