How To Customize Flutter Textfield Label?

flutter textfield label

Flutter textfield label customization. In this article, I will be implementing Flutter textfield label in Flutter textfield. I will be implementing Flutter textfield label with a proper Flutter code example and a step by step explanation. Let’s get right into implementing Flutter textfield label in our Flutter textfield.

What is Flutter Textfield Label?

Flutter textfield label can be used to describe the input Flutter textfield. When the Flutter textfield is unfocused or empty then the Flutter textfield label is shown where the input from user will be taken, means at the same position where the entered input will be shown but when the Flutter textfield is focused then it animates to the top left position of the Flutter textfield.

Flutter textfield label takes a widget means we can pass multiple Flutter text widgets to it or other Flutter widgets like Flutter containers etc. Flutter textfield label can be used to give more decoration to the Flutter textfield. Let’s now implement it using a proper Flutter code to have a better understanding of how Flutter textfield label works.

Implementing Flutter Textfield Label

Now to implement Flutter textfield label, we have to use the label constructor of the input decoration class. Let’s see the below code for this:

label: Row(
         children: [
                 Icon(
                      Icons.phone,
                      color: Colors.grey,
                     ),
                 SizedBox(
                     width: 5,
                         ),
                 Text(
                      'Enter phone number',
                     ),
                        ],
                      ),
flutter textfield label
flutter textfield label
You can see in the above code that I have used the Flutter textfield label constructor and have passed it a Flutter row widget, in its children I have passed a Flutter icon widget and a Flutter text widget, Flutter sized box widget is used to give some width space between the icon and text widget.
Now in the first, you can see that the Flutter textfield is unfocused and Flutter textfield label is showing like this.
In this second image, the Flutter textfield is focused and as you can see that I haven’t defined any text style to the Flutter text widget so it automatically turns the color of text to blue but if you define a text style and specify some color in it then the color of the Flutter text widget will remain the same for both focused and unfocused.
Using icon in Flutter textfield label is not a good practice as it has a padding of its own, you can use a container or other Flutter widgets depending on your Flutter app requirements but if you still want to use Flutter icon then you can make the Flutter textfield height bigger by using the content padding constructor.
flutter textfield label
In the above image, I have used a simple Flutter text widget in the Flutter textfield label.
Hope you now have a complete idea of how you can customize Flutter textfield label depending on your requirements. If you have any queries then you can comment it down.

Beautiful Flutter Textfield Source Code

flutter textfield label

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: [
                  TextField(
                    style: TextStyle(color: Colors.orange),
                    cursorColor: Colors.orange,
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        filled: true,
                        fillColor: Colors.orange.withOpacity(.08),
                        contentPadding: EdgeInsets.all(26),
                        label: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Icon(
                              Icons.home,
                              color: Colors.orange,
                            ),
                            SizedBox(
                              width: 5,
                            ),
                            Text(
                              'Enter your address',
                              style: TextStyle(
                                  color: Colors.orange.withOpacity(.6)),
                            ),
                          ],
                        ),
                        enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.orange),
                            borderRadius: BorderRadius.circular(30).copyWith(
                                topRight: Radius.circular(0),
                                topLeft: Radius.circular(0))),
                        focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.orange),
                            borderRadius: BorderRadius.circular(30).copyWith(
                                topRight: Radius.circular(0),
                                topLeft: Radius.circular(0)))),
                  )
                ],
              ))),
    ));
  }
}

Conclusion

In conclusion, you now have a complete understanding of how to use Flutter textfield label in Flutter textfield. I would be looking forward for you feedback on this post. Thanks for reading.

Leave a Comment

Your email address will not be published.