Flutter TextField 宽度应与包含文本的宽度匹配 [英] Flutter TextField width should match width of contained text
问题描述
在 Flutter 中,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 应该在输入文本时变宽,并在删除文本时缩小.
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 宽度应与包含文本的宽度匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!