【发布时间】: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.
【问题讨论】: