在有状态小部件颤振中传递密钥时出错
Getting error while passing key in stateful widget flutter
我正在尝试为 AnimatedContainer 的宽度设置动画。为此,我在外部文件中使用了一个有状态的小部件,这里是 class
的代码
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(),
),
)
],
);
}
}
但我收到错误提示“getter 未找到 'key'”
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);
我正在尝试为 AnimatedContainer 的宽度设置动画。为此,我在外部文件中使用了一个有状态的小部件,这里是 class
的代码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(),
),
)
],
);
}
}
但我收到错误提示“getter 未找到 'key'”
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);