22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- // which is the default configuration, and the second one has a filled input decoration.
6-
75import 'package:flutter/material.dart' ;
86
9- /// Flutter code sample for [DropdownMenu] s. The first dropdown menu has an outlined border.
7+ // Flutter code sample for [DropdownMenu]s. The first dropdown menu
8+ // has the default outlined border and demos using the
9+ // [DropdownMenuEntry] style parameter to customize its appearance.
10+ // The second dropdown menu customizes the appearance of the dropdown
11+ // menu's text field with its [InputDecorationTheme] parameter.
12+
13+ void main () {
14+ runApp (const DropdownMenuExample ());
15+ }
16+
17+ // DropdownMenuEntry labels and values for the first dropdown menu.
18+ enum ColorLabel {
19+ blue ('Blue' , Colors .blue),
20+ pink ('Pink' , Colors .pink),
21+ green ('Green' , Colors .green),
22+ yellow ('Orange' , Colors .orange),
23+ grey ('Grey' , Colors .grey);
24+
25+ const ColorLabel (this .label, this .color);
26+ final String label;
27+ final Color color;
28+ }
29+
30+ // DropdownMenuEntry labels and values for the second dropdown menu.
31+ enum IconLabel {
32+ smile ('Smile' , Icons .sentiment_satisfied_outlined),
33+ cloud (
34+ 'Cloud' ,
35+ Icons .cloud_outlined,
36+ ),
37+ brush ('Brush' , Icons .brush_outlined),
38+ heart ('Heart' , Icons .favorite);
1039
11- void main () => runApp (const DropdownMenuExample ());
40+ const IconLabel (this .label, this .icon);
41+ final String label;
42+ final IconData icon;
43+ }
1244
1345class DropdownMenuExample extends StatefulWidget {
1446 const DropdownMenuExample ({super .key});
@@ -25,18 +57,6 @@ class _DropdownMenuExampleState extends State<DropdownMenuExample> {
2557
2658 @override
2759 Widget build (BuildContext context) {
28- final List <DropdownMenuEntry <ColorLabel >> colorEntries = < DropdownMenuEntry <ColorLabel >> [];
29- for (final ColorLabel color in ColorLabel .values) {
30- colorEntries.add (
31- DropdownMenuEntry <ColorLabel >(value: color, label: color.label, enabled: color.label != 'Grey' ),
32- );
33- }
34-
35- final List <DropdownMenuEntry <IconLabel >> iconEntries = < DropdownMenuEntry <IconLabel >> [];
36- for (final IconLabel icon in IconLabel .values) {
37- iconEntries.add (DropdownMenuEntry <IconLabel >(value: icon, label: icon.label));
38- }
39-
4060 return MaterialApp (
4161 theme: ThemeData (
4262 useMaterial3: true ,
@@ -55,20 +75,30 @@ class _DropdownMenuExampleState extends State<DropdownMenuExample> {
5575 initialSelection: ColorLabel .green,
5676 controller: colorController,
5777 label: const Text ('Color' ),
58- dropdownMenuEntries: colorEntries,
5978 onSelected: (ColorLabel ? color) {
6079 setState (() {
6180 selectedColor = color;
6281 });
6382 },
83+ dropdownMenuEntries: ColorLabel .values.map <DropdownMenuEntry <ColorLabel >>(
84+ (ColorLabel color) {
85+ return DropdownMenuEntry <ColorLabel >(
86+ value: color,
87+ label: color.label,
88+ enabled: color.label != 'Grey' ,
89+ style: MenuItemButton .styleFrom (
90+ foregroundColor: color.color,
91+ ),
92+ );
93+ }
94+ ).toList (),
6495 ),
65- const SizedBox (width: 20 ),
96+ const SizedBox (width: 24 ),
6697 DropdownMenu <IconLabel >(
6798 controller: iconController,
6899 enableFilter: true ,
69100 leadingIcon: const Icon (Icons .search),
70101 label: const Text ('Icon' ),
71- dropdownMenuEntries: iconEntries,
72102 inputDecorationTheme: const InputDecorationTheme (
73103 filled: true ,
74104 contentPadding: EdgeInsets .symmetric (vertical: 5.0 ),
@@ -78,7 +108,16 @@ class _DropdownMenuExampleState extends State<DropdownMenuExample> {
78108 selectedIcon = icon;
79109 });
80110 },
81- )
111+ dropdownMenuEntries: IconLabel .values.map <DropdownMenuEntry <IconLabel >>(
112+ (IconLabel icon) {
113+ return DropdownMenuEntry <IconLabel >(
114+ value: icon,
115+ label: icon.label,
116+ leadingIcon: Icon (icon.icon),
117+ );
118+ },
119+ ).toList (),
120+ ),
82121 ],
83122 ),
84123 ),
@@ -105,29 +144,3 @@ class _DropdownMenuExampleState extends State<DropdownMenuExample> {
105144 );
106145 }
107146}
108-
109- enum ColorLabel {
110- blue ('Blue' , Colors .blue),
111- pink ('Pink' , Colors .pink),
112- green ('Green' , Colors .green),
113- yellow ('Yellow' , Colors .yellow),
114- grey ('Grey' , Colors .grey);
115-
116- const ColorLabel (this .label, this .color);
117- final String label;
118- final Color color;
119- }
120-
121- enum IconLabel {
122- smile ('Smile' , Icons .sentiment_satisfied_outlined),
123- cloud (
124- 'Cloud' ,
125- Icons .cloud_outlined,
126- ),
127- brush ('Brush' , Icons .brush_outlined),
128- heart ('Heart' , Icons .favorite);
129-
130- const IconLabel (this .label, this .icon);
131- final String label;
132- final IconData icon;
133- }
0 commit comments