首页 文章

有没有办法动态更改Flutter TextField的maxLines?

提问于
浏览
4

我有一个像这样的TextField:

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表单中的长答案文本选项中,它以一行开头:
enter image description here
并在我输入时展开:
enter image description here

我已经考虑将 maxLines 设置为每次用户点击换行符时递增的计数器,但是如何确定用户何时填满了换行符?

1 回答

  • 10

    Flutter在TextField中支持多线支持 . 在提出相同的issue之后,0.0.16版本中添加了多行功能 .

    确保将flutter升级到最新版本 . 要使用多行TextField:

    new TextField(
        maxLines: null,
        keyboardType: TextInputType.multiline,
    )
    

    希望这有帮助!

相关问题