简体   繁体   English

flutter TextFormField 验证显示对齐错误

[英]flutter TextFormField validation display alignment error

在此处输入图片说明

在此处输入图片说明

I have a TextFormField with validation on Empty.我有一个带有 Empty 验证的TextFormField

In order to control height, TextFormField was nested inside Container widget.为了控制高度, TextFormField嵌套在Container小部件中。

Which cause unexpected side effect of displaying error message overlap as attached pictures.这会导致显示错误消息与附加图片重叠的意外副作用。

to test sample code, press "Submit" to see error.要测试示例代码,请按“提交”以查看错误。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: SimpleForm(),
    );
  }
}

class SimpleForm extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final formKey = GlobalKey<FormState>();
    return SafeArea(
      child: Scaffold(
//          primary: true,
          body: Form(
            key: formKey,
            child: Column(
              children: [
                SizedBox(
                  height: 0,
                ),
//            Container(height: 0,),
                Container(
                  height: 38,
                  margin: EdgeInsets.all(6),
                  child: TextFormField(
                    maxLines: 1,
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      hintText: 'Name',
//                  errorStyle: TextStyle(fontSize: 0, height: 0),
                    ),
                    validator: (value) => (value.isEmpty) ? '**' : null,
                  ),
                ),
                FlatButton(
                  child: Text('Submit'),
                  onPressed: () {
                    formKey.currentState.validate();
                  },
                )
              ],
            ),
          )),
    );
  }
}

Solution 1. You can set helperText for the TextField 's decoration and increase the Container 's height:解决方案 1.您可以为TextFielddecoration设置helperText并增加Container的高度:

Container(
  height: 60,
  child: TextFormField(
    maxLines: 1,
    decoration: InputDecoration(
      border: OutlineInputBorder(),
      hintText: 'Name',
      helperText: ' ', // this is new
    ),
    validator: (value) => (value.isEmpty) ? '**' : null,
  ),
),

Solution 2. You can set the line height of the error message to 0 (it will be displayed above the text field):解决方案2.可以将错误信息的行高设置为0(会显示在文本框上方):

Container(
  height: 38,
  child: TextFormField(
    maxLines: 1,
    decoration: InputDecoration(
      border: OutlineInputBorder(),
      hintText: 'Name',
      errorStyle: TextStyle(height: 0), // this is new
    ),
    validator: (value) => (value.isEmpty) ? '**' : null,
  ),
),

You can use this你可以用这个

   TextFormField(
  decoration: new InputDecoration(
  enabledBorder: OutlineInputBorder(                     //change border of enable textfield
  borderRadius: BorderRadius.all(Radius.circular(40.0)),
  borderSide: BorderSide(color: colorValue),),
        focusedBorder: OutlineInputBorder(        //focus boarder
          borderRadius: BorderRadius.all(Radius.circular(40.0)),
          borderSide: BorderSide(color: colorValue),
        ),
                 focusedBorder: OutlineInputBorder(
      borderRadius: BorderRadius.all(Radius.circular(4)),
      borderSide: BorderSide(width: 1,color: Colors.red),
    ),
    disabledBorder: OutlineInputBorder(
      borderRadius: BorderRadius.all(Radius.circular(4)),
      borderSide: BorderSide(width: 1,color: Colors.orange),
    ),
    enabledBorder: OutlineInputBorder(
      borderRadius: BorderRadius.all(Radius.circular(4)),
      borderSide: BorderSide(width: 1,color: Colors.green),
    ),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.all(Radius.circular(4)),
      borderSide: BorderSide(width: 1,)
    ),
    errorBorder: OutlineInputBorder(.                              //error boarder
      borderRadius: BorderRadius.all(Radius.circular(4)),
      borderSide: BorderSide(width: 1,color: Colors.black)
    ),

        isDense: true,
        counterText: "",
        contentPadding: EdgeInsets.fromLTRB(10, 20, 10, 0),  //padding according to your need
        hintText: "create new",
        hintStyle: TextStyle(color: colorValue, fontSize: 13)),
  )),

Thanks for the answer Mobina ,感谢Mobina的回答,

Seem like issues is flutter limitation.似乎问题是颤振限制。 For time being..暂时的..

with border I decide to display error message on top.带边框我决定在顶部显示错误消息。 (Your solution :1) (您的解决方案:1)

without border I can display err msg as usual.没有边框我可以像往常一样显示 err msg。 (Your solution : 2) (您的解决方案:2)

// with border
            Container(
              height: 38,
              margin: EdgeInsets.all(6),
              child: TextFormField(
                textAlignVertical: TextAlignVertical.bottom,
                style: TextStyle(fontSize: 14),
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: 'Name',
                  errorStyle: TextStyle(height: 0),
                ),
                validator: (value) => (value.isEmpty) ? 'hide overlap' : null,
              ),
            ),
// without border
            Container(
              height: 38,
              margin: EdgeInsets.all(6),
              child: TextFormField(
                textAlignVertical: TextAlignVertical.bottom,
                style: TextStyle(fontSize: 14),
                decoration: InputDecoration(
                  hintText: 'Name',
                  helperText: ' ',   isDense: true,
                  counterText: "",
                  contentPadding: EdgeInsets.fromLTRB(10, 30, 10, 0),
                ),
                validator: (value) => (value.isEmpty) ? 'hide overlap' : null,
              ),
            ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter Textformfield 验证器将最后一个 TextFormfield 集中在验证错误上,而不是第一个 - Flutter Textformfield validator Focuses Last TextFormfield on validation error instead of first Flutter TextFormField Validation 使用构造函数调用一页错误 - Flutter TextFormField Validation using constructor to call in one page error TextFormField 在 TextFormField 中显示错误 - Flutter - TextFormField showing error inside the TextFormField- Flutter 我的 TextFormField 的验证总是抛出错误 - the validation of my TextFormField always throw an error Flutter - 如何根据验证结果更改 TextFormField 填充颜色 - Flutter - How to change TextFormField fillColor according Validation result 如何更改 Textformfield 验证文本的 position? Flutter - How can I change the position of the Textformfield validation text ? Flutter 如何将焦点添加到具有验证错误的 textformfield - How to add focus to textformfield which has validation error Flutter Textformfield 错误消息下移下一个小部件 - Flutter Textformfield error message shifted down the next widget 如何在flutter中的textformfield边界内显示标签和错误消息? - How to show label and error message inside the boundry of textformfield in flutter? 如何从 flutter 的文本字段中显示验证错误消息 - how to display validation error message out from the textfield in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM