如何设置TextField以绘制其错误边界



我有一个TextField。我添加了下面的输入装饰器,它带有一个特定的errorBorder。

如何在不同的边框样式之间切换。使用Form不足以满足我的需求,而且我发现Form相当笨重。我正在寻找一种手动方式来将TextField上的状态设置为错误。

InputDecoration(
filled:true,
fillColor: Color(0x22000000),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(3)),
borderSide: BorderSide(
color: MyColors.Accent,
width: 4,
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(3)),
borderSide: BorderSide(
color: MyColors.PrimaryContrast, width: 2
),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(3)),
borderSide: BorderSide(
color: Colors.red, width: 2
),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(3)),
borderSide: BorderSide(
color: Colors.red, width: 2
),
),
labelStyle: TextStyle(
fontFamily:'Din',
fontWeight: FontWeight.normal,
fontSize: 16,
letterSpacing: 1,
color: MyColors.Accent
)
)

如果您提供errorText,它应该显示errorBorder。它可以是一个空字符串。我不知道是否有其他方法可以触发errorBorder(实际上,我认为这是唯一的方法(。

出于某种原因,我没有看到flutter文档明确表示您需要提供errorText来显示错误边界,但是在errorBorder属性文档中,在另请参阅部分中说:

focusedErrorBorder,当InputDecorator.isFocused为true且InputDecoration.errorText为非null时显示。

在FocusedErrorBorder属性中还显示:

errorBorder,当InputDecorator.isFocused为false且InputDecoration.errorText为非null时显示。

我不知道为什么每个属性的文档本身都没有这样说。也许我错过了什么。

不管怎样,我希望这对你有帮助。

最新更新