fluttercnfluttercn
Components

Text Field

A customizable text input component for single-line and multi-line text entry

Installation

fluttercn add text-field

Usage

Basic Text Field

Simple text input:

Basic text field preview
TextField(
  controller: controller,
  placeholder: 'Enter text here',
)

Text Field with Label

Text field with label and description:

Text field with label preview
TextField(
  controller: controller,
  label: 'Full Name',
  description: 'Enter your first and last name',
  placeholder: 'John Doe',
)

Sizes

Three predefined sizes for different contexts:

Text field sizes preview
// Small
TextField(
  controller: controller,
  size: TextFieldSize.sm,
  placeholder: 'Small text field',
)
 
// Medium (default)
TextField(
  controller: controller,
  size: TextFieldSize.md,
  placeholder: 'Medium text field',
)
 
// Large
TextField(
  controller: controller,
  size: TextFieldSize.lg,
  placeholder: 'Large text field',
)

With Icons

Add prefix or suffix icons:

Text field with icons preview
// Prefix icon
TextField(
  controller: emailController,
  label: 'Email',
  placeholder: 'you@example.com',
  prefixIcon: Icons.email,
)
 
// Suffix icon
TextField(
  controller: searchController,
  placeholder: 'Search...',
  suffixIcon: Icons.search,
)

Password Field

Text field with password visibility toggle:

Password field preview
TextField(
  controller: passwordController,
  label: 'Password',
  placeholder: 'Enter your password',
  obscureText: true,
  prefixIcon: Icons.lock,
)

Multi-line Text Field

Text area for longer content:

Multi-line text field preview
TextField(
  controller: bioController,
  label: 'Bio',
  placeholder: 'Tell us about yourself',
  maxLines: 4,
  minLines: 3,
)

Error State

Show validation errors:

Text field error state preview
TextField(
  controller: controller,
  label: 'Email',
  placeholder: 'you@example.com',
  error: true,
  errorText: 'Please enter a valid email address',
  prefixIcon: Icons.email,
)

Read-only State

Display text without allowing edits:

Text field read-only state preview
TextField(
  controller: controller,
  label: 'Account ID',
  readOnly: true,
)

Examples

Login Form

Login form example preview
Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    Text(
      'Sign In',
      style: TextStyle(
        fontSize: 24,
        fontWeight: FontWeight.bold,
      ),
    ),
    SizedBox(height: AppTheme.spaceMd),
    TextField(
      controller: emailController,
      label: 'Email',
      placeholder: 'you@example.com',
      prefixIcon: Icons.email,
      keyboardType: TextInputType.emailAddress,
      textInputAction: TextInputAction.next,
    ),
    SizedBox(height: AppTheme.spaceMd),
    TextField(
      controller: passwordController,
      label: 'Password',
      placeholder: 'Enter your password',
      obscureText: true,
      prefixIcon: Icons.lock,
      textInputAction: TextInputAction.done,
      onSubmitted: (value) {
        // Handle login
      },
    ),
    SizedBox(height: AppTheme.spaceLg),
    Button(
      onPressed: () {
        // Handle login
      },
      fullWidth: true,
      child: Text('Sign In'),
    ),
  ],
)

API Reference

TextField

PropertyTypeDefaultDescription
controllerTextEditingController?nullController for the text field
initialValueString?nullInitial text value
onChangedValueChanged<String>?nullCallback when text changes
onSubmittedValueChanged<String>?nullCallback when user submits
placeholderString?nullPlaceholder text
labelString?nullLabel text above field
descriptionString?nullDescription text below label
sizeTextFieldSizemdText field size
disabledboolfalseDisable interactions
readOnlyboolfalseMake field read-only
errorboolfalseShow error state
errorTextString?nullError message
prefixIconIconData?nullIcon before text
suffixIconIconData?nullIcon after text
obscureTextboolfalseHide text (for passwords)
maxLinesint?1Maximum lines (null for unlimited)
minLinesint?nullMinimum lines (for multi-line)
maxLengthint?nullMaximum character length
keyboardTypeTextInputType?nullKeyboard type
textInputActionTextInputAction?nullInput action button
autofocusboolfalseAuto-focus on mount
widthdouble?nullOptional width constraint

TextFieldSize

  • sm - Small (compact size)
  • md - Medium (default size)
  • lg - Large (spacious size)

Features

  • Customizable Sizes - Three size options for different contexts
  • Icon Support - Prefix and suffix icons
  • Password Toggle - Automatic visibility toggle for password fields
  • Multi-line - Support for text areas
  • Validation States - Built-in error states and messages
  • Focus States - Visual feedback on focus
  • Hover Effects - Visual feedback on hover
  • Character Limit - Optional max length with counter
  • Keyboard Types - Support for email, number, phone, etc.
  • Input Actions - Done, next, search buttons
  • Read-only Mode - Display text without editing
  • Themed - Automatically adapts to light/dark mode

Password fields automatically show a visibility toggle icon. The toggle allows users to show/hide their password while typing.

When using obscureText: true, the field will always be single-line regardless of the maxLines setting.

Best Practices

  • Always provide a clear label for the field
  • Use meaningful placeholder text as examples
  • Show error states with helpful messages
  • Use appropriate keyboard types (email, phone, number)
  • Set textInputAction to guide users through forms
  • Use maxLength to prevent excessively long input
  • Combine minLines and maxLines for flexible text areas
  • Use prefix icons for context (email icon for email fields)
  • Use obscureText for sensitive data like passwords
  • Clear error states when user starts typing
  • Use readOnly instead of disabled when showing non-editable data
  • Provide descriptions for fields that need clarification
  • Use appropriate sizes based on the UI context