Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Working with forms in Flutter: Styling form fields

Capítulo 153

Estimated reading time: 5 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Working with forms is a crucial part of application development. In Flutter, there are several ways to style form fields to improve the user experience and make your app more attractive. In this chapter, we'll see how to style form fields using Flutter and Dart in a complete course.

We'll start with the basics: creating a form field. In Flutter, you can create a form field using the TextFormField widget. This widget allows you to create a form field with many customization options. For example, you can set initial text, text hint, keyboard type, validation and much more.

To style a form field, you can use the 'decoration' property. This property accepts an InputDecoration object that allows you to style the form field in various ways. For example, you can set background color, border color, border shape, text color, text font, icon, text hint and much more.

TextFormField(
  decoration: InputDecoration(
    fillColor: Colors.blue,
    filled: true,
    border: OutlineInputBorder(),
    labelText: 'Name',
    hintText: 'Type your name',
  ),
)

In the code sample above, the form field has a blue background color, an outer border, a 'Name' text label, and a 'Type your name' text hint.

Also, you can style the text inside the form field using the 'style' property. This property accepts a TextStyle object that allows you to style the text in a variety of ways. For example, you can set text color, text font, text size, text weight, line height, text decoration and more.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

TextFormField(
  style: TextStyle(
    color: Colors.white,
    fontSize: 20,
    fontWeight: FontWeight.bold,
  ),
  decoration: InputDecoration(
    fillColor: Colors.blue,
    filled: true,
    border: OutlineInputBorder(),
    labelText: 'Name',
    hintText: 'Type your name',
  ),
)

In the code sample above, the text inside the form field has a white color, a size of 20 points, and a bold weight.

Additionally, you can style the text hint using the 'hintStyle' property. This property accepts a TextStyle object that allows you to style the text hint in a variety of ways. For example, you can set the text tip color, text tip font, text tip size, text tip weight, text tip line height, text tip decoration, and much more.

TextFormField(
  style: TextStyle(
    color: Colors.white,
    fontSize: 20,
    fontWeight: FontWeight.bold,
  ),
  decoration: InputDecoration(
    fillColor: Colors.blue,
    filled: true,
    border: OutlineInputBorder(),
    labelText: 'Name',
    hintText: 'Type your name',
    hintStyle: TextStyle(
      color: Colors.white,
      fontSize: 16,
      fontStyle: FontStyle.italic,
    ),
  ),
)

In the code sample above, the text hint inside the form field has a white color, a size of 16 points, and an italic font.

Finally, you can style the text label using the 'labelStyle' property. This property accepts a TextStyle object that allows you to style the text label in a variety of ways. For example, you can set the text label color, text label font, text label size, text label weight, text label line height, text label decoration, and much more.

TextFormField(
  style: TextStyle(
    color: Colors.white,
    fontSize: 20,
    fontWeight: FontWeight.bold,
  ),
  decoration: InputDecoration(
    fillColor: Colors.blue,
    filled: true,
    border: OutlineInputBorder(),
    labelText: 'Name',
    labelStyle: TextStyle(
      color: Colors.white,
      fontSize: 18,
      fontWeight: FontWeight.bold,
    ),
    hintText: 'Type your name',
    hintStyle: TextStyle(
      color: Colors.white,
      fontSize: 16,
      fontStyle: FontStyle.italic,
    ),
  ),
)

In the code sample above, the text label inside the form field has a white color, a size of 18 points, and a bold weight.

In summary, styling form fields in Flutter is an easy and flexible task. You can style almost every aspect of a form field using the 'decoration', 'style', 'hintStyle' and 'labelStyle' properties. With these properties, you can create attractive and custom form fields to improve the user experience and make your application more attractive.

Now answer the exercise about the content:

Which of the following statements is true about styling form fields in Flutter?

You are right! Congratulations, now go to the next page

You missed! Try again.

The true statement is You can style almost every aspect of a form field using the 'decoration', 'style', 'hintStyle' and 'labelStyle' properties. In Flutter, these properties are used to customize the appearance of form fields extensively, as mentioned in the provided explanations.

Next chapter

Working with forms in Flutter: Adding submit and cancel buttons

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.