首页 > 解决方案 > Flutter TextFormField:在输入字段左侧对齐文本描述

问题描述

美好的一天,我正在 Flutter 中创建 TextFormFields,我有点沮丧,因为我不能在输入输入的左侧有文本,而留在那里。

当您输入该字段时,hintText 会消失。

prefixText 仅在您输入该字段后出现。

prefixIcon 完全符合我的要求,但我想要 Text 而不是图标。

在此处输入图像描述

上图使用了prefixIcon 和hintText。

有没有人建议如何用永久文本替换我目前拥有的图标?感谢您的任何帮助。

标签: flutterdartflutter-layout

解决方案


尝试prefixText在装饰中使用该属性。

TextField(
   controller: _textController,
   keyboardType: TextInputType.number,

   decoration: InputDecoration(            
     hintText: 'Enter a valid ZIP-Code',
     errorText: _hasInputError ? "ZIP-Code is too short" : null,
     counterText: _textController.text.length.toString(),
     helperText: "ZIP-Code of shipping destination",
     labelText: "ZIP-Code",
     prefixText: "DE - ",
     suffixIcon: Icon(Icons.airport_shuttle), 
   ),
),

参考这个链接

我认为您在使用prefixIcon时不能使用decoration.


推荐阅读