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

Flutter中如何设置TextFormField内部文本的起始缩进位置?

解决方案

方案1:基于现有Stack实现快速修改

直接给TextFormField添加水平内边距,把输入内容的展示区域左边界向右推24pt即可,同时为了保证文字居中效果对称,右侧也设置同等的右内边距:

Stack(
  children: [
    TextFormField(
      autofocus: true,
      autocorrect: false,
      maxLines: 1,
      textAlign: TextAlign.center,
      // 新增内容如下
      decoration: const InputDecoration(
        contentPadding: EdgeInsets.symmetric(horizontal: 24),
        // 可根据原有设计调整边框样式,不需要默认边框可以设为InputBorder.none
        border: InputBorder.none,
      ),
    ),
    Positioned(
      bottom: 14,
      left: 0,
      child: Text("Rp"),
    ),
  ],
)

方案2:原生规范实现(无需Stack嵌套)

Flutter的TextFormField自带前缀组件属性prefix,不需要手动用Stack做定位,多设备适配性更强,不会出现定位错位、重叠问题:

TextFormField(
  autofocus: true,
  autocorrect: false,
  maxLines: 1,
  textAlign: TextAlign.center,
  decoration: InputDecoration(
    // 内置Rp前缀
    prefix: Padding(
      padding: const EdgeInsets.only(right: 8),
      child: Text("Rp"),
    ),
    // 调整整体内边距,保证输入内容起始位置符合要求
    contentPadding: const EdgeInsets.symmetric(horizontal: 24),
    border: InputBorder.none,
  ),
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 19:24:04