You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter TextField文本消失问题求助:输入多位数字时文本消失

Fix for TextField Text Disappearing with 4-Digit Numbers in Flutter

Hey there! Let's get that TextField issue sorted out—you’re spot-on that font size and container constraints are causing your 4-digit numbers to vanish. Here’s how to fix it step by step:

Why this happens

Your TextField is wrapped in a Container with a fixed width of 49 and top padding of 15. When you type 4 digits, the default font size makes the text wider than the container’s available space, and the top padding squeezes the vertical room too—so the text gets clipped or pushed out of view entirely.

Solutions to try

1. Adjust the font size to fit the container

Shrink the font size just enough so 4 digits can fit within the 49-width container. Add or tweak the fontSize property in your TextStyle:

style: TextStyle(
  color: Color(0xFF000080),
  fontWeight: FontWeight.w600,
  fontSize: 16, // Start with this and adjust as needed
),

2. Optimize container padding and width

That top padding of 15 is taking up a lot of vertical space, which might also be contributing to text getting cut off. Try reducing it, and if you can, widen the container slightly to give 4 digits more room:

Container(
  width: 55, // Slightly wider to accommodate 4 digits
  height: 40,
  padding: EdgeInsets.only(top: 8, left: 7, right: 5, bottom: 5), // Less top padding
  decoration: BoxDecoration( 
    color: Colors.white, 
    border: Border.all(color: Colors.grey[800]), 
    borderRadius: BorderRadius.circular(3), 
  ), 
  child: TextField(/* ... rest of your TextField code ... */),
)

3. Set text alignment for better fit

Align the text to center or left to make the most of the available space in the container. Add the textAlign property to your TextField:

TextField(
  textAlign: TextAlign.center, // Centers the digits to prevent clipping on edges
  keyboardType: TextInputType.number,
  // ... rest of your TextField code ...
)

4. Limit input length (optional)

If you only ever need 3-digit numbers, you can restrict input to avoid the problem entirely by adding maxLength:

TextField(
  maxLength: 3,
  keyboardType: TextInputType.number,
  // ... rest of your TextField code ...
)

Start with adjusting the font size and padding first—those are the quickest fixes. If you need to support 4-digit numbers, widening the container will do the trick.

内容的提问来源于stack exchange,提问作者Diificult Ranger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:25:37