[英](flutter) How can I get argument in class?
I'm new to flutter, and I heard that creating widgets as classes is better.我是 flutter 的新手,我听说将小部件创建为类更好。 So I'm trying to change functions to classes.所以我试图将函数更改为类。
Widget Head(Size size){
return Container(
height: size.height*0.3 + 100,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xff3D1472),
Color(0xff771887),
]
)
),
);
}
the code above is one of the functions I want to change to stateless class.上面的代码是我想更改为无状态类的功能之一。 And I tried like this,我像这样试过,
class Head extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
height: size.height*0.3 + 100,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xff3D1472),
Color(0xff771887),
]
)
),
);
}
}
But I still don't know where I should get that 'size'.但我仍然不知道我应该从哪里得到那个“大小”。 How can I do that?我怎样才能做到这一点? Thank you for reading, and I'll wait for your advice!感谢您的阅读,我会等待您的建议!
You need to pass the arguments in the constructor, for example:您需要在构造函数中传递参数,例如:
class Head extends StatelessWidget {
Head({this.size});
final int size;
@override
Widget build(BuildContext context) {
return Container(
height: size.height*0.3 + 100,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xff3D1472),
Color(0xff771887),
]
)
),
);
}
}
And then when you call it, you can do:然后当你调用它时,你可以这样做:
Head(size : 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.