This is my view
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
flex: 3,
child: Obx(
() => TextFieldWidget(
control: from,
obsure: false,
text: DateFormat('dd-MM-yyyy').format(controller.dateRange.value.start).toString(),
callBackFunc: () async{
controller.showDatePicker();
},
hint: true,),
),
),
const Expanded(flex: 1, child: Icon(Icons.arrow_right_alt)),
Expanded(
flex: 3,
child: Obx(
() => TextFieldWidget(
control: to,
obsure: false,
text: DateFormat('dd-MM-yyyy').format(controller.dateRange.value.end).toString(),
callBackFunc: () async{
controller.showDatePicker();
},
hint: true,),
),
),
//TextFieldWidget(control:to_controller,obsure:false,text:'To'),
],
)};
my controller
Future showDatePicker() async{
final NewdateRange = await showDateRangePicker(
context: Get.context!,
firstDate: DateTime(DateTime.now().year-1),
lastDate: DateTime(DateTime.now().year 1),
initialDateRange: dateRange.value,
);
if(NewdateRange != null && NewdateRange != dateRange.value){
dateRange.value = NewdateRange;
update();
}}
textfield widget I created
Widget build(BuildContext context) {
return TextFormField(
controller: control,
obscureText: obsure,
onTap: callBackFunc,
onChanged: (text) => TextFieldWidget,
style: TextStyle(fontSize: 13),
decoration: InputDecoration(
labelText: text,
hintText: hint?text:'',
border: OutlineInputBorder(),
contentPadding: const EdgeInsets.all(10),
),
)};
I am flutter newbie, when I tab on textfield, datepicker showed, but when I saved the label text and hint text not update new date I was choose ?? Sorry for my poor English. Please help me !!!
CodePudding user response:
Before you provide the full code, I can only say that a TextField
will change value only when it is built in first time and through the controller, here TextFieldWidget.text
is changed when controller.dateRange
changed from Obx
, but that won't re-initialize your TextEditingController
so the value in the TextField
s won't change.
To solve this, you need to listen for changes and call control.text = newText
in didUpdateWidget
.
TextFieldWidget
@override
void didUpdateWidget(Widget oldWidget) {
if (oldWidget.text != widget.text) {
control.text = widget.text;
}
}
...