简体   繁体   English

Flutter 增加TextFormField的高度

[英]Flutter increase height of TextFormField

I am creating a Flutter app.我正在创建一个 Flutter 应用程序。 i made the design like this.我做了这样的设计。 在此处输入图像描述

My TextFormField form field for email and password heights are small.我的 TextFormField 表单字段为 email,密码高度很小。 I want it to be the same size of the button.我希望它与按钮大小相同。

final email = TextFormField(
    keyboardType: TextInputType.emailAddress,
    autofocus: false,
    initialValue: 'sathyabaman@gmail.com', 
    style: new TextStyle(fontWeight: FontWeight.normal, color: Colors.white),
    decoration: InputDecoration(
      hintText: 'Email', 
      contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(32.0)
      ),
    ),
  );

Whats the syntax for the height in text form field.文本表单字段中高度的语法是什么。

Just adjust the contentPadding in InputDecoration.只需在 InputDecoration 中调整 contentPadding。

在此处输入图片说明

final email = TextFormField(
    keyboardType: TextInputType.emailAddress,
    autofocus: false,
    initialValue: 'sathyabaman@gmail.com',
    style: new TextStyle(fontWeight: FontWeight.normal, color: Colors.white),
    decoration: InputDecoration(
      hintText: 'Email',
      contentPadding: new EdgeInsets.symmetric(vertical: 25.0, horizontal: 10.0),
      border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
    ),
  );

You can change the height by changing the minLines value just try this您可以通过更改 minLines 值来更改高度,试试这个

               TextFormField(
                           keyboardType: TextInputType.multiline,
                           controller: _opTextController,
                           decoration: InputDecoration(
                               isDense: true,
                             border: OutlineInputBorder(
                               borderSide: BorderSide(color: Colors.black)
                             )

                           ),
                           maxLines: 5,
                           minLines: 3,
                           // controller: cpfcontroller,
                         )

在此处输入图片说明

Use these two lines to control TextFormField Height inside InputDecoration .使用这两行来控制InputDecoration TextFormField Height。

isDense: true, 
contentPadding: EdgeInsets.fromLTRB(10, 10, 10, 0),

Full example完整示例

Material(
                elevation: 4,
                shadowColor: Colors.blue,
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                child: Padding(
                  padding: const EdgeInsets.only(left: 12),
                  child: TextFormField(
                    controller: searchProvider.searchController,
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                        hintText: 'hintText',
                        isDense: true, // important line
                        contentPadding: EdgeInsets.fromLTRB(10, 10, 10, 0),// control your hints text size
                        hintStyle: TextStyle(letterSpacing: 2, color: Colors.black54, fontWeight: FontWeight.bold),
                        fillColor:  Colors.white30 ,
                        filled: true,
                        border: OutlineInputBorder(borderRadius: BorderRadius.circular(30), borderSide: BorderSide.none)),
                  ),
                ),
              ),

You can user this code to customized your TextFormField您可以使用此代码自定义您的 TextFormField

new SizedBox(
  width: 200.0,
  height: 300.0,
  child: const Card(child: const Text('Hello World!')),
)

just add a Container.只需添加一个容器。 Adjust the height of the Container as per Your requirement and make textformfield the child of the container.根据您的要求调整容器的高度,并使 textformfield 成为容器的子项。

Set the expands attribute on TextFormField to be true then put TextFormField into a SizedBox with a heightTextFormField上的expands属性设置为true然后将TextFormField放入具有heightSizedBox

SizedBox(
   height: 40.0,
   child: TextFormField(
    keyboardType: TextInputType.emailAddress,
    autofocus: false,
    expands: true //Setting this attribute to true does the trick
    initialValue: 'sathyabaman@gmail.com', 
    style: new TextStyle(fontWeight: FontWeight.normal, color: Colors.white),
    decoration: InputDecoration(
      hintText: 'Email', 
      contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(32.0)
      ),
    ),
  ),
)

There's another alternative to adding extra, permanent padding to cover the errorText — which would probably mess up many designer's original project.除了添加额外的永久填充来覆盖errorText ,还有另一种选择——这可能会弄乱许多设计师的原始项目。

You could create a modified version of the source's TextFormField .您可以创建源的TextFormField的修改版本。

To achieve just that, you can:为了实现这一目标,您可以:

  1. Copy-paste all of the content in the source's TextFormField .复制粘贴源的TextFormField中的所有内容。
  2. Rename your custom TextFormField just so you avoid naming conflicts with the original.重命名您的自定义TextFormField只是为了避免与原始命名冲突。
    • You should probably also rename the internal state class.您可能还应该重命名内部状态类。
    • In VS Code, you can use Ctrl + H to replace TextFormField for, say, TextFormFieldWithErrorTextOption .在 VS Code 中,您可以使用Ctrl + HTextFormField替换为TextFormFieldWithErrorTextOption
  3. Add another parameter to the TextFormField 's constructor (below this line ), say, errorTextPresent :将另一个参数添加到TextFormField的构造函数(在此行下方),例如errorTextPresent
     // `true` is the current implicit default, ie, showing the `errorText` bool errorTextPresent = true
  4. Change the decoration's initialization for the internal TextField :更改内部TextField装饰初始化
    1. From:从:
       decoration: effectiveDecoration.copyWith(field.errorText)
    2. To:到:
       decoration: effectiveDecoration.copyWith( errorText: errorTextPresent ? field.errorText : null)
  5. Then, you can use your new TextFormField similarly to how you use any TextFormField :然后,你可以用你的新TextFormField类似于你如何使用任何TextFormField
     TextFormFieldWithErrorTextOption( errorTextPresent: false, // `false` will disable the `errorText` ... ),

Screenshot:截屏:

在此处输入图片说明


Code:代码:

You need to use a SizedBox and TextField.maxLines property.您需要使用SizedBoxTextField.maxLines属性。

@override
Widget build(BuildContext context) {
  final height = 100.0;
  return Scaffold(
    body: SizedBox( // <--- SizedBox
      height: height,
      child: TextField(
        cursorColor: Colors.red,
        maxLines: height ~/ 20,  // <--- maxLines
        decoration: InputDecoration(
          filled: true,
          hintText: 'Hint text',
          fillColor: Colors.grey,
        ),
      ),
    ),
  );
}
 Expanded(
                          child: TextFormField(
                            controller: emailAddressController,
                            obscureText: false,
                            decoration: InputDecoration(
                              labelText: 'Email Address',
                              labelStyle: TextStyle(
                                fontFamily: 'Lexend Deca',
                                color: Color(0xFF95A1AC),
                                fontSize: 14,
                                fontWeight: FontWeight.normal,
                              ),
                              hintText: ' Enter your email here ...',
                              hintStyle: TextStyle(
                                fontFamily: 'Lexend Deca ',
                                color: Color(0xFF95A1AC),
                                fontSize: 14,
                                fontWeight: FontWeight.normal,
                              ),
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(
                                  color: Color(0xFFDBE2E7),
                                  width: 2,
                                ),
                                borderRadius: BorderRadius.circular(8),
                              ),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(
                                  color: Color(0xFFDBE2E7),
                                  width: 2,
                                ),
                                borderRadius: BorderRadius.circular(8),
                              ),
                              filled: true,
                              fillColor: Colors.white,
                              contentPadding:
                                  EdgeInsetsDirectional.fromSTEB(
                                      10, 10, 0, 10),
                            ),
                            style: TextStyle(
                              fontFamily: ' Lexend Deca',
                              color: Color(0xFF2B343A),
                              fontSize: 14,
                              fontWeight: FontWeight.normal,
                            ),
                          ),
                        )

If anyone's searching for a different solution to change its height, besides changing the Theme.如果有人正在寻找一种不同的解决方案来改变它的高度,除了改变主题。 You can use the decoration's constraints parameter:您可以使用装饰的约束参数:

decoration: const InputDecoration(
            constraints: BoxConstraints(maxHeight: 50, minHeight: 50),
            labelText: "Your Label")

Use a TextField or a TextFormField, add this parameters, maxLines .使用 TextField 或 TextFormField,添加此参数maxLines See code below:请参阅下面的代码:

final inputBorder =
        OutlineInputBorder(borderSide: Divider.createBorderSide(context));

    TextFormField(
                controller: _myController,
                keyboardType: TextInputType.text,
                
                decoration: InputDecoration(
                  constraints: const BoxConstraints.expand(
                  height: 300, width: 400
                ),
                  hintText: 'What is Your Question?',
                  border: inputBorder,
                  focusedBorder: inputBorder,
                  enabledBorder: inputBorder,
                  filled: true,
                  contentPadding: const EdgeInsets.all(8),
                ),
                maxLength: 250,
                maxLines: 50,
    
              ),

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM