将颜色值分配给参数|飞镖扑来

发布于 2025-02-13 03:02:00 字数 1477 浏览 0 评论 0原文

我有一个有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(),
      )),
    );
  }
}

app

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2025-02-20 03:02:00

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);

并使用此内容,例如primary:widget.clr,


或者只是在NULL CLR情况下提供默认值,例如

primary: widget.clr ?? const Color.fromARGB(255, 195, 204, 203),

您想在此状态内更新颜色。您可以在状态类内创建本地变量。

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(),
      )),
    );
  }
}

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(),
      )),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文