【问题标题】:How to create a custom height TextFormField in flutter?如何在颤动中创建自定义高度 TextFormField?
【发布时间】:2020-11-01 01:44:18
【问题描述】:

我有一个表单,其中有两个 TextFormField 和一个底部按钮来验证它,但问题是屏幕看起来很尴尬。我希望 **TextFormField ** 底部的一两个完全覆盖它们之间的区域,即使没有内容并且一旦到达底部就可以滚动。

左边是我有的,但我想要右边的,我尝试用 ExpandableContainer 包装它,但它不起作用,因为我是使用边框属性。

【问题讨论】:

    标签: flutter textformfield


    【解决方案1】:

    您可以通过使用Expanded 小部件包装TexFormField 小部件并将TextFormFieldexpandsmaxLines 属性分别设置为truenull 来实现此目的。

    TextFormField 占用所有可用空间而不考虑屏幕大小,请查看下面提供的代码作为示例:

      // wrap it with an Expanded widget
             Expanded(
               child: TextFormField(
                 // set the keyboard type to multiline f
                 keyboardType: TextInputType.multiline,
                 // set maxlines to null
                 maxLines: null,
                 // set expands to true
                 expands: true,
                 decoration: InputDecoration(
                     isDense: true,
                     border: OutlineInputBorder(
                         borderSide: BorderSide(color: Colors.black)
                     )
                 ),
               ),
             ),
    

    【讨论】:

      【解决方案2】:

      您只需增加TextFormField 内的maxLines 即可增加它的大小,如下所示:

      Container(
        height: MediaQuery.of(context).size.height*0.5,//set the size according to your choice
        decoration: BoxDecoration(
          //Customize your container
          color: Colors.red,
          borderRadius: BorderRadius.all(Radius.circular(10.0)),
        ),
        child: TextFormField(
          maxLines: 30,//add any number of lines
          //Customize your text field
        )
      ),
      

      【讨论】:

      • 如果有 5 英寸和 6 英寸的设备怎么办?maxLines 的值相同,因为我想覆盖整个区域。
      • 您可以将其包装在Container 中,并提供height 作为MediaQuery。我正在编辑我的代码
      • 您现在可以查看代码了。如果您需要此代码中的任何其他帮助,请告诉我。
      • 这不是解决问题的更好方法,不同设备的高度设置会有所不同@HardikKumar。该问题要求TextFormField 覆盖屏幕上的所有可用空间。
      猜你喜欢
      • 1970-01-01
      • 2021-02-18
      • 2022-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 2018-07-29
      • 2022-07-05
      相关资源
      最近更新 更多