fluttercnfluttercn
Components

Dropdown

A customizable dropdown select component for choosing from a list of options

Installation

fluttercn add dropdown

Usage

Basic Dropdown

Simple dropdown with options:

Basic dropdown preview
Dropdown<String>(
  value: selectedValue,
  placeholder: 'Select an option',
  items: [
    DropdownItem(value: 'apple', label: 'Apple'),
    DropdownItem(value: 'banana', label: 'Banana'),
    DropdownItem(value: 'orange', label: 'Orange'),
  ],
  onChanged: (value) {
    setState(() => selectedValue = value);
  },
)

Dropdown with label and description:

Dropdown with label preview
Dropdown<String>(
  value: selectedFruit,
  placeholder: 'Choose a fruit',
  label: 'Favorite Fruit',
  description: 'Select your favorite fruit from the list',
  items: [
    DropdownItem(value: 'apple', label: 'Apple'),
    DropdownItem(value: 'banana', label: 'Banana'),
    DropdownItem(value: 'orange', label: 'Orange'),
  ],
  onChanged: (value) {
    setState(() => selectedFruit = value);
  },
)

Sizes

Three predefined sizes for different contexts:

Dropdown sizes preview
// Small
Dropdown<String>(
  value: value,
  size: DropdownSize.sm,
  items: items,
  onChanged: (value) {},
)
 
// Medium (default)
Dropdown<String>(
  value: value,
  size: DropdownSize.md,
  items: items,
  onChanged: (value) {},
)
 
// Large
Dropdown<String>(
  value: value,
  size: DropdownSize.lg,
  items: items,
  onChanged: (value) {},
)

With Icons

Add icons to dropdown items for better visual context:

Dropdown with icons preview
Dropdown<String>(
  value: theme,
  label: 'Theme',
  items: [
    DropdownItem(
      value: 'light',
      label: 'Light',
      icon: Icons.light_mode,
    ),
    DropdownItem(
      value: 'dark',
      label: 'Dark',
      icon: Icons.dark_mode,
    ),
    DropdownItem(
      value: 'system',
      label: 'System',
      icon: Icons.settings_brightness,
    ),
  ],
  onChanged: (value) {
    setState(() => theme = value);
  },
)

With Descriptions

Add descriptions to items for additional context:

Dropdown with descriptions preview
Dropdown<String>(
  value: plan,
  label: 'Subscription Plan',
  items: [
    DropdownItem(
      value: 'free',
      label: 'Free',
      description: 'Basic features for personal use',
      icon: Icons.person,
    ),
    DropdownItem(
      value: 'pro',
      label: 'Pro',
      description: 'Advanced features for professionals',
      icon: Icons.star,
    ),
    DropdownItem(
      value: 'team',
      label: 'Team',
      description: 'Collaboration tools for teams',
      icon: Icons.group,
    ),
  ],
  onChanged: (value) {
    setState(() => plan = value);
  },
)

Error State

Show validation errors:

Dropdown error state preview
Dropdown<String>(
  value: null,
  label: 'Country',
  placeholder: 'Select your country',
  error: true,
  errorText: 'Please select a country',
  items: [
    DropdownItem(value: 'us', label: 'United States'),
    DropdownItem(value: 'uk', label: 'United Kingdom'),
    DropdownItem(value: 'ca', label: 'Canada'),
  ],
  onChanged: (value) {
    setState(() => selectedCountry = value);
  },
)

Disabled State

Disable dropdown interactions:

Dropdown disabled state preview
Dropdown<String>(
  value: 'option1',
  disabled: true,
  items: [
    DropdownItem(value: 'option1', label: 'Option 1'),
    DropdownItem(value: 'option2', label: 'Option 2'),
  ],
  onChanged: (value) {},
)

Examples

Form with Validation

Form validation example preview
class FormWithDropdown extends StatefulWidget {
  @override
  State<FormWithDropdown> createState() => _FormWithDropdownState();
}
 
class _FormWithDropdownState extends State<FormWithDropdown> {
  String? country;
  bool showError = false;
 
  void _validateAndSubmit() {
    if (country == null) {
      setState(() => showError = true);
      return;
    }
    
    // Submit form
    print('Selected country: $country');
  }
 
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Dropdown<String>(
          value: country,
          label: 'Country *',
          placeholder: 'Select your country',
          error: showError && country == null,
          errorText: showError && country == null 
              ? 'Country is required' 
              : null,
          items: [
            DropdownItem(value: 'us', label: 'United States'),
            DropdownItem(value: 'uk', label: 'United Kingdom'),
            DropdownItem(value: 'ca', label: 'Canada'),
            DropdownItem(value: 'au', label: 'Australia'),
          ],
          onChanged: (value) {
            setState(() {
              country = value;
              showError = false;
            });
          },
        ),
        SizedBox(height: AppTheme.spaceLg),
        Button(
          onPressed: _validateAndSubmit,
          fullWidth: true,
          child: Text('Submit'),
        ),
      ],
    );
  }
}

API Reference

PropertyTypeDefaultDescription
itemsList<DropdownItem<T>>requiredList of dropdown items
onChangedValueChanged<T?>?requiredCallback when selection changes
valueT?nullCurrent selected value
placeholderStringSelect an optionPlaceholder text
labelString?nullOptional label text
descriptionString?nullOptional description
sizeDropdownSizemdDropdown size
disabledboolfalseDisable interactions
errorboolfalseShow error state
errorTextString?nullError message
widthdouble?nullOptional width constraint
PropertyTypeDefaultDescription
valueTrequiredValue of the item
labelStringrequiredDisplay label
iconIconData?nullOptional icon
descriptionString?nullOptional description text
  • sm - Small (compact size)
  • md - Medium (default size)
  • lg - Large (spacious size)

Features

  • Type Safe - Generic type support for any value type
  • Customizable Sizes - Three size options for different contexts
  • Icons Support - Add icons to both trigger and items
  • Item Descriptions - Add secondary text to items
  • Keyboard Navigation - Full keyboard support (coming soon)
  • Validation States - Built-in error states and messages
  • Smooth Animations - Scale and rotation animations
  • Hover Effects - Visual feedback on hover
  • Custom Width - Flexible width constraints
  • Themed - Automatically adapts to light/dark mode

The dropdown automatically closes when clicking outside or selecting an item. The overlay positioning is smart and adjusts based on available screen space.

Make sure to provide unique values for each DropdownItem. Using duplicate values may cause unexpected selection behavior.

Best Practices

  • Use clear, concise labels for dropdown items
  • Add icons when they provide meaningful context
  • Use descriptions for items that need additional explanation
  • Show error states with helpful error messages
  • Provide a meaningful placeholder text
  • Use disabled state for unavailable options
  • Keep the number of items reasonable (consider search for long lists)
  • Use appropriate size for the context (sm for compact UIs, lg for forms)
  • Always handle the onChanged callback
  • Use generic types appropriately (e.g., Dropdown<int> for numeric values)