How To Change Flutter Textformfield Error Text Weight [Easy Flutter Guide]

flutter textformfield error text weight

In this tutorial, we’ll learn how to properly customize Flutter textformfield error text weight by using practical Flutter code examples.

Introduction: Flutter Textformfield Error Text Weight

Flutter textformfield error text is the text that is shown in the bottom left side of Flutter textformfield underline/outline border.

Flutter textformfield error text weight is the thickness of error text. Let’s first see its default font weight. After that, we’ll customize it practically.

Default Flutter Textformfield Error Text Weight

For that, we’ve to use the decoration constructor of Flutter textformfield widget and pass it input decoration. Then by using the error text constructor of input decoration, we can easily set error text.

For demonstration, we’ll pass a simple string as this constructor only accepts a string. Let’s now see what the default Flutter textformfield error text weight is. See below code:

TextFormField(
         decoration: InputDecoration(
             errorText: 'Default Error Text Weight'
             )
              )
flutter textformfield error text default weight
The above image shows the default weight of error text. Let’s now customize its weight practically.

Change Flutter Textformfield Error Text Weight

In order to do that, we’ve to use the error style constructor of input decoration widget. It accepts a text style widget so we’ll pass it a Flutter text style widget. This text style has a fontWeight constructor. It takes a FontWeight so for demonstration, we’ll pass it FontWeight.bold. See below code:

decoration: InputDecoration(          
   errorText: 'Custom Error Text Weight',
   errorStyle: TextStyle(fontWeight: FontWeight.bold)
)

flutter textformfield error text font weight bold

We can see that the weight of error text is now set to bold. We can also set the text thickness using the below list.

FontWeight.bold
FontWeight.w100
FontWeight.w200
FontWeight.w300
FontWeight.w400
FontWeight.w500
FontWeight.w600
FontWeight.w700
FontWeight.w800
FontWeight.w900

You can pass any of these font weight values to fontWeight constructor to get your required Flutter textformfield error text weight. Do give them a try.

So this is how we can easily give an error text a weight of our choice. Hope you like this post.

The complete source code of Flutter textformfield error text with custom weight is given in the next section.

Custom Flutter Textformfield Error Text Weight Source Code

flutter textformfield error text weight

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 StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      body: Center(
          child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 40),
              child: TextFormField(
                decoration: InputDecoration(
                    errorText: 'Custom Error Text Weight',
                    errorStyle: TextStyle(fontWeight: w900)),
              ))),
    ));
  }
}

Conclusion

To conclude this tutorial, now you’ve a detailed practical knowledge of how to customize Flutter textformfield error text weight. I’d love to have your feedback on this post.

I’d also love to see you visit my other tutorials on Flutter app development and Python programming as well. Thank you for reading this post.

Leave a Comment

Your email address will not be published.