Flutter中obscureText为true时如何移除TextField默认图标
解决方案
要移除TextField在obscureText = true时显示的默认图标,只需在TextField中添加两个属性:enableSuggestions: false和autocorrect: false。这两个属性会禁用系统默认的密码输入相关提示图标,让你的自定义suffixIcon正常显示。
修改后的完整代码如下:
Padding( padding: const EdgeInsets.fromLTRB(20, 0, 20, 0), child: TextField( obscureText: _obscured, controller: _password, textInputAction: TextInputAction.done, keyboardType: TextInputType.visiblePassword, // 添加以下两行属性 enableSuggestions: false, autocorrect: false, decoration: InputDecoration( icon: Icon(Icons.lock, size: 32, color: Colors.black), labelText: "Password", labelStyle: TextStyle( color: Colors.black), suffixIcon: IconButton( icon: Icon(_obscured ? Icons.visibility_off : Icons.visibility), onPressed: () { setState(() { _obscured = !_obscured; }, ); }, ), ), ), ),
原理说明:当obscureText设为true且keyboardType为TextInputType.visiblePassword时,Flutter默认会启用输入建议和自动校正,这会触发系统显示默认的密码图标。通过禁用这两个属性,就能去掉这个默认图标,只保留你自定义的后缀按钮。
内容的提问来源于stack exchange,提问作者si pinter
相关产品推荐
相关产品推荐

