我创建了一个名为的文本字段chargesMax
并添加了一个ListTile
包含复选框和两个名为charges
and的文本字段的小部件time
。我想当用户在文本字段上输入一些值然后chargesMax
检查任何文本字段应该包含收费文本字段的值。然后如果用户修改然后对文本字段收费,那么只有这个字段会修改而不是所有的文本字段。checkbox
charges
listTile
这是我的代码
charges.text = chargesMax.text;
在上面的这一行中,我正在尝试做我想做的事情,但是当我选中任何 listTile 的复选框时,chargesMax 的值会显示为收费文本字段,但复选框的所有文本字段都会在选中任何复选框时进行修改。然而,我可以单独检查复选框。
完整代码:
TextEditingController time = TextEditingController();
TextEditingController charges = TextEditingController();
TextEditingController chargesMax = TextEditingController();
List _selectedCities = [];
void _onCitySelected(bool selected, cityId) {
if (selected == true) {
setState(() {
charges.text = chargesMax.text;
_selectedCities.add(cityId);
print(_selectedCities);
});
} else {
setState(() {
_selectedCities.remove(cityId);
});
}
}
Widget build(BuildContext context) {
return SafeArea(
child: Container(
child: Column(
children: [
//chargesMax textfield
textformfieldCustomwithouticon(
context,
TextInputType.number,
MediaQuery.of(context).size.width * 0.9,
chargesMax, (String value) {
setState(() {
chargesMax.text = value;
chargesMax.selection = TextSelection.fromPosition(
TextPosition(offset: chargesMax.text.length));
});
}, (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
}, 'Enter delivery charges for maximumn destination',
'Delivery charges', 55.0),
SizedBox(
height:
MediaQuery.of(context).size.height * 0.6,
child: ListView.builder(
itemCount: books.length,
itemBuilder: (context, index) {
final book = books[index];
if (isLoading) {
return showCircularLoader(context);
} else {
return Padding(
padding: const EdgeInsets.fromLTRB(
0, 10, 0, 0),
child: Column(
children: [
buildCitesList(book),
SizedBox10(),
Divider(),
SizedBox10()
],
),
);
//
}
}),
),
Widget buildCitesList(Book book) => ListTile(
leading: checkboxCustom(context, _selectedCities.contains(book.id),
(bool? selected) {
if (selected != null) {
setState(() {
_onCitySelected(selected, book.id);
});
}
}),
title: Text(book.title, style: GoogleFonts.montserrat()),
subtitle: Padding(
padding: const EdgeInsets.only(top: 10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
//charges textfield
textformfieldCustomwithouticon1(
context,
TextInputType.number,
MediaQuery.of(context).size.width * 0.3,
charges, (String value) {
setState(() {
charges.text = value;
charges.selection = TextSelection.fromPosition(
TextPosition(offset: charges.text.length));
});
}, (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
}, 'Charges', 'Charges', 10.0),
SizedBox(
width: 10,
),
textformfieldCustomwithouticon1(
context,
TextInputType.number,
MediaQuery.of(context).size.width * 0.3,
time, (String value) {
setState(() {
time.text = value;
time.selection = TextSelection.fromPosition(
TextPosition(offset: time.text.length));
});
}, (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
}, 'Time', 'Time', 10.0),
// SizedBox10(),
],
),
),
);
这是输出的快照
请帮助我如何做到这一点。