android - 在listview颤动中的textformfield中按下数字后键盘关闭
问题描述
我有一个 ReorderableListView 调用一个函数,该函数生成带有 TextTormFields、两个文本和一个数字的卡片,问题是当按下键盘上的任何字符后专注于数字文本字段时,键盘关闭。
Expanded(
child: ReorderableListView(
onReorder: _onReorder,
scrollDirection: Axis.vertical,
padding: const EdgeInsets.symmetric(vertical: 8.0),
children: List.generate(
itemsList.length,
(index) {
return _buildItemCard(itemsList[index],index,_numberController);
},
),
)
_buildItemCard(Map item, int index){
return Container(
width: MediaQuery.of(context).size.width * 1,
height: 200,
key: Key(index.toString()),
child: Card(
elevation: 2,
child: Container(
decoration: BoxDecoration(
color: colorPrimaryWhite,
border: Border.all(color: Colors.black, width: 0.5),
borderRadius: _borderRadius,
),
child: Column(children: <Widget>[
Row(children: [
Container(
width: MediaQuery.of(context).size.width * 0.45,
height: 100,
child: TextFormWidget(
L10n.of(context).field1Text,
text: item["field1"],
maxLines: 2,
icon: Icons.description,
validator: (text) {
bool result = Validator.isNotNullNorEmpty(text);
if (result) {
item["field1"] = text;
}
return result;
},
autovalidate: true,
validationErrorText: L10n.of(context).formsFillThisFieldError,
),
),
Container(
width: MediaQuery.of(context).size.width * 0.45,
height: 100,
child: TextFormWidget(
L10n.of(context).field2Text,
text: item["field2"],
maxLines: 2,
validator: (text) {
bool result = Validator.isNotNullNorEmpty(text);
if (result) {
item["field2"] = text;
}
return result;
},
autovalidate: true,
validationErrorText: L10n.of(context).formsFillThisFieldError,
),
)
]),
Row(children: [
Container(
width: MediaQuery.of(context).size.width * 0.45,
height: 80,
child: TextFormWidget(
"",
text: item["field3"].toStringAsFixed(0),
maxLines: 1,
maxLength: 5,
icon: Icons.attach_money,
onChanged: (v, c) {
bool result = Validator.isPositiveDouble(v);
if (result) {
item["field3"] = double.parse(v);
}
setState(() {});
},
inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly,
LengthLimitingTextInputFormatter(15),
],
keyboardType:
TextInputType.numberWithOptions(decimal: true, signed: false),
),
),
Flexible(flex: 1, child: new Container()),
IconButton(
icon: Icon(Icons.delete),
iconSize: 40,
onPressed: () => _deleteDialog(index),
)
],)
]),
),
)
);
而 TextfieldWidget 只是一种调用文本字段的简单方法,基本上是这样的:
TextFormField _buildTextFormField() {
TextFormField field = TextFormField(
controller: widget.controller ?? null,
key: widget.localKey ?? null,
validator: (value) {
if (widget.validator != null) {
return widget.validator(value) ? null : widget.validationErrorText;
}
return null;
},
onTap: widget.onTap != null ? ()=> widget.onTap(widget.controller) : null,
onChanged: widget.onChanged != null ? (val)=> widget.onChanged(val, widget.controller) : null,
enabled: widget.enabled ?? true,
initialValue: widget.text ?? null,
maxLength: widget.maxLength ?? null,
autovalidate: widget.autovalidate ?? false,
maxLines: widget.maxLines ?? null,
decoration: InputDecoration(
icon: widget.icon != null ? Icon(widget.icon) : null,
hintText: widget.hintText,
labelText: widget.label,
),
inputFormatters: widget.inputFormatters ?? null,
keyboardType: widget.keyboardType ?? null,
obscureText: widget.obscureText ?? false,
);
return field;
}
任何帮助,将不胜感激。提前致谢。
解决方案
我在课程的开头添加了一个 GlobalKeys 列表,并从列表视图中添加了键
Expanded(
child: ReorderableListView(
onReorder: _onReorder,
scrollDirection: Axis.vertical,
padding: const EdgeInsets.symmetric(vertical: 8.0),
children: List.generate(
itemsList.length,
(index) {
_keys.add(GlobalKey<FormFieldState>());
return _buildItemCard(itemsList[index],index,_numberController);
},
),
)
并在 te TextFormWidget 中使用它。
推荐阅读
- python - 编写一个 Python 程序,请求一个单词(小写字母)作为输入并将该单词翻译成 Pig Latin
- php - 在教义中设置@JoinColumn 名称
- javascript - JQuery下拉单击功能在asp.net mvc中不起作用
- codeigniter - 如何在codeiginter中上传多张照片?
- node.js - Windows 10 无法再通过 node.js 运行 javascript 文件或显示版本或 npm
- c# - 如何确定自定义线程池的#threads?
- html - 如何使用伪选择器模糊正方形中的文本?
- html - 如何将 adobe xd 转换为 html css (vue)?
- swift - Swift 约束协议?
- python - 在 Pyplot 中显示预测的分割输出数据