【发布时间】: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