简体   繁体   English

Flutter 文本字段随着用户类型展开

[英]Flutter textfield expand as user types

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.我有一个文本字段,需要如下多行,以便文本换行,但它占用了 maxLines 中定义的所有行的空间。 I want to be able to have it look like 1 line and expand as the text wraps.我希望能够让它看起来像 1 行并随着文本换行而扩展。 Any ideas of how to do this is flutter?关于如何做到这一点的任何想法是 flutter?

new TextField(
                decoration: const InputDecoration(
                  hintText: 'Reply',
                  labelText: 'Reply:',
                ),
                autofocus: false,
                focusNode: _focusnode,
                maxLines: 1,
                controller: _newreplycontroller,
                keyboardType: TextInputType.text,
              ),

Set maxLines to null and it will auto grow vertically .maxLines设置为 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 ;).它记录在案(但不完全清楚) here (编辑:我已经创建了一个 PR 来更新这个,我应该从这个问题开始;)。 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.为了弄清楚,我最终查看了TextField及其超类的代码,看看如果设置为 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.但我假设你想让它垂直自动增长。

As shown in the first answer, setting maxLines to null will do the trick.如第一个答案所示,将 maxLines 设置为 null 即可解决问题。 However this would allow the textfield to grow infinitely.然而,这将允许文本字段无限增长。

To have more control, set minLines to 1 and maxLines as needed.要获得更多控制,请根据需要将 minLines 设置为 1 和 maxLines。 Hope this helps!希望这有帮助!

Code Sample: TextField(minLines:1,maxLines:8)代码示例:TextField(minLines:1,maxLines:8)

Setting to null was working the same as setting to 1 (it grows horizontally just one line).设置为 null 与设置为 1 的工作方式相同(它仅水平增长一行)。

I think you have to set minLines and maxLines.我认为你必须设置 minLines 和 maxLines。 I'm using minLines: 1 and maxLines: 2 and when the first line reaches the end it expands another line.我正在使用 minLines: 1 和 maxLines: 2 ,当第一行到达末尾时,它会扩展另一行。 When the second line reaches the end it scroll the second to the first and creates the third line.当第二行到达末尾时,它将第二行滚动到第一行并创建第三行。 So you will have to set maxLenght too.所以你也必须设置 maxLenght 。

You can simply use this;你可以简单地使用它;

TextField get _text => TextField(
  maxLines: null,
  decoration: InputDecoration(
    constraints: BoxConstraints(
        maxHeight: responsiveHeight,
        maxWidth: responsiveWidth),
    contentPadding: EdgeInsets.symmetric(
        horizontal: responsiveHorizontalPadding,
        vertical: responsiveVerticalPadding),
    disabledBorder: InputBorder.none,
    border: InputBorder.none,
    enabledBorder: InputBorder.none,
    isDense: false,
  ));

@Rabi Roshan, @拉比罗斯汉,

As shown in the first answer, setting maxLines to null will do the trick.如第一个答案所示,将 maxLines 设置为 null 即可解决问题。 However this would allow the textfield to grow infinitely.然而,这将允许文本字段无限增长。

To have more control, set minLines to 1 and maxLines as needed.要获得更多控制,请根据需要将 minLines 设置为 1 和 maxLines。 Hope this helps!希望这有帮助!

Thank you so much!非常感谢!

I also prefer this version much more because it will only expand if the user inputs more than one line.我也更喜欢这个版本,因为它只会在用户输入多于一行时扩展。 And if it exceeds the maxLines, it will scroll vertically which is what should normally happen and what all big Chat Apps use.如果它超过 maxLines,它将垂直滚动,这是正常情况下应该发生的以及所有大型聊天应用程序都使用的。

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. textfield 并且需要如下多行以便文本换行,但是它占用了 maxLines 中定义的所有行的空间。 I want to be able to have it look like 1 line and expand as the text wraps我希望能够让它看起来像 1 行并随着文本换行而扩展

Container(
            child: new TextField (
              keyboardType: TextInputType.multiline,
              minLines: 1,
              maxLines: 10,
              decoration: new InputDecoration(
                  border: new OutlineInputBorder(
                    borderRadius: const BorderRadius.all(
                      const Radius.circular(10.0),
                    ),
                  ),
                  filled: true,
                  hintStyle: new TextStyle(color: Colors.grey[800]),
                  hintText: "Type in your text",
                  fillColor: Colors.white70),
            ),
            padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0),
          );

Using this should do the trick:使用这个应该可以解决问题:

keyboardType: TextInputType.multiline,
minLines: 1,
maxLines: 20,
maxLength: 1000,

Adding the minLines and MaxLines is important in order to get the required behaviour, whereas maxLength is a treat!添加 minLines 和 MaxLines 对于获得所需的行为很重要,而 maxLength 是一种享受!

Maybe too late, but better late than never!也许为时已晚,但迟到总比不到好!

The accepted answer works great for growing vertically.接受的答案非常适合垂直增长。 If you'd like the TextField to start small and expand horizontally as the user types, you can do the following:如果您希望TextField从小处开始并在用户键入时水平扩展,您可以执行以下操作:

IntrinsicWidth(
  child: TextField(...
),

In my case I wanted it centered on the screen (horizontally), so I did:就我而言,我希望它以屏幕为中心(水平),所以我做了:

Center(
   child: IntrinsicWidth(
      child: TextField()
   )
)

在此处输入图片说明

If you want to set a predefined height, use expands: true :如果要设置预定义的高度,请使用expands: true

SizedBox(
  height: 200,
  child: TextField(
    decoration: InputDecoration(hintText: 'Enter a message'),
    maxLines: null,
    expands: true,
  ),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM