How To Change Flutter Textformfield Error Style

flutter textformfield error style

Flutter textformfield error style customization. In this post, I will be discussing and implementing the customization of Flutter textformfield error style. Everything about Flutter textformfield error style will be discussed like what it is, its usage in Flutter textformfield and how can you customize it. So let’s not make you wait anymore and jump directly into the customization Flutter textformfield error style.

What is Flutter Textformfield Error Style?

Flutter textformfield error style, as the name suggests, it is the style of the error text in Flutter textformfield. If you want to have a detailed understanding of what Flutter textformfield error text is then click here. So in this post, we will see how we can change the style of Flutter textformfield.

Default Flutter Textformfield Error Style

To see the default error style, we have to define a simple Flutter textformfield and pass input decoration class in its decoration constructor. Using the error text constructor of the Flutter textformfield, we can see the default Flutter textformfield error style. We have implemented it logically with the condition that when the user enters a blank space the it will show an error. See the below code:

String errorTextvalue = '';
TextFormField(
        onChanged: (value) {
              setState(() {
                    if (value.contains(' ')) {
                      errorTextvalue = value;
                    } 
                    else {
                      errorTextvalue = '';
                    }
                  });
                },
                decoration: InputDecoration(
                  errorText:
                      errorTextvalue.isEmpty ? null : 'Don\'t use blank spaces',
                ),
              )
flutter textformfield default error style
As you can in the above image, the default error style shows red color. Let’s now see how we can change the default Flutter textformfield error style.

Change Flutter Textformfield Error Style

To change Flutter textformfield error style, we have to use the error style constructor of the input decoration class. The error style constructor text style class which means we can change the error style’s color, font size and many more. For demonstration, I have customized color and font size of error text. See the below code:

errorStyle: TextStyle(color: Colors.green, fontSize: 16))
flutter textformfield custom error style
In the above image, you can see that the default Flutter textformfield error style is now customized.
So in this way, you can change the Flutter textformfield error style. You can try out customizing other values of text style class like font weight, decoration etc.
Now you have a complete understanding of how to customize Flutter textformfield error style. If you still face any error regarding the customization of Flutter textformfield error style the do let me know in the comments. I would love to answer them.
As a treat, I have prepared a custom Flutter textformfield design for you in which Flutter textformfield error style is also customized. Hope you will love it. The complete source code is provided in the next section.

Custom Flutter Textformfield Design Source Code

flutter textformfield error style

import 'package:flutter/material.dart';
void main() {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Homepage(),
    );
  }
}
class Homepage extends StatefulWidget {
  @override
  State<Homepage> createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
  String errorTextvalue = '';
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      body: Center(
          child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 40),
              child: TextFormField(
                onChanged: (value) {
                  setState(() {
                    if (value.contains(' ')) {
                      errorTextvalue = value;
                    } else {
                      errorTextvalue = '';
                    }
                  });
                },
                decoration: InputDecoration(
                    border: InputBorder.none,
                    enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(50),
                        borderSide: BorderSide(color: Colors.purple)),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(50),
                        borderSide: BorderSide(color: Colors.purple)),
                    errorBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(50),
                        borderSide: BorderSide(color: Colors.red)),
                    focusedErrorBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(50),
                        borderSide: BorderSide(color: Colors.red)),
                    errorText: errorTextvalue.isEmpty
                        ? null
                        : 'Don\'t use blank spaces',
                    errorStyle: TextStyle(color: Colors.green, fontSize: 13)),
              ))),
    ));
  }
}

Conclusion

In conclusion, we have learned how to customize Flutter textformfield error style. I look forward to hearing from you regarding this post and invite you to check out my other articles on Flutter app development, Flutter widgets, amazing Flutter templates, Flutter animations, and many more. That’s it for this article. Thank for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *