TextField Widget and Attributes

TextField Widget and Attributes

ยท

3 min read

In the vibrant world of Flutter, user input plays a pivotal role in creating interactive and dynamic applications. The TextField widget emerges as a key player, allowing developers to effortlessly incorporate text input fields into their UI. In this blog post, let's explore the capabilities of the TextField widget and delve into its attributes through hands-on examples.

The TextField widget in Flutter is a versatile tool for capturing user input. Whether you're building a login screen, a search bar, or a form, the TextField empowers you to seamlessly handle text input, providing users with a responsive and engaging experience.

Key Attributes of TextField:

Now, let's explore some key attributes that provide you with control over the appearance and behavior of the TextField widget:

1. decoration:

  • The decoration property allows you to customize the visual appearance of the TextField, including elements like labels, borders, and hints.

2. controller:

  • If you need to interact with the content of the TextField programmatically, you can use the controller property to link it with a TextEditingController.

3. onChanged:

  • The onChanged property lets you specify a callback function that will be invoked whenever the text in the TextField changes. This is useful for real-time validation or updating other parts of your UI.

4. onSubmitted:

  • When the user submits the text (e.g., by pressing the enter key on the keyboard), the onSubmitted callback is triggered. This can be used to perform actions like submitting a form.

5. keyboardType:

  • The keyboardType property allows you to set the type of keyboard that should be displayed. Options include TextInputType.text, TextInputType.emailAddress, TextInputType.number, etc.

6. obscureText:

  • For password fields or any sensitive information, you can set obscureText to true to hide the entered text.

Basic Implementation:

Let's kick things off with a simple example to illustrate the fundamental use of the TextField widget:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter TextField Example'),
        ),
        body: Padding(
          padding: EdgeInsets.all(16),
          child: TextField(
            decoration: InputDecoration(
              labelText: 'Enter your text',
            ),
          ),
        ),
      ),
    );
  }
}

In this example, a basic TextField is integrated into a Flutter app, featuring a simple label ('Enter your text') as a placeholder.

Let's integrate these attributes into a comprehensive example:

TextField(
  decoration: InputDecoration(
    labelText: 'Password',
    hintText: 'Enter your password',
    icon: Icon(Icons.lock),
  ),
  controller: TextEditingController(),
  onChanged: (text) {
    // Handle text changes
  },
  onSubmitted: (text) {
    // Handle text submission
  },
  keyboardType: TextInputType.visiblePassword,
  obscureText: true,
)

In this example, we've customized the TextField with additional attributes such as a password hint, an icon, and callbacks for text changes and submission. Feel free to experiment with these attributes to tailor the TextField to your specific needs.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย