【问题标题】:Getting error while passing key in stateful widget flutter [closed]在有状态小部件颤动中传递密钥时出错[关闭]
【发布时间】:2021-05-09 17:13:09
【问题描述】:

我正在尝试为 AnimatedContainer 的宽度设置动画。为此,我在外部文件中使用了一个有状态的小部件,这里是这个类的代码

class AnimatedContainerWidget extends StatefulWidget {
const AnimatedContainerWidget({
    Key: key,
  }) : super(key: key);
  @override
  _AnimatedContainerWidgetState createState() =>
      _AnimatedContainerWidgetState();
}

class _AnimatedContainerWidgetState extends State<AnimatedContainerWidget> {
  double _height = 100.0;
  double _width = 100.0;
  _increaseWidth() {
    setState(() {
      _width = _width >= 300 ? 100.0 : _width += 50.0;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        AnimatedContainer(
          duration: Duration(milliseconds: 500),
          curve: Curves.elasticInOut,
          color: Colors.amber,
          height: _height,
          width: _width,
          child: FlatButton(
            child: Text('Tap to grow width\n $_width'),
            onPressed: _increaseWidth(),
          ),
        )
      ],
    );
  }
}

但我收到错误消息“找不到'key'的getter”

lib/widgets/animated_container.dart:5:10: Error: Getter not found: 'key'.
    Key: key,                                                           
         ^^^                                                            
lib/widgets/animated_container.dart:6:19: Error: Getter not found: 'key'.
  }) : super(key: key);                                                 
                  ^^^                                                   
                                                                        
                                                                        
FAILURE: Build failed with an exception. 

【问题讨论】:

    标签: flutter statefulwidget


    【解决方案1】:

    构造函数中有一个冒号需要去掉。

    const AnimatedContainerWidget({
        Key key, // Replace "Key: key" with "Key key"
    }) : super(key: key);
    

    【讨论】:

      【解决方案2】:

      这样写

       AnimatedContainerWidget({Key key, this.image}) : super(key: key);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-06
        • 2019-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-29
        • 2021-08-17
        • 1970-01-01
        相关资源
        最近更新 更多