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(),
)),
);
}
}
CodePudding user response:
You can provide default value on parameter, in your case on clr
class MyCalcButton extends StatefulWidget {
const MyCalcButton({
Key? key,
this.myNum,
required this.funkcja,
this.clr =const Color.fromARGB(255, 195, 204, 203),
}) : super(key: key);
and use this like primary: widget.clr,
.
Or just provide default value on null clr
case like
primary: widget.clr ?? const Color.fromARGB(255, 195, 204, 203),
If you like to update color within this state. you can create a local variable inside state class.
class _MyCalcButtonState extends State<MyCalcButton> {
late Color? color = widget.clr;
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
widget.funkcja();
},
style: ElevatedButton.styleFrom(
primary: color,
),
child: Center(
child: Text(
widget.myNum.toString(),
)),
);
}
}