将颜色值分配给参数|飞镖扑来
我有一个有40个按钮的GridView,这就是其中之一:
MyCalcButton(
//clr: Colors.amber
myNum: "2",
funkcja: () {},
),
在我的按钮类中,有2个必需参数, mynum
和 funkcja
和一个未缴纳的 - > clr:
。现在,我想更改此 clr
参数的值(颜色),其中一些按钮,而不是每个按钮。有没有办法将这种颜色分配给其中的某些颜色,并给其余的一种静态颜色?是否有必要为此 clr
给出一种颜色。
有此按钮的类
String myNum = "";
class MyCalcButton extends StatefulWidget {
const MyCalcButton({Key? key, this.myNum, required this.funkcja, this.clr})
: super(key: key);
final Function funkcja;
final myNum;
final Color? clr;
@override
State<MyCalcButton> createState() => _MyCalcButtonState();
}
class _MyCalcButtonState extends State<MyCalcButton> {
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
widget.funkcja();
},
style: ElevatedButton.styleFrom(
//maybe here i can change something to do this?? I dont know
primary: const Color.fromARGB(255, 195, 204, 203),
),
child: Center(
child: Text(
widget.myNum.toString(),
)),
);
}
}
i have GridView where there is 40 buttons, that's one of them:
MyCalcButton(
//clr: Colors.amber
myNum: "2",
funkcja: () {},
),
In my button class there are 2 required parameters, myNum
and funkcja
and one unrequired - > clr:
. Now i want to change value (color) of this clr
parameter some of those buttons, not every. Is there a way to assign this color to some of these and give rest of them one static color? Is it necessary to give one by one color for this clr
??
There is class of this button
String myNum = "";
class MyCalcButton extends StatefulWidget {
const MyCalcButton({Key? key, this.myNum, required this.funkcja, this.clr})
: super(key: key);
final Function funkcja;
final myNum;
final Color? clr;
@override
State<MyCalcButton> createState() => _MyCalcButtonState();
}
class _MyCalcButtonState extends State<MyCalcButton> {
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
widget.funkcja();
},
style: ElevatedButton.styleFrom(
//maybe here i can change something to do this?? I dont know
primary: const Color.fromARGB(255, 195, 204, 203),
),
child: Center(
child: Text(
widget.myNum.toString(),
)),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
clr
上,您可以在参数上提供默认值,并使用此内容,例如
primary:widget.clr,
。或者只是在NULL
CLR
情况下提供默认值,例如您想在此状态内更新颜色。您可以在状态类内创建本地变量。
You can provide default value on parameter, in your case on
clr
and use this like
primary: widget.clr,
.Or just provide default value on null
clr
case likeIf you like to update color within this state. you can create a local variable inside state class.