使用if条件设置图标。

huangapple go评论62阅读模式
英文:

Flutter: set Icon using if conditions

问题

![![输入图像描述][1]][1]我是Flutter的新手。
我可以使用三元运算符来为两种情况设置图标,但我需要为多个条件设置图标。所以,我需要使用if或case语句。我该怎么做呢?

我正在添加代码以及屏幕截图。请帮忙。

**代码**

```dart
inputDecoration(title) {
  return InputDecoration(
    prefixIcon: title == 'title' ? Icon(Icons.title_outlined) :
               title == 'description' ? Icon(Icons.description) : null,
    
    prefixIconColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
      if (states.contains(MaterialState.focused)) {
        return Colors.green;
      }
      if (states.contains(MaterialState.error)) {
        return Colors.red;
      }
      return Colors.grey;
    }),
    border: const OutlineInputBorder(
      gapPadding: 15,
      borderRadius: BorderRadius.all(Radius.circular(3))
    )
  );
}

请注意,我修复了你的代码中的一些语法错误,并使用了条件运算符来设置不同条件下的图标。

<details>
<summary>英文:</summary>

[![enter image description here][1]][1]I&#39;m new in Flutter journey.
I can use use ternary operator to set icon for 2 cases but I need to set Icon for multiple conditions. So, I need to use if or case statement. How can I do that.

I&#39;m adding code as well as screen shoot. Kindly help.

**Code**

    inputDecoration(title){
      return InputDecoration(
        prefixIcon:
          if(title == &#39;title&#39;){
        Icon(Icons.title_outlined)
        }
         if(title == &#39;description&#39;){
        Icon(Icons.description),
          },
    
        //prefixIcon: title ==&#39;title&#39; ? const Icon(Icons.title_outlined) : const Icon(Icons.description),
    
        prefixIconColor: MaterialStateColor.resolveWith((Set&lt;MaterialState&gt; states) {
          if(states.contains(MaterialState.focused)){
            return Colors.green;
          }if (states.contains(MaterialState.error)){
            return Colors.red;
          }
          return Colors.grey;
        }),
        border: const OutlineInputBorder(
          gapPadding: 15,
          borderRadius: BorderRadius.all(Radius.circular(3))
        )
      );
    }

 


  [1]: https://i.stack.imgur.com/tE2bx.png

</details>


# 答案1
**得分**: 3

```dart
InputDecoration(
  prefixIcon: Icon(
    title == 'title' ? Icons.title_outlined : Icons.description,
  ),
),
Icon buildPrefix(String title) {
  return switch (title) {
    'description' => const Icon(Icons.description),
    'title' => const Icon(Icons.title_outlined),
    _ => const Icon(Icons.error)
  };
}

...

InputDecoration(prefixIcon: buildPrefix(title)),
英文:

You can use ternary operators or a switch statement.

InputDecoration(
            prefixIcon: Icon(
              title == &#39;title&#39; ? Icons.title_outlined : Icons.description,
            ),
          ),

Dart 3 switch expression:

  Icon buildPrefix(String title) {
    return switch (title) {
      &#39;description&#39; =&gt; const Icon(Icons.description),
      &#39;title&#39; =&gt; const Icon(Icons.title_outlined),
      _ =&gt; const Icon(Icons.error)
    };
  }

...

 InputDecoration(prefixIcon: buildPrefix(title)),

答案2

得分: 1

我不认为在构造函数中使用if(){}来进行条件判断的语法是可接受的,而是可以在构建之前设置这个逻辑。

示例:

inputDecoration(title) {
  Widget prefixIcon;
  
  if (title == 'title') {
    prefixIcon = Icon(Icons.title_outlined);
  } else if (title == 'description') {
    prefixIcon = Icon(Icons.description);
  }
  
  return InputDecoration(
    prefixIcon: prefixIcon,
    prefixIconColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
      if (states.contains(MaterialState.focused)) {
        return Colors.green;
      } else if (states.contains(MaterialState.error)) {
        return Colors.red;
      }
      return Colors.grey;
    }),
    border: const OutlineInputBorder(
      gapPadding: 15,
      borderRadius: BorderRadius.all(Radius.circular(3))
    )
  );
}

或者,你可以使用三元运算符:

inputDecoration(title) {
  return InputDecoration(
    prefixIcon: title == 'title'
        ? Icon(Icons.title_outlined)
        : (title == 'description' ? Icon(Icons.description) : null),
    prefixIconColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
      if (states.contains(MaterialState.focused)) {
        return Colors.green;
      } else if (states.contains(MaterialState.error)) {
        return Colors.red;
      }
      return Colors.grey;
    }),
    border: const OutlineInputBorder(
        gapPadding: 15,
        borderRadius: BorderRadius.all(Radius.circular(3))),
  );
}
英文:

I do not think that the syntax is acceptable if you were doing an if condition using if(){} inside the constuctor, instead you could set this logic before the build.

Example:

inputDecoration(title) {
  Widget prefixIcon;
  
  if (title == &#39;title&#39;) {
    prefixIcon = Icon(Icons.title_outlined);
  } else if (title == &#39;description&#39;) {
    prefixIcon = Icon(Icons.description);
  }
  
  return InputDecoration(
    prefixIcon: prefixIcon,
    prefixIconColor: MaterialStateColor.resolveWith((Set&lt;MaterialState&gt; states) {
      if (states.contains(MaterialState.focused)) {
        return Colors.green;
      } else if (states.contains(MaterialState.error)) {
        return Colors.red;
      }
      return Colors.grey;
    }),
    border: const OutlineInputBorder(
      gapPadding: 15,
      borderRadius: BorderRadius.all(Radius.circular(3))
    )
  );
}

or, you could use the ternary operator

inputDecoration(title) {
  return InputDecoration(
    prefixIcon: title == &#39;title&#39;
        ? Icon(Icons.title_outlined)
        : (title == &#39;description&#39; ? Icon(Icons.description) : null),
    prefixIconColor: MaterialStateColor.resolveWith((Set&lt;MaterialState&gt; states) {
      if (states.contains(MaterialState.focused)) {
        return Colors.green;
      } else if (states.contains(MaterialState.error)) {
        return Colors.red;
      }
      return Colors.grey;
    }),
    border: const OutlineInputBorder(
        gapPadding: 15,
        borderRadius: BorderRadius.all(Radius.circular(3))),
  );
}

huangapple
  • 本文由 发表于 2023年7月3日 10:28:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定