[英]Flutter TextFormField not updating it's value
i'va made a TimePicker that will pop up when user taps on textFormField
it's working fine but the only problem is that i want to update textFormField
value by the one selected from timePicker
I've declared a controller
and initilize a selectedTimer
我制作了一个 TimePicker,当用户点击
textFormField
时它会弹出它工作正常但唯一的问题是我想用从timePicker
中选择的值更新textFormField
值我已经声明了controller
并初始化了一个selectedTimer
TimeOfDay selectedTime = TimeOfDay(hour: 0, minute: 0);
TextEditingController _timeController = TextEditingController();
And this is the code of TextFormField
这是
TextFormField
的代码
TextFormField(
controller: _timeController,
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
_selectTime(context);
},
),
And this is the code for the method _selectTime
这是方法
_selectTime
的代码
_selectTime(BuildContext context) async {
final TimeOfDay? timeOfDay = await showTimePicker(
context: context,
initialTime: selectedTime,
initialEntryMode: TimePickerEntryMode.dial,
);
if (timeOfDay != null && timeOfDay != selectedTime) {
setState(() {
selectedTime = timeOfDay;
TextEditingValue(text: timeOfDay.toString());
});
}
}
Do as follows:如下操作:
_timeController.text=timeOfDay.toString();
selectTime(BuildContext context) async {
final TimeOfDay? timeOfDay = await showTimePicker(
context: context,
initialTime: selectedTime,
initialEntryMode: TimePickerEntryMode.dial,
);
if (timeOfDay != null && timeOfDay != selectedTime) {
setState(() {
selectedTime = timeOfDay;
_timeController.text=timeOfDay.toString();
TextEditingValue(text: timeOfDay.toString());
});
}
} }
remove this line TextEditingValue(text: timeOfDay.toString());
删除此行
TextEditingValue(text: timeOfDay.toString());
and replace by this _timeController.text = selectedTime.toString();
并替换为这个
_timeController.text = selectedTime.toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.