What i am trying is to replicate "setText" in flutter without TextEditingController. Since I'm using FutureBuilder, i cannot go with manually updating state, thereby removing the choice of TextEditingController.
Bellow is the code for better understanding.
class DynamicFormBuilder {
GlobalKey<FormBuilderState> dynamicFormKey = GlobalKey<FormBuilderState>();
String getVariableData(String variable) {
return dynamicFormKey.currentState.fields[variable].currentState.value;
}
void setVariableData(String variable, String value) {
dynamicFormKey.currentState.fields[variable].currentState.didChange(value);
}
Widget workOnWidget(DynamicFormResponse widget) {
switch (widget.widget) {
case "text":
return Text(
widget.displayText,
style: TextStyle(
fontWeight: widget.style.fontWeight == "bold"
? FontWeight.bold
: FontWeight.normal,
fontSize: double.parse(widget.style.fontSize)),
);
case "editable":
return FormBuilderTextField(
attribute: widget.variable,
decoration: InputDecoration(labelText: widget.displayText),
onChanged: (val) {
if (widget.onChanged != null) {
onChangedFunctionMapper[widget.variable](val);
}
},
);
case "datepicker":
return FormBuilderDateTimePicker(
attribute: widget.variable,
inputType: InputType.date,
format: DateFormat("dd/MM/yyyy"),
decoration: InputDecoration(labelText: widget.displayText),
validators: [
FormBuilderValidators.required(),
],
);
case "divider":
return Container(
margin: EdgeInsets.fromLTRB(5, 10, 5, 10),
width: double.infinity,
height: 10,
color: accentColor,
);
case "dropdown":
return FormBuilderDropdown(
attribute: widget.variable,
items: convertDropdown(widget.options),
hint: Text(widget.hint),
decoration: InputDecoration(labelText: widget.displayText),
);
case "radioButton":
return FormBuilderRadioButton(
displayText: widget.displayText,
attribute: widget.variable,
isHorizontal: widget.isHorizontal,
onChanged: (val) {
if (val == "yes") {
visibilityMap["age"] = true;
} else {
visibilityMap["age"] = false;
}
},
options: widget.options
.map((lang) => FormBuilderFieldOption(value: lang))
.toList(growable: false),
);
default:
return Text("lol");
}
}
Widget buildForms(BuildContext context, Future<dynamic> fetchJSON) {
jsonData = new List();
dynamicFormKey = GlobalKey<FormBuilderState>();
accentColor = Theme.of(context).accentColor;
return FutureBuilder(
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.none &&
snapshot.hasData == null) {
return Container(
child: Text("No Data got loaded"),
);
} else if (snapshot.connectionState == ConnectionState.done) {
for (int i = 0; i < snapshot.data.length; i++) {
jsonData.add(DynamicFormResponse.fromJson(snapshot.data[i]));
}
return ListView(
padding: EdgeInsets.all(10),
children: <Widget>[
FormBuilder(
key: dynamicFormKey,
autovalidate: false,
child: Card(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: jsonData.map<Widget>((widget) {
print(widget.widget);
return workOnWidget(widget);
}).toList(),
),
),
),
),
],
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
future: fetchJSON,
);
}
}
So here when i do updates using setVariableData(variable, data), the variable in state is getting updated, but the same is not getting reflected in TextFields.