Popular Searches
Popular Course Categories
Popular Courses

Creating text input fields

Creating text input fields

Flutter Forms & User Input

Creating Text Input Fields in Flutter

Text input fields are essential UI components in Flutter applications. They allow users to enter information such as names, email addresses, passwords, phone numbers, search queries, messages, comments, and other data. Flutter provides the TextField widget for general text input and TextFormField when the input needs to participate in a Form with validation and other form functionality. Official Flutter Text Input Documentation


1. What Is a Text Input Field?

A text input field is a user-interface component that allows users to type and edit text. In Flutter, the most common widget for creating a text input field is TextField.

Common Uses

  • Username input
  • Full name input
  • Email address input
  • Password input
  • Phone number input
  • Search fields
  • Address fields
  • Comments and feedback
  • Chat messages
  • Product or course search
  • Registration forms
  • Login forms

2. TextField in Flutter

TextField is the standard Flutter widget for accepting text input. It can be customized with labels, hints, icons, borders, keyboard types, controllers, callbacks, formatting, focus behavior, and other properties.

Basic Syntax

TextField(
  decoration: const InputDecoration(
    labelText: 'Name',
  ),
)

Flutter's documentation describes TextField as the commonly used text input widget. Its appearance can be customized using InputDecoration. :contentReference[oaicite:0]{index=0}


3. Basic Text Input Field

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Text Input Example'),
        ),
        body: const Padding(
          padding: EdgeInsets.all(20),
          child: TextField(
            decoration: InputDecoration(
              labelText: 'Enter your name',
              border: OutlineInputBorder(),
            ),
          ),
        ),
      ),
    );
  }
}

4. Understanding InputDecoration

InputDecoration is used to customize the appearance and supporting information of a text input field.

Important InputDecoration Properties

Property Purpose
labelText Displays a label describing the field.
hintText Displays an example or instruction inside the field.
helperText Displays additional information below the field.
prefixIcon Displays an icon before the input text.
suffixIcon Displays an icon after the input text.
prefixText Displays text before the entered value.
suffixText Displays text after the entered value.
border Defines the default border.
focusedBorder Defines the border when the field has focus.
enabledBorder Defines the border when the field is enabled but not focused.
filled Enables a filled background.
fillColor Controls the filled background color.

5. TextField with Label

The labelText property identifies what information the user should enter.

TextField(
  decoration: const InputDecoration(
    labelText: 'Full Name',
    border: OutlineInputBorder(),
  ),
)

6. TextField with Hint

The hintText property provides an example or instruction to the user.

TextField(
  decoration: const InputDecoration(
    hintText: 'Enter your full name',
    border: OutlineInputBorder(),
  ),
)

Label and Hint Together

TextField(
  decoration: const InputDecoration(
    labelText: 'Email',
    hintText: '[email protected]',
    border: OutlineInputBorder(),
  ),
)

7. TextField with Prefix Icon

A prefix icon is useful for visually communicating the purpose of the input field.

TextField(
  decoration: const InputDecoration(
    labelText: 'Username',
    prefixIcon: Icon(Icons.person_outline),
    border: OutlineInputBorder(),
  ),
)

Common Prefix Icons

Icons.person_outline
Icons.email_outlined
Icons.phone_outlined
Icons.lock_outline
Icons.search
Icons.location_on_outlined
Icons.calendar_today

8. TextField with Suffix Icon

A suffix icon appears on the right side of the input field.

TextField(
  decoration: const InputDecoration(
    labelText: 'Search',
    suffixIcon: Icon(Icons.search),
    border: OutlineInputBorder(),
  ),
)

9. Creating an Email Input Field

When collecting an email address, use TextInputType.emailAddress to request an email-friendly keyboard on supported platforms.

TextField(
  keyboardType: TextInputType.emailAddress,
  decoration: const InputDecoration(
    labelText: 'Email Address',
    hintText: '[email protected]',
    prefixIcon: Icon(Icons.email_outlined),
    border: OutlineInputBorder(),
  ),
)

10. Creating a Password Input Field

For passwords, use obscureText: true so that the entered characters are hidden.

TextField(
  obscureText: true,
  decoration: const InputDecoration(
    labelText: 'Password',
    hintText: 'Enter your password',
    prefixIcon: Icon(Icons.lock_outline),
    border: OutlineInputBorder(),
  ),
)

11. Password Visibility Toggle

A practical password field can include an eye icon that allows the user to show or hide the password.

class PasswordField extends StatefulWidget {
  const PasswordField({super.key});

  @override
  State createState() => _PasswordFieldState();
}

class _PasswordFieldState extends State {
  bool hidePassword = true;

  @override
  Widget build(BuildContext context) {
    return TextField(
      obscureText: hidePassword,
      decoration: InputDecoration(
        labelText: 'Password',
        prefixIcon: const Icon(Icons.lock_outline),
        suffixIcon: IconButton(
          icon: Icon(
            hidePassword
                ? Icons.visibility
                : Icons.visibility_off,
          ),
          onPressed: () {
            setState(() {
              hidePassword = !hidePassword;
            });
          },
        ),
        border: const OutlineInputBorder(),
      ),
    );
  }
}

12. Creating a Phone Number Input Field

Use TextInputType.phone when the user needs to enter a telephone number.

TextField(
  keyboardType: TextInputType.phone,
  decoration: const InputDecoration(
    labelText: 'Phone Number',
    hintText: 'Enter your phone number',
    prefixIcon: Icon(Icons.phone_outlined),
    border: OutlineInputBorder(),
  ),
)

13. Creating a Numeric Input Field

Use an appropriate numeric keyboard type when the field expects numeric input.

TextField(
  keyboardType: TextInputType.number,
  decoration: const InputDecoration(
    labelText: 'Age',
    hintText: 'Enter your age',
    prefixIcon: Icon(Icons.numbers),
    border: OutlineInputBorder(),
  ),
)

14. Creating a Search Input Field

Text input fields are commonly used for search functionality.

TextField(
  keyboardType: TextInputType.text,
  decoration: InputDecoration(
    hintText: 'Search products',
    prefixIcon: const Icon(Icons.search),
    suffixIcon: IconButton(
      icon: const Icon(Icons.clear),
      onPressed: () {},
    ),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(12),
    ),
  ),
  onChanged: (value) {
    print('Search: $value');
  },
)

15. Creating a Multiline Text Input

For descriptions, comments, messages, and addresses, a multiline text field can be created using maxLines.

TextField(
  maxLines: 5,
  decoration: const InputDecoration(
    labelText: 'Description',
    hintText: 'Enter your description',
    border: OutlineInputBorder(),
  ),
)

Unlimited Lines

TextField(
  maxLines: null,
  decoration: const InputDecoration(
    labelText: 'Comments',
    border: OutlineInputBorder(),
  ),
)

16. Minimum and Maximum Lines

You can control the minimum and maximum visible lines using minLines and maxLines.

TextField(
  minLines: 3,
  maxLines: 6,
  decoration: const InputDecoration(
    labelText: 'Message',
    border: OutlineInputBorder(),
  ),
)

17. Limiting Text Length

The maxLength property can be used when an input should have a maximum number of characters.

TextField(
  maxLength: 50,
  decoration: const InputDecoration(
    labelText: 'Username',
    border: OutlineInputBorder(),
  ),
)

18. Reading Input Using TextEditingController

When the application needs to retrieve the text entered by the user, use a TextEditingController. Flutter's documentation recommends disposing of the controller when it is no longer needed. Flutter Retrieve Text Input Guide

class UserInput extends StatefulWidget {
  const UserInput({super.key});

  @override
  State createState() => _UserInputState();
}

class _UserInputState extends State {
  final nameController = TextEditingController();

  @override
  void dispose() {
    nameController.dispose();
    super.dispose();
  }

  void getName() {
    print(nameController.text);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextField(
          controller: nameController,
          decoration: const InputDecoration(
            labelText: 'Name',
            border: OutlineInputBorder(),
          ),
        ),
        const SizedBox(height: 16),
        ElevatedButton(
          onPressed: getName,
          child: const Text('Get Name'),
        ),
      ],
    );
  }
}

19. Setting Initial Text

A controller can be initialized with an initial value.

final nameController = TextEditingController(
  text: 'John Doe',
);

TextField(
  controller: nameController,
)

This is useful when editing existing user information.


20. Handling Input Changes with onChanged

The onChanged callback runs whenever the text changes. It is useful for live search, filtering, character counts, and dynamic UI updates. Flutter Handle TextField Changes

TextField(
  onChanged: (value) {
    print('User entered: $value');
  },
)

Live Search Example

TextField(
  onChanged: (query) {
    searchProducts(query);
  },
)

21. Handling Submitted Input

The onSubmitted callback can be used when the user submits the current input from the keyboard.

TextField(
  onSubmitted: (value) {
    print('Submitted value: $value');
  },
)

22. TextInputAction

The textInputAction property controls the action button displayed on the software keyboard.

TextField(
  textInputAction: TextInputAction.next,
  decoration: const InputDecoration(
    labelText: 'First Name',
    border: OutlineInputBorder(),
  ),
)

Common TextInputAction Values

TextInputAction.next
TextInputAction.previous
TextInputAction.done
TextInputAction.search
TextInputAction.send
TextInputAction.go

23. Text Capitalization

The textCapitalization property controls how text is capitalized during input.

TextField(
  textCapitalization: TextCapitalization.words,
  decoration: const InputDecoration(
    labelText: 'Full Name',
    border: OutlineInputBorder(),
  ),
)

Common Options

TextCapitalization.none
TextCapitalization.characters
TextCapitalization.words
TextCapitalization.sentences

24. Creating Read-Only Input Fields

Use readOnly: true when users should not edit the field directly.

TextField(
  readOnly: true,
  decoration: const InputDecoration(
    labelText: 'User ID',
    hintText: 'USER-1001',
    border: OutlineInputBorder(),
  ),
)

25. Creating Disabled Input Fields

Set enabled: false to disable the input field.

TextField(
  enabled: false,
  decoration: const InputDecoration(
    labelText: 'Disabled Field',
    border: OutlineInputBorder(),
  ),
)

26. Autofocus

The autofocus property can request focus automatically when the field is displayed.

TextField(
  autofocus: true,
  decoration: const InputDecoration(
    labelText: 'Search',
    prefixIcon: Icon(Icons.search),
    border: OutlineInputBorder(),
  ),
)

Flutter's focus documentation demonstrates autofocus as a way to give a text field focus as soon as it becomes visible. Flutter Focus and Text Fields


27. FocusNode

A FocusNode allows developers to control which text field receives keyboard focus programmatically.

class FocusExample extends StatefulWidget {
  const FocusExample({super.key});

  @override
  State createState() => _FocusExampleState();
}

class _FocusExampleState extends State {
  final emailFocus = FocusNode();

  @override
  void dispose() {
    emailFocus.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextField(
          focusNode: emailFocus,
          decoration: const InputDecoration(
            labelText: 'Email',
            border: OutlineInputBorder(),
          ),
        ),
        ElevatedButton(
          onPressed: () {
            emailFocus.requestFocus();
          },
          child: const Text('Focus Email'),
        ),
      ],
    );
  }
}

Focus nodes are long-lived objects and should be disposed when the associated state is disposed. :contentReference[oaicite:1]{index=1}


28. Creating Multiple Text Input Fields

A form often contains several input fields arranged vertically.

Column(
  children: [
    TextField(
      decoration: const InputDecoration(
        labelText: 'Full Name',
        border: OutlineInputBorder(),
      ),
    ),
    const SizedBox(height: 16),
    TextField(
      keyboardType: TextInputType.emailAddress,
      decoration: const InputDecoration(
        labelText: 'Email',
        border: OutlineInputBorder(),
      ),
    ),
    const SizedBox(height: 16),
    TextField(
      keyboardType: TextInputType.phone,
      decoration: const InputDecoration(
        labelText: 'Phone',
        border: OutlineInputBorder(),
      ),
    ),
  ],
)

29. Moving Between Multiple Input Fields

Focus can be moved from one input field to another using FocusNode and requestFocus().

final nameFocus = FocusNode();
final emailFocus = FocusNode();

TextField(
  focusNode: nameFocus,
  textInputAction: TextInputAction.next,
  onSubmitted: (_) {
    emailFocus.requestFocus();
  },
  decoration: const InputDecoration(
    labelText: 'Name',
    border: OutlineInputBorder(),
  ),
)

TextField(
  focusNode: emailFocus,
  keyboardType: TextInputType.emailAddress,
  decoration: const InputDecoration(
    labelText: 'Email',
    border: OutlineInputBorder(),
  ),
)

30. Styling a Text Input Field

Text input fields can be styled to match the application's design.

TextField(
  style: const TextStyle(
    fontSize: 18,
    fontWeight: FontWeight.w500,
  ),
  decoration: InputDecoration(
    labelText: 'Username',
    hintText: 'Enter username',
    prefixIcon: const Icon(Icons.person_outline),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(12),
    ),
  ),
)

31. Focused Border

You can define a different border when the user is actively typing in the field.

TextField(
  decoration: InputDecoration(
    labelText: 'Email',
    border: const OutlineInputBorder(),
    focusedBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(10),
      borderSide: const BorderSide(
        width: 2,
      ),
    ),
  ),
)

32. Enabled Border

enabledBorder defines the border displayed when the field is enabled but does not currently have focus.

TextField(
  decoration: InputDecoration(
    labelText: 'Username',
    enabledBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(10),
      borderSide: const BorderSide(
        width: 1,
      ),
    ),
    focusedBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(10),
      borderSide: const BorderSide(
        width: 2,
      ),
    ),
  ),
)

33. Filled Text Input Field

A filled input field can provide a modern visual appearance.

TextField(
  decoration: InputDecoration(
    labelText: 'Username',
    filled: true,
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(12),
    ),
  ),
)

34. Prefix and Suffix Text

Prefix and suffix text can be used for values such as currency, units, or URLs.

TextField(
  decoration: const InputDecoration(
    labelText: 'Price',
    prefixText: '₹ ',
    suffixText: ' INR',
    border: OutlineInputBorder(),
  ),
)

35. Helper Text

helperText provides additional information about what the user should enter.

TextField(
  decoration: const InputDecoration(
    labelText: 'Username',
    helperText: 'Use 3 to 20 characters',
    border: OutlineInputBorder(),
  ),
)

36. Error Text

errorText can display an error message below the input field.

TextField(
  decoration: const InputDecoration(
    labelText: 'Username',
    errorText: 'Username is already taken',
    border: OutlineInputBorder(),
  ),
)

For complete form validation, TextFormField with a Form is generally more suitable. Flutter's validation recipe uses a validator that returns an error string when input is invalid and null when valid. :contentReference[oaicite:2]{index=2}


37. TextField and TextFormField

Flutter provides both TextField and TextFormField. TextField is suitable for general text input, while TextFormField integrates with a surrounding Form and supports validation and other form functionality. :contentReference[oaicite:3]{index=3}

TextField TextFormField
General text input Form-based text input
Useful for search fields Useful for registration and login forms
Does not directly use a Form validator Supports the validator property
Supports controllers Supports controllers
Supports callbacks Supports callbacks and Form integration

38. Creating a Validated Text Input Field

For a validated form, use Form and TextFormField.

final formKey = GlobalKey();

Form(
  key: formKey,
  child: Column(
    children: [
      TextFormField(
        decoration: const InputDecoration(
          labelText: 'Name',
          border: OutlineInputBorder(),
        ),
        validator: (value) {
          if (value == null || value.trim().isEmpty) {
            return 'Please enter your name';
          }
          return null;
        },
      ),
      const SizedBox(height: 16),
      FilledButton(
        onPressed: () {
          if (formKey.currentState!.validate()) {
            print('Input is valid');
          }
        },
        child: const Text('Submit'),
      ),
    ],
  ),
)

Flutter's official form-validation approach uses a GlobalKey, a TextFormField validator, and FormState.validate() when the form is submitted. :contentReference[oaicite:4]{index=4}


39. Input Formatters

Input formatters can be used to restrict or transform the text entered by the user.

import 'package:flutter/services.dart';

TextField(
  keyboardType: TextInputType.number,
  inputFormatters: [
    FilteringTextInputFormatter.digitsOnly,
  ],
  decoration: const InputDecoration(
    labelText: 'PIN',
    border: OutlineInputBorder(),
  ),
)

Limit Number of Characters

TextField(
  inputFormatters: [
    LengthLimitingTextInputFormatter(10),
  ],
)

40. Creating an Address Input Field

TextField(
  maxLines: 4,
  textCapitalization: TextCapitalization.sentences,
  decoration: const InputDecoration(
    labelText: 'Address',
    hintText: 'Enter your complete address',
    prefixIcon: Icon(Icons.location_on_outlined),
    border: OutlineInputBorder(),
  ),
)

41. Creating a Comment Input Field

TextField(
  minLines: 3,
  maxLines: 5,
  maxLength: 500,
  decoration: const InputDecoration(
    labelText: 'Comment',
    hintText: 'Write your comment',
    border: OutlineInputBorder(),
  ),
)

42. Complete User Information Form

import 'package:flutter/material.dart';

class UserInformationForm extends StatefulWidget {
  const UserInformationForm({super.key});

  @override
  State createState() =>
      _UserInformationFormState();
}

class _UserInformationFormState
    extends State {
  final nameController = TextEditingController();
  final emailController = TextEditingController();
  final phoneController = TextEditingController();
  final addressController = TextEditingController();

  @override
  void dispose() {
    nameController.dispose();
    emailController.dispose();
    phoneController.dispose();
    addressController.dispose();
    super.dispose();
  }

  void submitForm() {
    print('Name: ${nameController.text}');
    print('Email: ${emailController.text}');
    print('Phone: ${phoneController.text}');
    print('Address: ${addressController.text}');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('User Information'),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(20),
        child: Column(
          children: [
            TextField(
              controller: nameController,
              textCapitalization: TextCapitalization.words,
              decoration: const InputDecoration(
                labelText: 'Full Name',
                hintText: 'Enter your full name',
                prefixIcon: Icon(Icons.person_outline),
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 16),
            TextField(
              controller: emailController,
              keyboardType: TextInputType.emailAddress,
              decoration: const InputDecoration(
                labelText: 'Email Address',
                hintText: '[email protected]',
                prefixIcon: Icon(Icons.email_outlined),
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 16),
            TextField(
              controller: phoneController,
              keyboardType: TextInputType.phone,
              decoration: const InputDecoration(
                labelText: 'Phone Number',
                prefixIcon: Icon(Icons.phone_outlined),
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 16),
            TextField(
              controller: addressController,
              maxLines: 4,
              decoration: const InputDecoration(
                labelText: 'Address',
                hintText: 'Enter your address',
                prefixIcon: Icon(Icons.location_on_outlined),
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 24),
            SizedBox(
              width: double.infinity,
              child: FilledButton(
                onPressed: submitForm,
                child: const Text('Submit'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

43. Creating a Login Input Interface

Column(
  children: [
    TextField(
      keyboardType: TextInputType.emailAddress,
      decoration: const InputDecoration(
        labelText: 'Email Address',
        hintText: 'Enter your email',
        prefixIcon: Icon(Icons.email_outlined),
        border: OutlineInputBorder(),
      ),
    ),
    const SizedBox(height: 16),
    TextField(
      obscureText: true,
      decoration: const InputDecoration(
        labelText: 'Password',
        hintText: 'Enter your password',
        prefixIcon: Icon(Icons.lock_outline),
        suffixIcon: Icon(Icons.visibility_off),
        border: OutlineInputBorder(),
      ),
    ),
    const SizedBox(height: 20),
    SizedBox(
      width: double.infinity,
      child: FilledButton(
        onPressed: () {},
        child: const Text('Login'),
      ),
    ),
  ],
)

44. Handling Keyboard Overflow

When several text input fields are displayed vertically, the software keyboard may reduce the available screen space. A SingleChildScrollView can make the form scrollable.

Scaffold(
  body: SafeArea(
    child: SingleChildScrollView(
      padding: const EdgeInsets.all(20),
      child: Column(
        children: [
          TextField(
            decoration: const InputDecoration(
              labelText: 'Name',
              border: OutlineInputBorder(),
            ),
          ),
          const SizedBox(height: 16),
          TextField(
            decoration: const InputDecoration(
              labelText: 'Email',
              border: OutlineInputBorder(),
            ),
          ),
        ],
      ),
    ),
  ),
)

45. Practical Text Input Flow

User opens screen
      ↓
Text input field is displayed
      ↓
User taps field
      ↓
Keyboard appears
      ↓
User enters text
      ↓
onChanged() receives changes
      ↓
Controller stores current value
      ↓
User presses Next/Done/Submit
      ↓
Application processes the input

46. Best Practices for Creating Text Input Fields

  • Use clear and descriptive labels.
  • Use helpful hints when additional guidance is needed.
  • Select the correct keyboard type for the expected input.
  • Use obscureText for password fields.
  • Use TextEditingController when the application needs to read or control the value.
  • Dispose controllers when they are no longer needed.
  • Use TextFormField when input validation is part of a Form.
  • Use input formatters when appropriate.
  • Use focus management to create a smooth multi-field experience.
  • Make forms scrollable when they contain many fields.
  • Provide meaningful validation and error messages.
  • Avoid collecting unnecessary user information.
  • Keep the visual design consistent across the application.

47. Common Mistakes

  • Using a normal TextField for password input without hiding the text.
  • Forgetting to dispose TextEditingController.
  • Using an inappropriate keyboard type.
  • Creating text fields with unclear labels.
  • Using very large fixed widths.
  • Ignoring keyboard-related overflow.
  • Not providing validation for required form fields.
  • Creating separate controllers unnecessarily for fields that do not need programmatic access.
  • Ignoring focus management in long forms.
  • Depending only on client-side validation for security.

48. Text Input Fields in a Practical Application

Text input fields are used throughout real-world Flutter applications.

Application Typical Text Fields
Login App Email, Password
Registration App Name, Email, Phone, Password
E-commerce App Search, Coupon, Address
Chat App Message
Education App Search, Student Name, Course
Banking App Account Number, PIN, Search
Profile App Name, Email, Phone, Address
Feedback App Name, Email, Comments

49. Mini Project: Student Registration Form

Create a Flutter screen containing the following text input fields:

  • Student Name
  • Email Address
  • Phone Number
  • Age
  • Course Name
  • Address
  • Comments

Expected Layout

Student Registration

[ Full Name                  ]

[ Email Address              ]

[ Phone Number               ]

[ Age                        ]

[ Course Name                ]

[ Address                    ]
[                            ]
[                            ]

[ Comments                   ]
[                            ]
[                            ]

[       REGISTER             ]

Learning Objectives

  • Create multiple TextFields.
  • Use different keyboard types.
  • Use TextEditingController.
  • Handle multiline input.
  • Read user-entered values.
  • Manage input focus.
  • Create a responsive form layout.
  • Prepare the form for validation using TextFormField.

50. TextField vs TextFormField Quick Comparison

Feature TextField TextFormField
Text input Yes Yes
InputDecoration Yes Yes
Controller Yes Yes
onChanged Yes Yes
onSubmitted Yes Yes
Form validation Not directly through a Form validator Yes
Best use General input and search Structured forms

51. Important Properties

Property Description
controller Reads and controls the input value.
decoration Controls labels, hints, borders, icons, and other visual elements.
keyboardType Specifies the desired keyboard type.
obscureText Hides entered text.
focusNode Controls focus.
autofocus Requests focus automatically.
textInputAction Controls the keyboard action button.
textCapitalization Controls capitalization behavior.
textAlign Controls horizontal alignment.
maxLines Controls the maximum number of lines.
minLines Controls the minimum number of lines.
maxLength Limits the number of characters.
readOnly Prevents editing while keeping the field enabled.
enabled Enables or disables the field.
onChanged Runs whenever input changes.
onSubmitted Runs when the input is submitted.
inputFormatters Formats or restricts input.

52. Interview Questions

Q1. What is TextField in Flutter?

TextField is a Flutter widget used to accept editable text input from users.

Q2. What is InputDecoration?

InputDecoration is used to customize the appearance and supporting elements of a text input field.

Q3. How do you read the value entered in a TextField?

Use a TextEditingController and access its text property.

Q4. What is obscureText?

obscureText hides characters entered into the field and is commonly used for passwords.

Q5. How do you create a multiline input?

Set maxLines to a value greater than one or use maxLines: null when appropriate.

Q6. What is onChanged?

onChanged is a callback that runs whenever the user changes the text.

Q7. What is the difference between TextField and TextFormField?

TextField is a general input widget, while TextFormField integrates with Form and provides form-specific functionality such as validation.

Q8. Why should TextEditingController be disposed?

Controllers should be disposed when they are no longer needed so their associated resources can be released.

Q9. What is FocusNode?

FocusNode identifies and manages keyboard focus for a specific input widget.

Q10. How can you restrict input?

Input can be restricted or formatted using appropriate inputFormatters.


53. Quick Revision

  • TextField is used to create editable text input fields.
  • InputDecoration controls the field's appearance.
  • labelText identifies the field.
  • hintText provides input guidance.
  • prefixIcon and suffixIcon add useful icons.
  • keyboardType selects an appropriate input keyboard.
  • obscureText is useful for passwords.
  • TextEditingController reads and controls input values.
  • onChanged responds to input changes.
  • onSubmitted responds when the user submits input.
  • FocusNode helps manage focus.
  • maxLines creates multiline fields.
  • maxLength limits the number of characters.
  • inputFormatters can restrict or format input.
  • TextFormField is preferred when the field needs Form validation.
  • Controllers and FocusNodes should be disposed when they are no longer needed.

54. Official Flutter Resources


55. JustAcademy Flutter Training Resources


56. Key Takeaways

Creating text input fields is a fundamental Flutter skill. The TextField widget provides a flexible way to collect user input, while InputDecoration makes it possible to create clear and professional input interfaces. Controllers can be used to retrieve and manage entered values, callbacks can respond to user actions, keyboard types can improve the input experience, and focus management can make multi-field forms easier to use.

For structured forms that require validation, TextFormField should be used with Form and appropriate validators. Combining these concepts allows developers to create practical login forms, registration forms, search interfaces, profile screens, feedback forms, and many other Flutter applications.

Previous Next
whatsapp