如何解决在有状态小部件颤动中传递密钥时出错
我正在尝试为 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.
解决方法
构造函数中有一个冒号需要删除。
const AnimatedContainerWidget({
Key key,// Replace "Key: key" with "Key key"
}) : super(key: key);
,
这样写
AnimatedContainerWidget({Key key,this.image}) : super(key: key);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。