Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can not update widget when data changed using Obx

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 !!!

>Solution :

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 TextFields 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;
  }
}
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading