[英]Custom widget constructor in Flutter
In my Flutter course we are building a custom widget, a reusable card as below.在我的 Flutter 课程中,我们正在构建一个自定义小部件,一个可重复使用的卡片,如下所示。
I wanted to know why we are defining final Color color after the constructor ReusableCard({@required this.colour}) and not before?我想知道为什么我们在构造函数ReusableCard({@required this.colour})之后而不是之前定义最终颜色颜色?
class ReusableCard extends StatelessWidget {
ReusableCard({@required this.colour});
final Color colour;
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0),
),
height: 200,
width: 170,
);
}
}
In fact, is recommended to sort constructor declarations before other members.事实上,建议将构造函数声明排在其他成员之前。
https://dart-lang.github.io/linter/lints/sort_constructors_first.html https://dart-lang.github.io/linter/lints/sort_constructors_first.html
It really all boils down to preference.这真的归结为偏好。 There isn't a strict rule that tells you that the variable and constructor has to be defined in a certain order.
没有严格的规则告诉您必须按特定顺序定义变量和构造函数。 It doesn't really matter even if your code looks like this:
即使您的代码看起来像这样也没有关系:
class ReusableCard extends StatelessWidget {
final Color colour;
ReusableCard({@required this.colour});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0),
),
height: 200,
width: 170,
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.