【问题标题】:Flutter 2.2.3 : The argument type 'Animation<dynamic>' can't be assigned to the parameter type 'Animation<double>'Flutter 2.2.3:参数类型'Animation<dynamic>'不能分配给参数类型'Animation<double>'
【发布时间】:2021-07-08 14:39:22
【问题描述】:

我试图从这里传递列表位置并得到这个错误:参数类型“动画”​​不能分配给参数类型“动画”​​。我按照 youtuber 的步骤进行操作,可以在此处查看完整代码:https://github.com/codespiration/petadoption/blob/master/menu_frame.dart。非常感谢你们的帮助

> @override   void initState() {
>     super.initState();
>     _animationController = AnimationController(vsync: this, duration: duration);
>     scaleAnimation =
>         Tween<double>(begin: 1.0, end: 0.6).animate(_animationController);
>     smallerScaleAnimation =
>         Tween<double>(begin: 1.0, end: 0.5).animate(_animationController);
> 
>     scaleAnimations = [
>       Tween<double>(begin: 1.0, end: 0.7).animate(_animationController),
>       Tween<double>(begin: 1.0, end: 0.6).animate(_animationController),
>       Tween<double>(begin: 1.0, end: 0.5).animate(_animationController),
>     ];
>     _animationController.forward();   }
> 
> Widget buildScreenStack(int position) {
>     final deviceWidth = MediaQuery.of(context).size.width;
>     return AnimatedPositioned(
>       duration: duration,
>       top: 0,
>       bottom: 0,
>       left: menuOpen ? deviceWidth * 0.35 : 0.0,
>       right: menuOpen ? deviceWidth * -0.65 : 0.0,
>       child: ScaleTransition(
>         scale: scaleAnimations[position],
>         child: GestureDetector(
>           onTap: () {
>             if (menuOpen) {
>               setState(() {
>                 menuOpen = false;
>                 _animationController.reverse();
>               });
>             }
>           },
>           child: AbsorbPointer(
>             absorbing: menuOpen,
>             child: Stack(
>               children: <Widget>[
>                 Material(
>                   animationDuration: duration,
>                   borderRadius: BorderRadius.circular(menuOpen ? 30.0 : 0.0),
>                   child: screens[position],
>                 ),
>               ],
>             ),
>           ),
>         ),
>       ),
>     );   }
> 
>   @override   Widget build(BuildContext context) {
>     final deviceWidth = MediaQuery.of(context).size.width;
>     return Stack(
>       children: finalStack(),
>     );   } }

【问题讨论】:

    标签: flutter dart animationcontroller


    【解决方案1】:

    scaleAnimations 字段被声明为List&lt;Animation&gt;,并且由于Animation 是通用的,这隐含地与List&lt;Animation&lt;dynamic&gt;&gt; 相同。因此运行时会将列表中的任何元素解释为Animation&lt;dynamic&gt;,即使它们实际上具有Animation&lt;double&gt; 类型。

    要解决这个问题,您只需要更改字段声明:

    List<Animation<double>> scaleAnimations;
    

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 2020-10-25
      • 1970-01-01
      • 2021-12-30
      • 2021-08-03
      • 1970-01-01
      • 2021-05-25
      • 2021-08-29
      • 2021-10-02
      相关资源
      最近更新 更多