dart 我不希望textfield返回任何字符串,而是应该只显示红色的错误边界,没有错误文本在 Flutter

xtupzzrd  于 5个月前  发布在  Flutter
关注(0)|答案(1)|浏览(73)

下面的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);
        },
      ),
    );
  }
}

字符串

kx1ctssn

kx1ctssn1#

试试这个代码

SizedBox(
      child: TextFormField(
        controller: controller.textInputController,
        readOnly: false,
        style: TextStyle(decoration: TextDecoration.none),
        decoration: InputDecoration(
          isCollapsed: true,
          contentPadding: EdgeInsets.zero,
          errorStyle: TextStyle(color: Colors.transparent, fontSize: 0),
          errorBorder: OutlineInputBorder(borderSide: BorderSide(color: Colors.red, width: 2)),
          focusedErrorBorder: OutlineInputBorder(borderSide: BorderSide(color: Colors.red, width: 2)),
          filled: false,
        ),
        autovalidateMode: AutovalidateMode.onUserInteraction,
        validator: (value) {
          if (value == null || value.isEmpty) {
            return '';
          }
          return null;
        },
        onChanged: (value) {},
      ),
    )

字符串

相关问题