decoration: InputDecoration(
labelStyle: new TextStyle(color: Colors.green),
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.yellow),
),
hintText: 'What do people call you?',
labelText: 'User ID',
errorText: <------ I want to use conditional here
),
我想根据不同的条件呈现不同的errorText。我试图使用Text()
小部件来做到这一点,但后来却没有用。如何在errorText
属性内使用其他文本?
您可以使用 ternary operator
(condition)? then:else
在您的代码中:
decoration: InputDecoration(
labelStyle: new TextStyle(color: Colors.green),
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.yellow),
),
hintText: 'What do people call you?',
labelText: 'User ID',
errorText: (true)? "Error true":"Error false"
),
对于多个条件:
errorText: (x == 1)? "Error 1": (x == 2)?"Error 2":"Error else"
或者您可以创建一个根据条件返回字符串的函数
errorText: _getError()
---
String _getError(){
if(x == 1) return "Error 1";
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句