当输入文本时自动展开的Flutter文本字段,当达到一定高度时开始滚动文本
问题内容:
我已经尝试了Flutter TextField的许多配置,但无法弄清楚如何构建此配置。
我正在寻找一个最初为单行的文本字段,它会随着文本输入到其中而自动扩展,然后在某个时候开始自动滚动。
这可以通过使用maxLines:null属性部分实现。但是,当输入大量文本时,文本字段中的文本本身就会溢出。
而且,如果将maxLines设置为一个值,则整个文本字段本身会扩展到许多行以开始而不是从一行开始。
是否有办法在某些点上限制文本字段的高度,就像在许多聊天应用程序(如WhatsApp和电报)中所做的那样。
问题答案:
Container(
child: new ConstrainedBox(
constraints: BoxConstraints(
maxHeight: 300.0,
),
child: TextField(
maxLines: null,
),
),
),
),
)
在较旧的Flutter版本中,
Container(
child: new ConstrainedBox(
constraints: BoxConstraints(
maxHeight: 300.0,
),
child: new Scrollbar(
child: new SingleChildScrollView(
scrollDirection: Axis.vertical,
reverse: true,
child: new TextField(
maxLines: null,
),
),
),
),
)