If you want to clear error message in TextFormField in Flutter? Use the following example code to clear error message in TextFormField in Flutter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | final _textKey = GlobalKey<FormState>(); final TextEditingController _controller = TextEditingController(); Widget _getPhoneInputForm() { final RegExp _phoneRegex = RegExp(r"^\+{1}\d{10,17}"); bool isError = false; bool isButtonPressed = false; return Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Padding( padding: EdgeInsets.symmetric(horizontal: 36.0), child: Form( key: _textKey, child: TextFormField( keyboardType: TextInputType.phone, decoration: InputDecoration( hintText: hint_enter_phone, contentPadding: EdgeInsets.all(24.0), fillColor: Colors.blueGrey.withOpacity(0.3), filled: true, border: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(16.0)), borderSide: BorderSide(color: Colors.blueGrey))), controller: _controller, validator: (str) { if (!isButtonPressed) { return null; } isError = true; if (str.isEmpty) { return err_empty_field; } else if (!_phoneRegex.hasMatch(str)) { return err_invalid_phone; } isError = false; }, onFieldSubmitted: (str) { if (_textKey.currentState.validate()) _phoneLogin(); }, ), onChanged: () { isButtonPressed = false; if (isError) { _textKey.currentState.validate(); } }, ), ), RaisedButton( color: Colors.teal, textColor: Colors.white, onPressed: () { isButtonPressed = true; if (_textKey.currentState.validate()) _phoneLogin(); }, child: Text(login), ) ], ); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.