在flutter docs中,有一个无状态小部件子类的示例代码,如下所示:
class GreenFrog extends StatelessWidget {
const GreenFrog({ Key key }) : super(key: key);
@override
Widget build(BuildContext context) {
return new Container(color: const Color(0xFF2DBD3A));
}
}
Run Code Online (Sandbox Code Playgroud)
还有这个
class Frog extends StatelessWidget {
const Frog({
Key key,
this.color: const Color(0xFF2DBD3A),
this.child,
}) : super(key: key);
final Color color;
final Widget child;
@override
Widget build(BuildContext context) {
return new Container(color: color, child: child);
}
}
Run Code Online (Sandbox Code Playgroud)
什么是关键,什么时候应该使用这个超级构造函数?好像你有自己的构造函数,你必须有{Key key}为什么?我已经看到了其他没有使用super关键字的例子,所以这就是我的困惑所在.
我真的不明白是如何@required工作的。例如,我见过这个代码:
class Test{
final String x;
Test({
@required this.x
});
factory Test.initial(){
return Test(x: "");
}
}
Run Code Online (Sandbox Code Playgroud)
但是@required这里应该怎么做呢?似乎它使可选参数成为非可选参数。