TextFormField Error message on validation – Flutter

I have implemented SignIn screen in which I created customTextFormField cause I need to use it in another screen also but my concern is when someone enter the value in email field it will show error in password field even if nothing has been entered yet. Here’s a image:
enter image description here

SignIn:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class SignIn extends StatefulWidget {
const SignIn({super.key});
@override
State<SignIn> createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
final _sformkey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Sign In',
style: TextStyle(fontSize: 22,fontWeight: FontWeight.w600),
),
SizedBox(height: 10),
Form(
key: _sformkey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
children: [
//Email field
CustomTextFormField(
controller: _emailController,
hintText: 'Email',
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter email address";
} else if (!RegExp(
r'^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$')
.hasMatch(value)) {
return 'Please enter a valid email address';
}
return null;
},
),
SizedBox(height: 4),
//Password field
CustomTextFormField(
controller: _passwordController,
hintText: 'Password',
keyboardType: TextInputType.visiblePassword,
validator: (value) {
if (value == null || value.isEmpty) {
return 'The email or password you have entered is incorrect.';
}
return null;
},
),
SizedBox(height: 20),
//Sign In Button
GestureDetector(
onTap: () {
if (_sformkey.currentState!.validate()) {
Navigator.pushReplacementNamed(context, '/nextpage');
}
},
child: Container(
//SignIn Button
),
),
],
),
),
],
),
),
),
);
}
}
</code>
<code>class SignIn extends StatefulWidget { const SignIn({super.key}); @override State<SignIn> createState() => _SignInState(); } class _SignInState extends State<SignIn> { TextEditingController _emailController = TextEditingController(); TextEditingController _passwordController = TextEditingController(); final _sformkey = GlobalKey<FormState>(); @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 'Sign In', style: TextStyle(fontSize: 22,fontWeight: FontWeight.w600), ), SizedBox(height: 10), Form( key: _sformkey, autovalidateMode: AutovalidateMode.onUserInteraction, child: Column( children: [ //Email field CustomTextFormField( controller: _emailController, hintText: 'Email', keyboardType: TextInputType.emailAddress, validator: (value) { if (value == null || value.isEmpty) { return "Please enter email address"; } else if (!RegExp( r'^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$') .hasMatch(value)) { return 'Please enter a valid email address'; } return null; }, ), SizedBox(height: 4), //Password field CustomTextFormField( controller: _passwordController, hintText: 'Password', keyboardType: TextInputType.visiblePassword, validator: (value) { if (value == null || value.isEmpty) { return 'The email or password you have entered is incorrect.'; } return null; }, ), SizedBox(height: 20), //Sign In Button GestureDetector( onTap: () { if (_sformkey.currentState!.validate()) { Navigator.pushReplacementNamed(context, '/nextpage'); } }, child: Container( //SignIn Button ), ), ], ), ), ], ), ), ), ); } } </code>
class SignIn extends StatefulWidget {
  const SignIn({super.key});

  @override
  State<SignIn> createState() => _SignInState();
}

class _SignInState extends State<SignIn> {
  TextEditingController _emailController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();

  final _sformkey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'Sign In',
                style: TextStyle(fontSize: 22,fontWeight: FontWeight.w600),
              ),
              SizedBox(height: 10),
              Form(
                key: _sformkey,
                autovalidateMode: AutovalidateMode.onUserInteraction,
                child: Column(
                  children: [
                    //Email field
                    CustomTextFormField(
                      controller: _emailController,
                      hintText: 'Email',
                      keyboardType: TextInputType.emailAddress,
                      validator: (value) {
                        if (value == null || value.isEmpty) {
                          return "Please enter email address";
                        } else if (!RegExp(
                            r'^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$')
                            .hasMatch(value)) {
                          return 'Please enter a valid email address';
                        }
                        return null;
                      },
                    ),
                    SizedBox(height: 4),
                    //Password field
                    CustomTextFormField(
                      controller: _passwordController,
                      hintText: 'Password',
                      keyboardType: TextInputType.visiblePassword,
                      validator: (value) {
                        if (value == null || value.isEmpty) {
                          return 'The email or password you have entered is incorrect.';
                        }
                        return null;
                      },
                    ),
                    SizedBox(height: 20),
                    //Sign In Button
                    GestureDetector(
                      onTap: () {
                        if (_sformkey.currentState!.validate()) {
                          Navigator.pushReplacementNamed(context, '/nextpage');
                        }
                      },
                      child: Container(
                        //SignIn Button
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

CustomTextFormField:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class CustomTextFormField extends StatelessWidget {
const CustomTextFormField(
{super.key,
required this.controller,
required this.hintText,
required this.keyboardType,
required this.validator});
final TextEditingController controller;
final String hintText;
final TextInputType keyboardType;
final FormFieldValidator<String> validator;
@override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width * 0.9,
child: TextFormField(
controller: controller,
validator: validator,
decoration: InputDecoration(
hintText: hintText,
border: OutlineInputBorder(),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey.shade400, width: 1.5),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey, width: 2),
),
focusedErrorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red.shade900, width: 2),
),
errorMaxLines: 2,
),
keyboardType: keyboardType,
),
);
}
}
</code>
<code>class CustomTextFormField extends StatelessWidget { const CustomTextFormField( {super.key, required this.controller, required this.hintText, required this.keyboardType, required this.validator}); final TextEditingController controller; final String hintText; final TextInputType keyboardType; final FormFieldValidator<String> validator; @override Widget build(BuildContext context) { return Container( width: MediaQuery.of(context).size.width * 0.9, child: TextFormField( controller: controller, validator: validator, decoration: InputDecoration( hintText: hintText, border: OutlineInputBorder(), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.grey.shade400, width: 1.5), ), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.grey, width: 2), ), focusedErrorBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.red.shade900, width: 2), ), errorMaxLines: 2, ), keyboardType: keyboardType, ), ); } } </code>
class CustomTextFormField extends StatelessWidget {
  const CustomTextFormField(
      {super.key,
        required this.controller,
        required this.hintText,
        required this.keyboardType,
        required this.validator});

  final TextEditingController controller;
  final String hintText;
  final TextInputType keyboardType;
  final FormFieldValidator<String> validator;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width * 0.9,
      child: TextFormField(
        controller: controller,
        validator: validator,
        decoration: InputDecoration(
          hintText: hintText,
          border: OutlineInputBorder(),
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.grey.shade400, width: 1.5),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.grey, width: 2),
          ),
          focusedErrorBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.red.shade900, width: 2),
          ),
          errorMaxLines: 2,
        ),
        keyboardType: keyboardType,
      ),
    );
  }
}

I have also tried AutovalidateMode.onUserInteraction to prevent this but I don’t understand what silly mistake I am making. I just want that if someone enter the wrong value in email and password it will show error otherwise it will clear the error message.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật