下面的textfield正在表中呈现。我想实现textfield的输入验证。假设它是必需的,验证正在实现,但问题是,因为我不希望文本出错,我已经做了,errorStyle:TextStyle(color:Colors.transparent,fontSize:然后每当验证器被调用时,文本字段的底部边框就会向上移动一点,看起来很难看。
class _TableTextInputFieldComponentState
extends ConsumerState<TableTextInputFieldComponent> {
TextEditingController textInputController = TextEditingController();
String columnName = '';
Map<String, dynamic> columnAccess = {};
Map<String, dynamic> columnNumberTypeFieldDetail = {};
bool isrequired = false;
bool isStrikeThrough = false;
bool isEmpty = false;
FocusNode focusNode = FocusNode();
String errorMessage = '';
u/override
void initState() {
// TODO: implement initState
isStrikeThrough = widget.isStrikeThrough;
if (widget.indexOfRow == 0) {
isStrikeThrough = false;
}
columnNumberTypeFieldDetail = widget.column;
columnName = columnNumberTypeFieldDetail['name'];
columnAccess = columnNumberTypeFieldDetail['access'];
if (columnNumberTypeFieldDetail.containsKey('validators')) {
if (columnNumberTypeFieldDetail['validators'].containsKey('required')) {
isrequired = columnNumberTypeFieldDetail['validators']['required'];
}
}
if (widget.value != '' && widget.value != null) {
textInputController.text =
widget.value.toString(); // Convert to string if it's a number
}
// Add the focusNode listener to update the state
focusNode.addListener(() {
updateEmptyState();
});
super.initState();
}
// Extracted method to update the isEmpty state
void updateEmptyState() {
setState(() {
isEmpty = textInputController.text.isEmpty;
});
}
u/override
Widget build(BuildContext context) {
return SizedBox(
//adding height didn't have any effect really.[the textfield is covering all whole datacell, but the moment validator is called, the textfield shrinks and it's bottom border shifts a bit upwards][1]
height: 200,
child: TextFormField(
controller: textInputController,
focusNode: focusNode,
readOnly: widget.readOnly || isStrikeThrough,
style: TextStyle(
decoration: isStrikeThrough
? TextDecoration.lineThrough
: TextDecoration.none,
),
decoration: const InputDecoration(
isCollapsed: true,
contentPadding: EdgeInsets.fromLTRB(5, 20, 5, 13),
errorStyle: TextStyle(color: Colors.transparent, fontSize: 0),
filled: false,
),
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: isrequired
? (value) {
if (value == null || value.isEmpty) {
errorMessage = 'Text is required';
return errorMessage;
}
return null;
}
: null,
onChanged: (value) {
//Handle string input
value = textInputController.text;
widget.onValueChangeInTable(
widget.indexOfRow, value, columnName, columnAccess);
},
),
);
}
}
字符串
1条答案
按热度按时间kx1ctssn1#
试试这个代码
字符串