简体   繁体   English

[Flutter/Dart]:如何动态改变Form中TextFormField的键盘类型?

[英][Flutter/Dart]: How to dynamically change the keyboard type for a TextFormField in a Form?

I have a Widget that renders a ListView of TextFormFields to input data.我有一个小部件,它呈现 TextFormFields 的 ListView 以输入数据。 However, I want to change the keyboard type for one of my textfields dynamically.但是,我想动态更改我的文本字段之一的键盘类型。 Here is an overview of the Widget:以下是小部件的概述:


class CreateTodo extends StatefulWidget {
  @override
  _CreateTodoState createState() => _CreateTodoState();
}

class _CreateTodoState extends State<CreateTodo> {
  final _formKey = GlobalKey<FormState>();
  final double formWidth = 175.0;

  final Map<String, Icon> itemsMap = {
    'Heading': Icon(Icons.title),
    'Description': Icon(Icons.edit),
    'Date': Icon(Icons.calendar_today)
  };

  final items = ['Heading', 'Description', 'Date'];

  _changeKeyboardType(entry) {
    if (entry == "Description") {
      return TextInputType.multiline;
    }

    return TextInputType.text;
  }

  ListView createListItems() {
    return ListView.builder(
      padding: EdgeInsets.only(
        left: 50.0,
        right: 50.0,
        top: 20.0
      ),
      scrollDirection: Axis.vertical,
      itemCount: 3,
      itemBuilder: (context, index) {
        var currentHeading = this.items[index];
        var currentIcon = this.itemsMap[currentHeading];

        return Padding(
          padding: EdgeInsets.only(bottom: 50.0),
          child: new TextFormField(
              validator: (input) {
                if (input.isEmpty) {
                  return 'You must enter a ${currentHeading.toLowerCase()} for your to-do';
                }
              },
              keyboardType: this._changeKeyboardType(currentHeading),
              decoration: new InputDecoration(
                prefixIcon: currentIcon,
                labelText: currentHeading,
                border: OutlineInputBorder(),
              ),
            )
        );
      }
    );
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      autovalidate: true,
      child: Expanded(
        child: (this.createListItems())
      )
    );
  }
}

The main line that isn't working is: keyboardType: this._changeKeyboardType(currentHeading) ... Am I approaching this problem the wrong way?不起作用的主线是: keyboardType: this._changeKeyboardType(currentHeading) ...我是否以错误的方式处理这个问题? Any direction will be super helpful.任何方向都会非常有帮助。 I basically want the textFormFeld for the description being multiline so users can have a better visualization of their description.我基本上希望描述的 textFormFeld 是多行的,以便用户可以更好地可视化他们的描述。

My working solution for now:我现在的工作解决方案:

// Where entry is an object containing its focusNode and textInputType
entry.focusNode.unfocus();
setState(() {
  entry.textInputType = type;
});
// The delay was necessary, otherwise only the 2nd tap would actually change
Future.delayed(const Duration(milliseconds: 1), () {
  FocusScope.of(context).requestFocus(entry.focusNode);
});

Only downside I couldn't fix so far is that the keyboard is disappearing before it re-appears with the new TextInputType .到目前为止我无法修复的唯一缺点是键盘在使用新的TextInputType重新出现之前消失了。

Google's Spreadsheet app does it instantaneous without the close, would love to know if and how that'll be doable in Flutter. Google 的电子表格应用程序无需关闭即可立即完成,很想知道这是否以及如何在 Flutter 中可行。

You'd have to change your logic as my example is asynchronous.您必须更改逻辑,因为我的示例是异步的。
Let me know if that helps or if you need more help.如果这有帮助,或者您需要更多帮助,请告诉我。
Or, if you've already figured it out, how you did it或者,如果你已经想通了,你是如何做到的

Credits: It's inspired by https://stackoverflow.com/a/58498474/3484824积分:它的灵感来自https://stackoverflow.com/a/58498474/3484824

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

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