Flutter文本字段随用户类型而扩展 [英] Flutter textfield expand as user types
问题描述
我有一个文本字段,需要具有以下多行内容,以便文本自动换行,但是它占用了maxLines中定义的所有行的空间.我希望能够使它看起来像1行并随着文本换行而扩展.关于如何执行此操作的任何想法都扑朔迷离?
I have a textfield and need to have multi-line as below so that the text wraps, however it takes up the space of all the lines that is defined in maxLines. I want to be able to have it look like 1 line and expand as the text wraps. Any ideas of how to do this is flutter?
new TextField(
decoration: const InputDecoration(
hintText: 'Reply',
labelText: 'Reply:',
),
autofocus: false,
focusNode: _focusnode,
maxLines: 1,
controller: _newreplycontroller,
keyboardType: TextInputType.text,
),
推荐答案
将maxLines
设置为null,它将自动垂直增长.
Set maxLines
to null and it will auto grow vertically.
已记录(但不完全清楚)此处(我已经创建了一个PR来对此进行更新,对于这个问题,我应该首先开始做;).为了弄清楚这一点,我最终查看了TextField
及其超类的代码,看看如果将其设置为null会出现什么情况.
It's documented (but not entirely clearly) here (edit: I've created a PR to update this, which I should've done to begin with for this question ;). To figure it out I ended up looking at the code for TextField
and its superclass(es), seeing what the behavior would be if set to null.
因此,您的代码应如下所示:
So your code should be as follows:
new TextField(
decoration: const InputDecoration(
hintText: 'Reply',
labelText: 'Reply:',
),
autofocus: false,
focusNode: _focusnode,
maxLines: null,
controller: _newreplycontroller,
keyboardType: TextInputType.text,
),
如果您要使其自动水平地 增长,则可能不应该-至少不基于文本内容.但是我假设您想使其垂直垂直增长.
If you're trying to make it autogrow horizontally, you probably shouldn't - at least not based on the text content. But I'm asuming you want to make it autogrow vertically.
这篇关于Flutter文本字段随用户类型而扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!