首页 > 解决方案 > 在颤振中,如何从自动生成的文本字段列表中保存数据

问题描述

在我的应用程序中,我有一个人可以选择的卧室列表,如果用户选择 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"),

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

标签: androidflutterdartflutter-test

解决方案


当索引没有控制器时,您可以使用 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;
}

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


推荐阅读