简体   繁体   English

缩小TextFormField以仅适合文本

[英]Shrink TextFormField to fit only text

How can I shrink a TextFormField to fit only the text inside it and its associated prefix/suffix icons? 如何缩小TextFormField使其仅适合其中的文本及其关联的前缀/后缀图标?

I'm trying to display a prefix (dollar) icon next to my number input. 我试图在我的数字输入旁边显示一个前缀(美元)图标。 I want the field to align the the right of the screen. 我希望该字段对齐屏幕的右侧。 My TextFormField is inside a row: 我的TextFormField位于一行内:

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: <Widget>[
    Text('TITLE'),
    Expanded(
       TextFormField(
         textAlign: TextAlign.end,
         decoration: InputDecoration(
           prefixText: '\$',
         ),
         ...
       ),
    ), 
  ],
),

While this displays the numbers to the right, the TextFormField is expanding to fill all available space, and putting the prefix text on the left: 当这在右边显示数字时,TextFormField会扩展以填充所有可用空间,并将前缀文本放在左侧:

TITLE$_____________1000 标题:$ _______________ 1000

I would like to add a spacer between the title and the form field, and let the form field occupy only the space needed to show its numbers: 我想在标题和表单字段之间添加一个空格,让表单字段仅占据显示其数字所需的空间:

TITLE<----spacer---->$1000 标题<---- spacer ----> $ 1000

My attempts so far have only resulted in the spacer sharing spacing with the form field: 到目前为止,我的尝试仅导致间隔符与表单字段共享间距:

TITLE<--spacer-->$____1000 TITLE <-spacer-> $ ____ 1000

I want to style the prefix text differently than the field text, so I can't use an inputFormatter to add my prefix. 我想给前缀文本设置样式而不是字段文本,所以不能使用inputFormatter添加前缀。 There doesn't seem to be any way to tell the form field to draw the prefix text next to the field text. 似乎没有任何办法告诉表单字段在字段文本旁边绘制前缀文本。 I suspect my issue with the prefix text is related to this bug: https://github.com/flutter/flutter/issues/18788 我怀疑我的前缀文本问题与此错误有关: https : //github.com/flutter/flutter/issues/18788

If there's a way to tell the form field to occupy the minimum amount of space needed, I can work around the prefix text bug. 如果有一种方法可以告诉表单字段占用所需的最小空间,那么我可以解决前缀文本错误。

I don't think you can shrink a TextFormField to fit only the text inside . 我认为您不能shrink a TextFormField to fit only the text inside Not easily anyway. 反正不容易。 I don't have the answer but maybe some pointers could also help ya. 我没有答案,但也许有些指针也可以帮助您。

First: you'd have to know what font you are using and calculate how long the text would be if you rendered it on the screen with the exact same font style as the input's. 首先:您必须知道使用的是哪种字体,并计算出如果您使用与输入完全相同的字体样式在屏幕上呈现文本,那么该文本将持续多长时间。 Once you have that, in theory, it should be possible to programmatically calculate the width of the input field. 理论上,一旦有了这些,就应该可以以编程方式计算输入字段的宽度。

Flutter uses RenderObject 's to keep track of sizes. Flutter使用RenderObject跟踪大小。 In fact, RenderBox is a child class of RenderObject . 事实上, RenderBox是一个子类的RenderObject You can retrieve them by via GlobalKey . 您可以通过GlobalKey检索它们。

If I remember correctly the API: 如果我没记错的话,API:

final gk = GlobalKey();
RenderBox box = gk.currentContext.findRenderObject();

Curious what solution you'll find. 好奇您会找到什么解决方案。 If you figure this out, please drop a response :) 如果您解决了这个问题,请回复:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM