flutter - 颤振:最大长度不适用于文本字段
问题描述
我正在创建自己的 PinCode Widget,基本上当我按下按钮时,按钮的值将传递给 TextField。例如,如果我按下按钮“1”,它会将值“1”传递给 TextField。
我有配置 Maxlength = 4 的 TextField。问题是当我写超过 4 时它仍然在写并且不会阻止它写 TextField。
我做错了吗?
编辑
我尝试使用它但不阻止写入。
TextFormField(
inputFormatters: [
LengthLimitingTextInputFormatter(4),
]
)
这是我的代码
class _PinCodeScreenState extends State<PinCodeScreen> {
TextEditingController _pinCodeController = TextEditingController();
bool _obsecureToggle = false;
@override
void dispose() {
_pinCodeController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
double mqHeight = MediaQuery.of(context).size.height;
double mqWidth = MediaQuery.of(context).size.width;
final textTheme = Theme.of(context).textTheme;
return Scaffold(
backgroundColor: Theme.of(context).primaryColor,
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
flex: 4,
child: Container(
height: double.infinity,
child: TextFormField(
maxLengthEnforced: true,
maxLength: 4,
textAlign: TextAlign.center,
controller: _pinCodeController,
readOnly: true,
decoration: InputDecoration(
border: InputBorder.none,
),
style: textTheme.display3
.copyWith(color: Colors.white, letterSpacing: 40),
),
),
),
Flexible(
flex: 14,
child: Container(
height: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: .5),
borderRadius: BorderRadius.circular(30),
),
child: Wrap(
alignment: WrapAlignment.spaceEvenly,
spacing: 4,
children: _listPinCodeNumber
.map(
(f) => ButtonPinCode(
child: f.number == "backspace"
? Icon(Icons.backspace)
: Text(
f.number == "backspace" ? "<" : f.number,
style: textTheme.display1
.copyWith(color: Colors.black),
),
mediaQueryHeight: mqHeight,
mediaQueryWidth: mqWidth,
onPressed: () => _actionPinCode("${f.number}"),
),
)
.toList(),
),
),
),
],
),
),
);
}
_actionPinCode(parameter) {
switch (parameter) {
case "backspace":
setState(() {
if (_pinCodeController.text.length <= 1) {
_pinCodeController.text = "";
print(_pinCodeController.text);
} else {
_pinCodeController.text = _pinCodeController.text
.substring(0, (_pinCodeController.text.length - 1));
print(_pinCodeController.text);
}
});
break;
default:
setState(() {
if (_pinCodeController.text.length != 0) {
_pinCodeController.text += parameter;
print(_pinCodeController.text);
} else {
_pinCodeController.text = parameter;
print(_pinCodeController.text);
}
});
}
}
}
解决方案
尝试这个
TextFormField(
maxLength: 4,
inputFormatters: [
LengthLimitingTextInputFormatter(4),],
textAlign: TextAlign.center,
controller: _pinCodeController,
readOnly: true,
decoration: InputDecoration(
border: InputBorder.none,
hintText: '',
counterStyle: TextStyle(color: Colors.black)
),
style: TextStyle(color: Colors.black, letterSpacing: 40),
),
推荐阅读
- angular - 我需要进行两次订阅等待
- c++ - 在编译时将枚举值映射到带有模板的相应类型?
- javascript - 如何为更改文件夹html +纯JS创建简单的分页
- android - 如何从网格视图将 url 图像显示到 Android Studion 中的 ViewPager
- python - cvxpy 正在解决产生空答案
- ios - 如何在swift中执行SQL where In查询并绑定语句中的参数?
- javascript - Discord.js 在事件处理程序上关闭未处理的承诺拒绝
- python - Spotify Web API 未检测到 POST 请求的正文
- python - Selenium Web Scraping find_element_by_xpath 总是捕获会话字符串
- generics - F# 部分应用程序函数包装器不保留参数函数的返回类型泛型