【发布时间】:2022-07-05 04:48:16
【问题描述】:
我的 textformfield 的样式是用灰色包装容器包装的,以便在该字段处于焦点时看起来不同。但是,每当出现错误消息时,它就会变得臃肿(大小增加),因为该字段是用那个灰色的包装容器包装的。
如何在灰色包装容器之外自定义错误消息的样式,以使 textformfield 的大小不会膨胀?
基本上我希望错误消息位于包装容器的灰色之外。
@override Widget build(BuildContext context) {
final InputDecorationTheme inputTheme = Theme.of(context).inputDecorationTheme;
return Focus(
canRequestFocus: false,
child: Builder(builder: (context) {
final FocusNode focusNode = Focus.of(context);
final bool hasFocus = focusNode.hasFocus;
return GestureDetector(
onTap: () {
if (hasFocus) {
focusNode.unfocus();
} else {
focusNode.requestFocus();
}
},
child: Container(
decoration: BoxDecoration(
color: hasFocus ? Colors.white : Color(0xFFF4F4F4),
border: hasFocus
? Border.all(color: Color(0xFF0E4DA4), width: 2)
: Border.all(width: 2, color: Colors.transparent),
borderRadius: const BorderRadius.all(Radius.circular(5)),
boxShadow: hasFocus
? [
BoxShadow(
color: Color(0xFFF4F4F4),
spreadRadius: 3,
blurRadius: 0,
offset: Offset(0, 0), // changes position of shadow
),
]
: [],
),
child: TextFormField(
enabled: enabled,
key: this.customKey,
controller: textEditingController,
initialValue: initialValue,
inputFormatters: isNumericalOnly
? [
FilteringTextInputFormatter.digitsOnly,
FilteringTextInputFormatter.singleLineFormatter,
]
: [
FilteringTextInputFormatter.singleLineFormatter,
],
keyboardType: isNumericalOnly ? TextInputType.number : TextInputType.text,
focusNode: focusNodeToggle,
maxLength: maxLength,
validator: (String? value) {
return validator != null ? validator!(value.toString()) : null;
},
onSaved: (String? value) {
return onSaved != null ? onSaved!(value.toString()) : null;
},
onChanged: (String? value) {
return onChanged != null ? onChanged!(value.toString()) : null;
},
buildCounter: maxLength != null && isCounterVisible == true
? (BuildContext context, {int? currentLength, int? maxLength, bool? isFocused}) =>
Container(child: Text('$currentLength/$maxLength'))
: (BuildContext context, {int? currentLength, int? maxLength, bool? isFocused}) =>
null,
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.auto,
hintText: customHintText,
helperText: customHelperText,
helperMaxLines: 2,
filled: true,
fillColor: Colors.transparent,
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
labelStyle: hasFocus ? inputTheme.labelStyle : TextStyle(color: Color(0xFF525252)),
label: Text.rich(
TextSpan(
children: <InlineSpan>[
WidgetSpan(
child: Text(
label.toString(),
),
),
WidgetSpan(
child: isDataloading
? LoadingIndicator(
width: 15,
height: 15,
)
: Text('')),
],
),
),
),
),
),
);
}),
);
}
【问题讨论】:
-
@7mada 不,我想将错误消息定位在包装容器之外,而不仅仅是设置文本外观的样式。
-
您需要使用 bool 和 Text 小部件管理自定义验证,并在点击您提交的小部件时设置条件
标签: flutter dart flutter-layout flutter-web