I am trying to clear TextFormField data after validation and clear the validation errors. I am using formkey.currentState.reset to clear the errors. If I use formkey.currentState.reset errors are being cleared but the values in fields don't clear,and if I dont use formkey.currentState.reset fields are being cleared but errors remain unclear. How can i fix it Here is my form with text fields
homeView.dart
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
if (formKey.currentState!.validate()) {
print("validated");
controller.resetAll();
formKey.currentState!.reset();
}
},
child: Icon(Icons.add),
),
body: SafeArea(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Form(
key: formKey,
child: Column(
children: [
Wrap(
spacing: 5,
children: [
CustomTextField(
textEditingController: controller.usernameController,
onChanged: (value) {
// Handle onChanged event for username
},
hintText: 'Enter your username',
isEnabled: true,
keyboardType: TextInputType.text,
validateField: (value) {
if (value!.isEmpty) {
return "required";
} else {
return null;
}
},
),
SizedBox(height: 16),
CustomTextField(
textEditingController: controller.emailController,
onChanged: (value) {
// Handle onChanged event for email
},
hintText: 'Enter your email',
isEnabled: true,
keyboardType: TextInputType.emailAddress,
validateField: (value) {
if (value!.isEmpty) {
return "required";
} else {
return null;
}
},
),
Following is homeController
homeController.dart
class HomeController extends GetxController {
var name="".obs;
late TextEditingController usernameController ;
late TextEditingController emailController ;
late TextEditingController passwordController;
late TextEditingController ageController;
late TextEditingController phoneNumberController ;
GlobalKey<FormState> formKey = GlobalKey();
// getting data from api
@override
void onInit() {
name.value="";
usernameController =TextEditingController();
emailController =TextEditingController();
passwordController=TextEditingController();
ageController=TextEditingController();
phoneNumberController =TextEditingController();
formKey = GlobalKey<FormState>();
super.onInit();
}
resetAll(){
usernameController.clear() ;
emailController.clear() ;
passwordController.clear();
ageController.clear();
phoneNumberController.clear();
}
}
Following is customTextField
customTextField.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import '../../config/theme/my_styles.dart';
import '../../config/translations/strings_enum.dart';
class CustomTextField extends StatelessWidget {
CustomTextField(
{Key? key,
required this.textEditingController,
this.onChanged,
this.hintText,
required this.isEnabled,
this.keyboardType,
this.inputFormatters,
this.validateField
})
: super(key: key);
final TextEditingController textEditingController;
final void Function(String)? onChanged;
final String? hintText;
final TextInputType? keyboardType;
final String? Function(String?)? validateField;
bool isEnabled = true;
List<TextInputFormatter>? inputFormatters;
@override
Widget build(BuildContext context) {
return Container(
width: 150,
child: TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: validateField,
controller: textEditingController,
style: TextStyle(
fontSize: 12,
),
keyboardType: keyboardType,
inputFormatters: inputFormatters,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
width: 1.2,
color: Colors.black26,
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
width: 1.2,
color: Colors.black26,
)),
isDense: true,
isCollapsed: true,
contentPadding: EdgeInsets.symmetric(vertical: 5.1)),onChanged:onChanged ,),
);
}
}