问题描述
在颤动中,TextField没有固有宽度;它只知道如何将自身调整为其父容器的整个宽度.如何将宽度设置为所包含文本的宽度.
In flutter, TextField doesn't have an intrinsic width; it only knows how to size itself to the full width of its parent container. How do I set the width to the width of the contained text.
我尝试将TextField放在容器中
I tried putting the TextField inside a container
如此处概述如何更新Flutter TextField的高度和宽度?
new Container(
width: 100.0,
child: new TextField()
)
我希望TextField的宽度与它包含的文本的宽度匹配.输入文本时,TextField应该变宽,而删除文本时,TextField应该变窄.
I expect the width of the TextField to match the width of the text it contains. The TextField should grow wider as text is typed, and shrink narrower as text is deleted.
推荐答案
Flutter具有 IntrinsicWidth 为您进行计算.只需将TextField
或TextFormField
包裹在其中,如下所示:
Flutter has IntrinsicWidth to do the calculation for you. Just wrap your TextField
or TextFormField
in it as follow:
IntrinsicWidth(child: TextField())
这篇关于Flutter TextField宽度应与所包含文本的宽度匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!