[英]How to display a textfield value in other textfield when checkbox is checked in flutter using ListTile
I created a textfield named chargesMax
and add a ListTile
widget which contains checkbox and two textfield named charges
and time
.我创建了一个名为
chargesMax
的文本字段,并添加了一个ListTile
小部件,其中包含复选框和两个名为charges
和time
的文本字段。 I want when user enter some value on chargesMax
textfield and then check any checkbox
then charges
textfield should contain the value of chargesMax textfield.我希望用户在
chargesMax
文本字段上输入一些值,然后选中任何checkbox
,然后charges
文本字段应该包含chargesMax 文本字段的值。 and then if user modify then charges textfield then only this field will modify not all the textfield of listTile
.然后如果用户修改然后对文本字段收费,那么只有这个字段会修改不是
listTile
的所有文本字段。
here is my code这是我的代码
charges.text = chargesMax.text;
In this above line i'm trying to do what i want but when i check the checkbox of any listTile, value of chargesMax display to charges textfield but all textfield of checkbox modify on check on any checkbox.在上面的这一行中,我正在尝试做我想做的事情,但是当我选中任何 listTile 的复选框时,chargesMax 的值会显示为收费文本字段,但复选框的所有文本字段都会在选中任何复选框时进行修改。 wherease, i can check the checkbox indiviually.
然而,我可以单独检查复选框。
Full code:完整代码:
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(),
],
),
),
);
here is the snap of output这是 output 的快照
please help how i can do this.请帮助我如何做到这一点。
.addListener
on chargesMaxController
for chargesMax
. .addListener
上的chargesMaxController
用于chargesMax
。
Here is the simplified version of your desired output.这是您想要的 output 的简化版本。
class TimeToCode extends StatefulWidget {
TimeToCode({Key? key}) : super(key: key);
@override
_TimeToCodeState createState() => _TimeToCodeState();
}
class _TimeToCodeState extends State<TimeToCode> {
TextEditingController chargesMaxController = TextEditingController();
TextEditingController controller1 = TextEditingController();
TextEditingController controller2 = TextEditingController();
bool tile1CheckBox = false;
bool tile2CheckBox = false;
@override
void initState() {
super.initState();
chargesMaxController.addListener(() {
if (tile1CheckBox) {
setState(() {
controller1.text = chargesMaxController.text;
});
}
if (tile2CheckBox) {
setState(() {
controller2.text = chargesMaxController.text;
});
}
});
}
@override
void dispose() {
chargesMaxController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Text("chargesMax "),
TextField(
controller: chargesMaxController,
),
ListTile(
leading: Checkbox(
value: tile1CheckBox,
onChanged: (value) {
setState(() {
tile1CheckBox = value!;
});
if (value!) {
setState(() {
controller1.text = chargesMaxController
.text; // handle Ui update on checkedBOx value changes
});
}
},
),
title: TextField(
controller: controller1,
),
),
ListTile(
leading: Checkbox(
value: tile2CheckBox,
onChanged: (value) {
setState(() {
tile2CheckBox = value!;
});
if (value!) {
setState(() {
controller2.text = chargesMaxController
.text; // handle Ui update on checkedBOx value changes
});
}
},
),
title: TextField(
controller: controller2,
),
)
],
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.