[英]How to expand a textField in flutter looks like a text Area
当我在横向模式下点击 textField 时,我想全屏展开,就像 whatsapp
TextFormField(
keyboardType: TextInputType.number,
decoration: InputDecoration(
prefixIcon: Padding(
padding: EdgeInsets.all(0.0),
child: Icon(Icons.person,
size: 40.0, color: Colors.white),
),
hintText: "Input your opinion",
hintStyle: TextStyle(color: Colors.white30),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(new Radius.circular(25.0))),
labelStyle: TextStyle(color: Colors.white)),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 25.0,
),
controller: host,
validator: (value) {
if (value.isEmpty) {
return "Empty value";
}
},
)
您需要做的就是在创建TextField
时设置maxLines
变量。 我在 Card 小部件中添加了文本字段,以便您可以看到总面积。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Simple Material App"),
),
body: Column(
children: <Widget>[
Card(
color: Colors.grey,
child: Padding(
padding: EdgeInsets.all(8.0),
child: TextField(
maxLines: 8,
decoration: InputDecoration.collapsed(hintText: "Enter your text here"),
),
)
)
],
)
);
}
将maxLines设置为null并将keyboardType 设置为TextInputType.multiline,如下所示:
TextField(
maxLines: null,
keyboardType: TextInputType.multiline,
)
不幸的是,在颤振中您无法设置 TextField 或 TextFormField 的最小高度。 为 TextField 或 TextFormField 提供固定大小的最佳方法是指定 Field 应采用的最大行数,而不会进一步扩展。 注意:这不限制输入文本的数量,它只限制显示的行数。
例子:
TextFormField(
keyboardType: TextInputType.multiline,
maxLines: 8,
maxLength: 1000,
),
这会将字段的大小限制为 8 行,但最多可以包含 1000 个字符。 希望这会帮助某人。
设置expands: true
TextField(
maxLines: null,
expands: true,
keyboardType: TextInputType.multiline,
)
或者
如果您在Column
内使用它,请使用:
Expanded(
child: TextField(
maxLines: null,
expands: true,
),
)
如果您的 textField 小部件是 Column 的子小部件并且设置expands: true
。
可以设置textAlignVertical: TextAlignVertical.top
使文本在顶部对齐。
这是文本区域的更好解决方案如果您的 UI 中有多个文本区域,那么您必须在其中指定最大行数。 使用此代码创建一个普通组件,它会根据内容进行扩展
TextField(
maxLines: null,
keyboardType: TextInputType.multiline,
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.