【问题标题】:Is there a way to dynamically change the Flutter TextField's maxLines?有没有办法动态改变 Flutter TextField 的 maxLines?
【发布时间】:2018-03-27 10:07:12
【问题描述】:

我有一个这样的文本字段:

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 Forms 中的 Long Answer Text 选项中,它以一行开头: 并在我键入时扩展:

我考虑过将maxLines 设置为一个计数器,每次用户点击换行时都会递增,但我如何确定用户何时填满了一行?

【问题讨论】:

    标签: textarea textfield flutter


    【解决方案1】:

    Flutter 在 TextField 中的多行支持滞后。在提出 issue 之后,0.0.16 版本中添加了多行功能。

    确保将 Flutter 升级到最新版本。要获取多行 TextField,请使用:

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

    希望这有帮助!

    【讨论】:

    • 这太好了,谢谢!事实证明,由于这个issue,我现在无法将 maxLines 设置为 null,但我希望它一旦解决就可以工作。
    • 这看起来只是一个文本文件。不像 textarea。
    【解决方案2】:

    它看起来像一个文本区域。你可以试试 ma​​xLines

    最大线数:8

    TextField(
            maxLines: 8,
            decoration: InputDecoration(hintText: "Enter your text here", border: OutlineInputBorder(),
            labelText: 'Post Body'),
        ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 2021-05-14
      • 2021-11-06
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2021-10-22
      相关资源
      最近更新 更多