英文:
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'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'm adding code as well as screen shoot. Kindly help.
**Code**
inputDecoration(title){
return InputDecoration(
prefixIcon:
if(title == 'title'){
Icon(Icons.title_outlined)
}
if(title == 'description'){
Icon(Icons.description),
},
//prefixIcon: title =='title' ? const Icon(Icons.title_outlined) : const Icon(Icons.description),
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))
)
);
}
[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 == 'title' ? Icons.title_outlined : Icons.description,
),
),
Dart 3 switch expression:
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)),
答案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 == '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))
)
);
}
or, you could use the ternary operator
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))),
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论