我想将嵌套的Padding / Container / Padding结构包装在它自己的Widget中,以便我可以重用它:
我通过以下方式声明我的窗口小部件:
class UpperWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: this.width,
child: Padding(padding: const EdgeInsets.all(8.0), child: child)),
);
}
final double width;
final Widget child;
const UpperWidget(Key key, this.width, this.child) : super(key: key);
}
我想在以下地方使用它:
class LowerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return UpperWidget(
width:50,
child: Text("Hello World");
}),
);
}
}
当我将鼠标悬停在宽度上时,Android Studio会向我显示错误:
3 required argument(s) expected but 0 found
Try adding additional required arguments.
The named parameter 'width' isn't defined.
Try correcting the name to an existing parameter, or defining a parameter with this name.
我该怎么办,以便width
和child
被正确地视为已命名参数?
{}
class UpperWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: this.width,
child: Padding(padding: const EdgeInsets.all(8.0), child: child)),
);
}
final double width;
final Widget child;
const UpperWidget({Key key, this.width, this.child}) : super(key: key);
}
如果还需要它们,则可以包含@required
注释:
const UpperWidget({Key key, @required this.width, @required this.child}) : super(key: key);