flutter - 如何从 TextField 中删除内容填充?

我是 Flutter 的新手,我正在创建登录表单,因为我使用了 TextField 并添加了前缀图标,但我在文本字段(用户 ID 和 pin)之间和前缀图标之前得到了一些额外的空格。我也尝试过 InputDecorationTheme 但它没有用。

请告诉我如何删除或减少空间。??

下面是我的代码:

TextField(
  maxLength: 8,
  keyboardType: TextInputType.number,
  decoration: InputDecoration(
    hintText: hint,
    hintStyle: TextStyle(fontSize: 12.0),
    prefixIcon: Icon(icon),
    counterText: '',
  ),
)

最佳答案

更新(2022 年 8 月):仍与 Flutter 3.0.5 相同
更新(2021 年 4 月):仍在 Flutter 2.0.4 中工作

从 Flutter 1.17.5 开始(在 2.X 中仍然相同)要完全手动删除或操作填充,首先您必须设置 isDense: true 然后您可以调整 contentPadding 你想要的,或者在父小部件上应用填充。

// using theme
ThemeData(
  inputDecorationTheme: InputDecorationTheme(
     isDense: true,// this will remove the default content padding
     // now you can customize it here or add padding widget
     contentPadding: EdgeInsets.symmetric(horizontal: 0, vertical: 0),
     ...
  ),
)

// per widget
TextField(
   decoration: InputDecoration(
     isDense: true,
     contentPadding: EdgeInsets.symmetric(horizontal: 0, vertical: 0),
     ...
   ),
)

正如 Ataberk 的评论中所述你也可以使用 contentPadding: E​​dgeInsets.zero

TextField(
   decoration: InputDecoration(
     isDense: true,
     contentPadding: EdgeInsets.zero,
     ...
   ),
)

https://stackoverflow.com/questions/53644897/

相关文章:

dart - 如何创建 "fake" Dart :io File from in-memory by

dart - Flutter 自定义范围 slider

dart - Flutter 不会使用不同的参数重建相同的小部件

android - Flutter 项目超过 .dex 方法引用计数限制

android - 如何禁用使用 Flutter 1.0 在 Google map 中点击标记时出现

dart - 桶填充 flutter

dart - 在 RichText 小部件中向 TextSpan 背景添加额外的填充

text - 如何使用 Flutter 编写带有要点的段落?

flutter - Flutter 支持虚拟现实还是增强现实?

flutter - 从网络加载图像时如何修复 Listview 滚动卡顿