有没有一种方法可以动态更改Flutter TextField的maxLines? [英] Is there a way to dynamically change the Flutter TextField's maxLines?
问题描述
我有一个像这样的TextField:
I have a TextField like this:
new Flexible(
fit: FlexFit.loose,
child: new Container(
alignment: FractionalOffset.topLeft,
child: new TextField(
decoration: new InputDecoration(
hintText: 'Add a note',
),
maxLines: 1,
onChanged: (value) => _showSaveOptions(value),
),
),
),
我想将我的TextField从maxLines
设置为1开始,但是在键入时,我希望maxLines增加,以便所有文本都保留在页面上,而我没有向上滚动.但是,我也不想将maxLines
设置为5或10,因为空的TextField占用了必要的更多空间.我要实现的行为的一个示例是在Google表单的长答案文本"选项中,该选项以一行开头:
并随着我的输入而扩展:
I'd like to have my TextField to start out with maxLines
set to 1, but as I type, I would like the maxLines to increase, so that all the text remains on the page and I don't have to scroll up. However, I also don't want to start off with maxLines
set to say, 5 or 10, because the empty TextField takes up more space that necessary. An example of the behavior I want to implement is in the Long Answer Text option in Google Forms, where it starts with one line:
and expands as I type:
我已经考虑过将maxLines
设置为一个计数器,该计数器在用户每次换行时都会增加,但是如何确定用户何时填满行?
I've considered setting maxLines
to a counter that increments every time the user hits a newline, but how do I determine when the user has filled up a line?
推荐答案
Flutter在TextField中落后于多行支持.在针对同一问题提出了问题之后,在 0.0.16 版本.
Flutter was lagging multiline support in TextField. After an issue was raised regarding the same, the multiline feature has been added in the 0.0.16 release.
确保将flutter升级到最新版本.要获取多行TextField,请使用:
Make sure you upgrade flutter to the latest release. To get multiline TextField use:
new TextField(
maxLines: null,
keyboardType: TextInputType.multiline,
)
希望这对您有帮助!
这篇关于有没有一种方法可以动态更改Flutter TextField的maxLines?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!