【问题标题】:flutter TextFormField validation display alignment errorflutter TextFormField 验证显示对齐错误
【发布时间】:2020-12-15 17:56:27
【问题描述】:

我有一个 TextFormField,在 Empty 上进行验证。

为了控制高度,TextFormField 嵌套在 Container 小部件中。

这会导致错误消息重叠显示为附加图片的意外副作用。

要测试示例代码,请按“提交”查看错误。

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();
                  },
                )
              ],
            ),
          )),
    );
  }
}

【问题讨论】:

    标签: validation flutter textformfield


    【解决方案1】:

    解决方案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,
      ),
    ),
    

    解决方案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,
      ),
    ),
    

    【讨论】:

    • 不客气,布拉克!很高兴它有帮助! @布拉克
    【解决方案2】:

    你可以用这个

       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)),
      )),
    

    【讨论】:

      【解决方案3】:

      感谢Mobina的回答,

      似乎问题是颤振限制。暂时..

      我决定在顶部显示错误消息。 (您的解决方案:1)

      没有边框我可以像往常一样显示错误消息。 (您的解决方案: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,
                    ),
                  ),
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-27
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 2019-12-25
        • 2019-12-04
        • 2020-10-01
        • 2019-08-12
        相关资源
        最近更新 更多