简体   繁体   English

在 listview flutter 内的 textformfield 中按下数字后键盘关闭

[英]keyboard close after pressing a number in textformfield inside listview flutter

I have a ReorderableListView that calls a function that generates cards with TextTormFields, two texts and one numeric, the problem is that when focused on the numeric textfield after pressing any character on the keyboard, the keyboard close.我有一个 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),
            )
          ],)
        ]),
      ),
    )
);

And TextfieldWidget is just an easy way to call textfields, basically this:而 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;

} }

Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance.提前致谢。

I've added a GlobalKeys list at the start of the class and added keys from the listview我在课程开始时添加了一个 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);
              },
            ),
          )

And used it in te TextFormWidget.并在 te TextFormWidget 中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM