General method
All those who have done the front-end work should have submitted the form. Take text input as an example, the general TextField component is provided in flutter
TextField( decoration: InputDecoration(labelText: 'Product name'), onChanged: (String value) { setState(() { _name = value; }); print(value); }, );
onChanged callback a method containing the String value parameter, which assigns a value to the property through the setState method
TextFormField
//Form state GlobalKey<FormState> _formKey = GlobalKey<FormState>(); @override Widget build(BuildContext context) { // TODO: implement build return Container( margin: EdgeInsets.all(10.0), child: Form( //Binding state properties key: _formKey, child: ListView( children: <Widget>[ _buildNameText(), _buildDescriptionText(), _buildPriceText(), Container( padding: EdgeInsets.all(10.0), child: RaisedButton(child: Text('Add to'), onPressed: _submitValues), ) ], ))); } Widget _buildNameText() { return TextFormField( decoration: InputDecoration(labelText: 'Product name'), validator: (String value) { //Remove leading and trailing spaces if (value.isEmpty || value.trim().length <= 5) { return 'The number of name words must be greater than 5'; } }, onSaved: (String value) { text = value; }, ); }
flutter provides a set of solutions for form submission. It is mainly composed of Form,TextFormField, component, globalkey < formstate > attribute. onSaved callback a method with String value as parameter. The calling time is different from onChanged, and it will be called when ﹐ formKey.currentState.save() is executed. The validator in TextFormField is a verifier, and it also calls back a method, which can be customized The condition returns the error message. Its function will be called in ﹐ formKey.currentState.validate(); method call is a callback