【问题标题】:in flutter, how do I save data from a list of autogenerated textfields在颤振中,如何从自动生成的文本字段列表中保存数据
【发布时间】:2021-09-26 11:01:53
【问题描述】:

在我的应用程序中,我有一个人可以选择的卧室列表,如果用户选择 1 间和 2 间卧室,则从这些选择中生成两个文本字段

   _apartmentBedrooms.length > 0
                  ? Container(
                      child: ListView.builder(
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemCount: _apartmentBedrooms.length,
                        itemBuilder: (context, index) {
                          _controllers.add(new TextEditingController());
                          print(_controllers[index]);
                          return BlocBuilder<AddPropertyBloc, AddPropertyState>(
                            builder: (context, state) {
                              return Padding(
                                padding: const EdgeInsets.only(left: 15, right: 15, top: 10),
                                child: Container(
                                  padding: EdgeInsets.only(right: 15, left: 10),
                                  height: 40,
                                  decoration: BoxDecoration(
                                    color: ColorConfig.smokeLight,
                                    borderRadius: BorderRadius.circular(5),
                                  ),
                                  child: Padding(
                                    padding: const EdgeInsets.only(bottom: 5.0),
                                    child: TextFormField(
                                      controller: _controllers[index],
                                      onFieldSubmitted: (value) {
                                        // BlocProvider.of<AddPropertyBloc>(context).add(EnteredPriceEvent(price: value));
                                        print(_controllers[index].text);
                                        prices.add(_controllers[index].text);
                                        print(prices);
                                      },
                                      keyboardType: TextInputType.number,
                                      style: TextStyle(
                                        fontFamily: FontConfig.regular,
                                        fontSize: Sizeconfig.small,
                                        color: ColorConfig.dark,
                                      ),
                                      decoration: InputDecoration(
                                        hintText: _apartmentBedrooms[index].toString() + " bedroom Price*",
                                        hintStyle: TextStyle(
                                          fontFamily: FontConfig.regular,
                                          fontSize: Sizeconfig.small,
                                          color: ColorConfig.dark,
                                        ),
                                        border: InputBorder.none,
                                      ),
                                    ),
                                  ),
                                ),
                              );
                            },
                          );
                        },
                      ),
                    )
                  : Text("choose bedrooms first"),

我有一个为每个选定的卧室生成的 textEditingController 列表。问题是如果我使用 onChanged 它最终会从任何更改的项目中添加无限项到列表中,当我使用 onfieldsubitted 并更改任何值时,它们会作为我不想要的新项目添加到列表中。我想保存每间卧室的价格,无论是更改还是添加。我怎样才能做到这一点。

【问题讨论】:

    标签: android flutter dart flutter-test


    【解决方案1】:

    当索引没有控制器时,您可以使用 Map 简单地生成控制器。像这样的:

    Map<int, TextEditingController> _mapControllers = Map();
    
    TextEditingController _controllerOf(int index) {
        var item = _mapControllers[index];
        if(item == null) {
           item = TextEditingController();
           _mapControllers[index] = item;
        }
        return item;
    }
    

    你可以像这样使用它:

    TextFormField(
        controller: _controllerOf(index),
        ...
    )
    

    然后,您可以通过以下方式从控制器中获取文本:

    List<String> _textsOf(Map<int, TextEditingController> mapControllers) {
        // Sort map by index of the builder.
        var sortedKeys = mapControllers.keys.toList()..sort();
    
        List<String> texts = [];
        for(var key in sortedKeys) {
           texts.add(mapControllers[key].text);
        }
    
        return texts;
    }
    

    请注意,我还没有测试代码。

    【讨论】:

      猜你喜欢
      • 2020-04-30
      • 2022-12-09
      • 2020-07-28
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      相关资源
      最近更新 更多