我正在尝试的是在没有TextEditingController的情况下复制“ setText”。由于使用的是FutureBuilder,因此无法手动更新状态,因此无法选择TextEditingController。
下面是更好理解的代码。
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,
);
}
}
所以在这里,当我使用setVariableData(variable,data)进行更新时,处于状态的变量正在更新,而TextFields中没有反映同样的状态。
答案 0 :(得分:0)
首先,您的问题很令人困惑。但是,我想,您正在寻找一种在提交数据时将值设置为某些xyz变量的方法,对吗?
您可以通过两种方式执行此操作,这些方式是:
- 使用onSubmitted Property:当用户在文本字段中完成值的编辑后,将给出值
- 使用TextEditingController:控制要编辑的文本。
使用TextEditingController的代码:
TextEditingController _controller = new TextEditingController();
TextField(
controller: _controller
)
//suppose you have a button which will print the data of the textfield for on onPressed
RaisedButton(
onPressed: () {
//this will print the value you've entered into the textfield
print(this._controller.text)
//change the value using setState here
setState((){
value_to_be_changed_variable = this._controller.text
})
},
child: const Text('Submit', style: TextStyle(fontSize: 20))
)
使用onSubmitted属性的代码:
TextField(
onSubmitted: (value) {
//do the operation here with the help of setState()
setState((){
variable_value_to_be_changed = value
})
}
)
我希望这足以满足您的需求。有关更多信息,请彻底了解Textfield Flutter Class。它将在很多方面为您提供帮助