How To Set Flutter Textformfield Default Value?

flutter textformfield default value

Flutter textformfield default value. In this article, I will be discussing and implementing Flutter textformfield default value. I will be discussing the role, usage and importance of Flutter textformfield default value and how can you set a custom value according to your Flutter app requirements. So let’s not waste anymore of your precious time and start implementing custom Flutter textformfield default value.

Why is Flutter Textformfield Default Value Used?

Flutter textformfield default value is used when you want to automatically show a value in the Flutter textformfield, let’s say we have a description and we want to edit this description using the Flutter textformfield, so for that we have to use the Flutter textformfield default value to store the already present text of the description and show it in the Flutter textformfield so user don’t have to write the already present description again, in this way, the user can add to this description or delete it and create a new description.

So this is how Flutter textformfield default value works. Don’t worry I will explain Flutter textformfield default value using a proper example for you to better understand the importance of it.

Custom Flutter Textformfield Default Value

Let’s now give our Flutter textformfield default value default value a custom value to test how it works. For that, you have to use the initial value constructor of the Flutter textformfield. This initial value constructor takes a string only. Let’s implement it using code:

  TextFormField(
           initialValue: 'This is the initial/default value',
                  )
flutter textformfield default value
As you can see in the above code, I have used the initial constructor of the Flutter textformfield and have passed a string defined above to it. In the image above, you can see that we now have a pre defined string in our Flutter textformfield which satisfies the purpose of Flutter textformfield default value. So this is how you can give your Flutter textformfield a default value.
Congrats for making it this far. Hope you don’t have any queries left regarding the customization of Flutter textformfield default value. If you still confused then comment down your queries. Now as a treat, I have made a custom Flutter textformfield design for you to use in which Flutter textformfield default value default value is also implemented. Hope you will love it. The complete source code of this custom Flutter textformfield is given in the next section.

Custom Flutter Textformfield Source Code

flutter textformfield initial value

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> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      body: Center(
          child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 40),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  TextFormField(
                    initialValue: 'This is the initial value',
                    style: TextStyle(color: Colors.green),
                    cursorColor: Colors.green,
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        filled: true,
                        fillColor: Colors.green.withOpacity(.08),
                        prefixIcon: Icon(
                          Icons.person,
                          color: Colors.green,
                        ),
                        suffixIcon: Icon(
                          Icons.visibility,
                          color: Colors.green,
                        ),
                        enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                                color: Colors.green.withOpacity(.05)),
                            borderRadius: BorderRadius.circular(30).copyWith(
                                bottomRight: Radius.circular(0),
                                bottomLeft: Radius.circular(0))),
                        focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                                color: Colors.green.withOpacity(.05)),
                            borderRadius: BorderRadius.circular(30).copyWith(
                                bottomRight: Radius.circular(0),
                                bottomLeft: Radius.circular(0)))),
                  )
                ],
              ))),
    ));
  }
}

Conclusion

In conclusion, now you are familiar with the customization of the Flutter textformfield default value. You can comment down your feedback. I would love to read it. You can also check out my other articles on Flutter widgets, animations, templates and many more. Thanks for reading this post.

Leave a Comment

Your email address will not be published.