[英]Flutter - How to change the Color class' value that placed inside a custom widget?
I'd like to create a widget that I'll use it a lot of times, so I don't have to create it manully.我想创建一个我会经常使用它的小部件,所以我不必手动创建它。
here's what I've built:这是我建立的:
Widget keyWidget(String keyNumber, String keyName) {
return Container(
width: 100.0,
height: 50.0,
color: Colors.red, //How I change the color's value? (Colors.{this_one})?
child: FlatButton(
onPressed: () {
final player = AudioCache();
player.play('key$keyNumber.mp3');
},
child: Text('$keyName'),
),
);
}
How do I change the color's value ( color: Colors.{this_one},
) so I could set it in the keyWidget
properties?如何更改颜色的值(
color: Colors.{this_one},
)以便我可以在keyWidget
属性中设置它?
You can pass your Color
as argument in your keyWidget()
method您可以在
keyWidget()
方法中将Color
作为参数传递
Try this way试试这个方法
Widget keyWidget(String keyNumber, String keyName,Color mColor) {
return Container(
width: 100.0,
height: 50.0,
color: mColor, //How I change the color's value? (Colors.{this_one})?
child: FlatButton(
onPressed: () {
final player = AudioCache();
player.play('key$keyNumber.mp3');
},
child: Text('$keyName'),
),
);
}
Now you can call this method like this.现在你可以像这样调用这个方法了。
keyWidget('keyNumber','keyName',Colors.red);
keyWidget('keyNumber','keyName',Colors.pink);
keyWidget('keyNumber','keyName',Color(0xffACACAC));
You can also make Color
parameter optional您还可以使
Color
参数可选
SAMPLE CODE示例代码
Widget keyWidget(String keyNumber, String keyName,{ mColor=Colors.red}) {
return Container(
width: 100.0,
height: 50.0,
color: mColor, //How I change the color's value? (Colors.{this_one})?
child: FlatButton(
onPressed: () {
final player = AudioCache();
player.play('key$keyNumber.mp3');
},
child: Text('$keyName'),
),
);
}
Now you can call this method like this.现在你可以像这样调用这个方法了。
keyWidget('keyNumber', 'keyName');
keyWidget('keyNumber', 'keyName',mColor: Colors.accents);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.