main
1import 'package:flutter/material.dart';
2
3/// A [ListTile] that allows choosing from a dropdown.
4class DropDownListTile<T> extends StatefulWidget {
5 /// Creates a list tile that allows choosing an item from a dropdown.
6 ///
7 /// Using this is equivalent to using a [ListTile] with a trailing [DropdownButton]. Please refer to those classes for
8 /// argument definitions.
9 const DropDownListTile({
10 required this.title,
11 required this.value,
12 required this.onChanged,
13 required this.items,
14 this.leading,
15 this.subtitle,
16 super.key,});
17
18 /// Primary description of the tile.
19 final Widget title;
20
21 /// Secondary description below the title.
22 final Widget? subtitle;
23
24 /// A widget to display before the title.
25 final Widget? leading;
26
27 /// The value of the currently selected [DropdownMenuItem].
28 final T? value;
29
30 /// A list of items the user can select.
31 final List<DropdownMenuItem<T>> items;
32
33 /// Called when the selection changes.
34 final void Function(T? value) onChanged;
35
36 @override
37 State<DropDownListTile<T>> createState() => _DropDownListTileState<T>();
38}
39
40class _DropDownListTileState<T> extends State<DropDownListTile<T>> {
41 final focusNode = FocusNode();
42
43
44 @override
45 void dispose() {
46 focusNode.dispose();
47 super.dispose();
48 }
49
50 @override
51 Widget build(BuildContext context) => ListTile(
52 title: widget.title,
53 subtitle: widget.subtitle,
54 leading: widget.leading,
55 onTap: focusNode.requestFocus,
56 trailing: DropdownButton<T>(
57 focusNode: focusNode,
58 value: widget.value,
59 items: widget.items,
60 onChanged: widget.onChanged,
61 ),
62 );
63}