onSaved is not saving my values in the main class from where i am calling this class
i want to generate custom form as per api return data and bind the data in form and then it should validate as called on validate in flutter
i have created custom textform field for generating form field
from main class i am using listview with for loop to generate a list and then my form is going to validate
class CustomTextField extends StatefulWidget {
CustomTextField(
{@required this.focusNode,
@required this.nextFocusNode,
@required this.textEditingController,
@required this.validator,
@required this.labelText,
@required this.dataText});
final FocusNode focusNode;
final FocusNode nextFocusNode;
final TextEditingController textEditingController;
final FormFieldValidator<String> validator;
final String labelText;
String dataText;
@override
_CustomTextFieldState createState() => new _CustomTextFieldState();
}
class _CustomTextFieldState extends State<CustomTextField> {
@override
Widget build(BuildContext context) {
return Container(
height: 65,
child: new TextFormField(
style: Utility.textFormFieldStyle(context),
keyboardType: TextInputType.text,
textInputAction: widget.nextFocusNode == null
? TextInputAction.done
: TextInputAction.next,
focusNode: widget.focusNode,
onFieldSubmitted: (v) {
FocusScope.of(context).requestFocus(widget.nextFocusNode);
},
decoration: InputDecoration(
labelText: widget.labelText,
contentPadding: Utility.edgeInsetsGeometry()),
controller: widget.textEditingController,
validator: widget.validator,
onSaved: (String val) {
widget.dataText = val;
// not saving my value in my main class
print("costom text view ${widget.dataText}");
}));
}
}
/// main class ----- belo code is run from a stateful class ----------------------///
List<FocusNode> listFocusNode;
List<String> listDataText;
List<TextEditingController> listTextEditingController;
List<Widget> listFormField;
@override
void initState() {
super.initState();
listFocusNode = <FocusNode>[];
listTextEditingController = <TextEditingController>[];
listFormField = <Widget>[];
listDataText = <String>[];
for (int i = 0; i < 5; i++) {
listFocusNode.add(FocusNode());
}
for (int i = 0; i < 5; i++) {
listDataText.add("old");
}
for (int i = 0; i < 5; i++) {
listTextEditingController.add(TextEditingController());
}
for (int i = 0; i < 5; i++) {
listFormField.add(CustomTextField(
dataText: listDataText[i],
focusNode: listFocusNode[i],
labelText: "field$i",
textEditingController: listTextEditingController[i],
nextFocusNode: i == 4 ? null : listFocusNode[i + 1],
// validator: validateNull,
validator: validateName,
));
}
}
_validateForm() {
if (_key.currentState.validate()) {
// No any error in validation
_key.currentState.save();
print("Name ${listDataText.toString()}");
// when i print this data in log its printing old data only but onSaved should save value to the proper location in array list
Navigator.of(context).pop();
} else {
setState(() {
_validate = true;
});
}
}
final nameController = TextEditingController(); final descriptionController = TextEditingController(); setState(() { nameController. text = widget.name; descriptionController. text = widget. description; });
To add a border to a TextField/TextFormField in Flutter, you can specify the decoration property and then use the InputDecoration and OutlineInputBorder widget. The OutlineInputBorder widget has the borderSide widget which you can use to pass in the BorderSide widget with width and color parameter to create the border.
Flutter provides a Form widget to create a form. The form widget acts as a container, which allows us to group and validate the multiple form fields. When you create a form, it is necessary to provide the GlobalKey. This key uniquely identifies the form and allows you to do any validation in the form fields.
onSaved() function won't be called automatically after successful validation. We have to call _formKey.currentState.save() manually to save our variables. Good luck
Form(
key: key,
child: TextFormField(
onSaved: (val) {
print('saved');
},
validator: (val) {
print('validating');
},
),
),
RaisedButton(
child: Text('Click me'),
onPressed: () {
if (key.currentState.validate()) {
key.currentState.save();
print('valid');
}
},
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With