动态设置颜色时出现死代码警告

发布于 2025-01-09 17:03:27 字数 1374 浏览 4 评论 0原文

我正在尝试使按钮在按下后变成灰色。我阅读了如何执行此操作的信息。我能找到的最好方法是使用三元运算符设置材质颜色,然后更改 setState(() {}) 块中的条件:

  Container vehicleButton(IconData icon_, String text_, {required Function onClickAction}){
    const double buttonSizeX = 200;
    const double buttonSizeY = 100;
    const double iconSize = 60;
    const double buttonTextSize = 15;

    const double buttonMargin = 5;
    
    Color buttonColor = const Color(0xff2196f3);
    const Color iconColor = Color(0xffffffff);
    const Color buttonTextColor = Color(0xffffffff);

    bool pressed = false;

    return Container(padding: const EdgeInsets.only(left: buttonMargin, right: buttonMargin, top: buttonMargin, bottom: buttonMargin), child: SizedBox.fromSize(
      size: Size(buttonSizeX, buttonSizeY), child: Material(color: pressed ? Color.fromARGB(255, 143, 143, 143) : buttonColor, child: InkWell(
      onTap: () {
        setState(() {
          pressed = true;
        });
        onClickAction();
      },
      child: Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
          Icon(icon_, color: iconColor, size: iconSize),
          Text(text_, style: TextStyle(fontSize: buttonTextSize, color: buttonTextColor)),
    ])))));
  }

但是,我收到一条警告: ? 之后的代码在我的三元运算符中是死代码。事实上,按钮按下后不会变成灰色。

我想也许 Material 的值是最终的并且无法更改,但这并不能解释为什么我在互联网上找到的所有示例都使用这种方法。

I'm trying to make buttons that turn grey after having been pressed. I read up on how to do this. The best way I could find is to set the material color with a ternary operator and then change the condition for that in a setState(() {}) block:

  Container vehicleButton(IconData icon_, String text_, {required Function onClickAction}){
    const double buttonSizeX = 200;
    const double buttonSizeY = 100;
    const double iconSize = 60;
    const double buttonTextSize = 15;

    const double buttonMargin = 5;
    
    Color buttonColor = const Color(0xff2196f3);
    const Color iconColor = Color(0xffffffff);
    const Color buttonTextColor = Color(0xffffffff);

    bool pressed = false;

    return Container(padding: const EdgeInsets.only(left: buttonMargin, right: buttonMargin, top: buttonMargin, bottom: buttonMargin), child: SizedBox.fromSize(
      size: Size(buttonSizeX, buttonSizeY), child: Material(color: pressed ? Color.fromARGB(255, 143, 143, 143) : buttonColor, child: InkWell(
      onTap: () {
        setState(() {
          pressed = true;
        });
        onClickAction();
      },
      child: Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
          Icon(icon_, color: iconColor, size: iconSize),
          Text(text_, style: TextStyle(fontSize: buttonTextSize, color: buttonTextColor)),
    ])))));
  }

However, I'm getting a warning that the code after the ? in my ternary operator is dead code. And indeed, the button does not turn grey after pressing.

I thought maybe the values of Material are final and cannot be changed, but this wouldn't explain why all the examples I could find on the internet use this method.

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

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

发布评论

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

评论(3

苍景流年 2025-01-16 17:03:27

您有:

Container vehicleButton(...) {
  bool pressed = false;

  return Container(
      ...
      child: SizedBox.fromSize(
          ...
          child: Material(
              color: pressed ? Color.fromARGB(255, 143, 143, 143) : buttonColor,
              child: InkWell(
                  onTap: () {
                    setState(() {
                      pressed = true;
                    });
                    onClickAction();
                  },

Dart 分析工具正确报告 Color.fromARGB(...) 是死代码,因为条件三元运算符检查 本地 的状态 Pressed 变量在检查时始终 false。尽管 onTap 处理程序设置 pressed = true,但它设置的是本地 pressed 变量的状态,该变量永远不会被再次读取。

您可能希望将 pressed 设为包含 vehicleButton 方法的任何 State 类的成员变量。

You have:

Container vehicleButton(...) {
  bool pressed = false;

  return Container(
      ...
      child: SizedBox.fromSize(
          ...
          child: Material(
              color: pressed ? Color.fromARGB(255, 143, 143, 143) : buttonColor,
              child: InkWell(
                  onTap: () {
                    setState(() {
                      pressed = true;
                    });
                    onClickAction();
                  },

The Dart analysis tools are correctly reporting that Color.fromARGB(...) is dead code because the conditional ternary operator checks the state of the local pressed variable, which at the time it's checked, is always false. Although the onTap handler sets pressed = true, it's setting the state of the local pressed variable, which will never be read again.

You likely intend for pressed to be a member variable of whatever State class contains the vehicleButton method.

黯然#的苍凉 2025-01-16 17:03:27

我建议您不要使用 SizedBox,而是使用 flutter dev 提供的按钮: https:// /docs.flutter.dev/release/writing-changes/buttons

您可以进行的一个简单尝试是:

RaisedButton(
      child: new Text('Attention'),
      textColor: Colors.white,
      color: pressColor ? Colors.grey : Colors.blue, //Choose your personal colors
      onPressed: () => setState(() => pressColor = !pressColor), //change the button colors
    );

Instead of using a SizedBox I suggest you to use the buttons provided by flutter dev: https://docs.flutter.dev/release/breaking-changes/buttons

A simple try you can give is:

RaisedButton(
      child: new Text('Attention'),
      textColor: Colors.white,
      color: pressColor ? Colors.grey : Colors.blue, //Choose your personal colors
      onPressed: () => setState(() => pressColor = !pressColor), //change the button colors
    );
夕色琉璃 2025-01-16 17:03:27

请记住,您必须定义变量 build(BuildContext context)

keep in mind that you have to define your variable build(BuildContext context)

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