controller property

TextEditingController? controller
final

Controls the text being edited or selected in the menu.

If null, this widget will create its own TextEditingController.

A TextEditingController can be provided to programmatically clear or modify the displayed selection. For example, calling TextEditingController.clear clears the text displayed in the dropdown menu.

When tracking the selected value in a state variable, calling TextEditingController.clear alongside updating the state variable to null is required to clear the displayed selection, as setting initialSelection to null does not clear the text field.

This sample demonstrates how to programmatically clear the selection in a DropdownMenu using a TextEditingController.

To see it in action, copy and run this code snippet on DartPad.

import 'package:material_ui/material_ui.dart';

/// Flutter code sample for [DropdownMenu] showing how to programmatically
/// clear or reset the selection using a [TextEditingController].

void main() => runApp(const DropdownMenuClearExampleApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Clear DropdownMenu Sample')),
        body: const Center(child: DropdownMenuClearExample()),
      ),
    );
  }
}

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

  @override
  State<DropdownMenuClearExample> createState() =>
      _DropdownMenuClearExampleState();
}

enum ColorItem {
  red('Red'),
  green('Green'),
  blue('Blue');

  const ColorItem(this.label);
  final String label;
}

class _DropdownMenuClearExampleState extends State<DropdownMenuClearExample> {
  final TextEditingController _controller = TextEditingController();
  ColorItem? _selectedColor = ColorItem.red;

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

  void _clearSelection() {
    setState(() {
      _selectedColor = null;
      _controller.clear();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        DropdownMenu<ColorItem>(
          controller: _controller,
          initialSelection: _selectedColor,
          label: const Text('Color'),
          onSelected: (ColorItem? color) {
            setState(() {
              _selectedColor = color;
            });
          },
          dropdownMenuEntries: ColorItem.values
              .map<DropdownMenuEntry<ColorItem>>((ColorItem color) {
                return DropdownMenuEntry<ColorItem>(
                  value: color,
                  label: color.label,
                );
              })
              .toList(),
        ),
        const SizedBox(height: 20),
        FilledButton(
          onPressed: _clearSelection,
          child: const Text('Clear selection'),
        ),
        const SizedBox(height: 20),
        Text('Selected: ${_selectedColor?.label ?? 'None'}'),
      ],
    );
  }
}

See also:

  • initialSelection, which sets the initial selection when the widget is first created.

Implementation

// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/dropdown_menu/dropdown_menu.3.dart#body}
///
/// </callout-box>
///
/// See also:
///
///  * [initialSelection], which sets the initial selection when the widget is
///    first created.
final TextEditingController? controller;