Flutter文本字段随用户类型而扩展 [英] Flutter textfield expand as user types

查看:96
本文介绍了Flutter文本字段随用户类型而扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本字段,需要具有以下多行内容,以便文本自动换行,但是它占用了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆