Unverified Commit 6bb412e3 authored by Qun Cheng's avatar Qun Cheng Committed by GitHub

Added `controller` and `onSelected` properties to DropdownMenu (#116259)

parent e5e21c98
......@@ -9,23 +9,31 @@ import 'package:flutter/material.dart';
void main() => runApp(const DropdownMenuExample());
class DropdownMenuExample extends StatelessWidget {
class DropdownMenuExample extends StatefulWidget {
const DropdownMenuExample({super.key});
List<DropdownMenuEntry> getEntryList() {
final List<DropdownMenuEntry> entries = <DropdownMenuEntry>[];
@override
State<DropdownMenuExample> createState() => _DropdownMenuExampleState();
}
for (int index = 0; index < EntryLabel.values.length; index++) {
// Disabled item 1, 2 and 6.
final bool enabled = index != 1 && index != 2 && index != 6;
entries.add(DropdownMenuEntry(label: EntryLabel.values[index].label, enabled: enabled));
}
return entries;
}
class _DropdownMenuExampleState extends State<DropdownMenuExample> {
final TextEditingController colorController = TextEditingController();
final TextEditingController iconController = TextEditingController();
ColorLabel? selectedColor;
IconLabel? selectedIcon;
@override
Widget build(BuildContext context) {
final List<DropdownMenuEntry> dropdownMenuEntries = getEntryList();
final List<DropdownMenuEntry<ColorLabel>> colorEntries = <DropdownMenuEntry<ColorLabel>>[];
for (final ColorLabel color in ColorLabel.values) {
colorEntries.add(
DropdownMenuEntry<ColorLabel>(value: color, label: color.label, enabled: color.label != 'Grey'));
}
final List<DropdownMenuEntry<IconLabel>> iconEntries = <DropdownMenuEntry<IconLabel>>[];
for (final IconLabel icon in IconLabel.values) {
iconEntries.add(DropdownMenuEntry<IconLabel>(value: icon, label: icon.label));
}
return MaterialApp(
theme: ThemeData(
......@@ -34,25 +42,53 @@ class DropdownMenuExample extends StatelessWidget {
),
home: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 20),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownMenu(
label: const Text('Label'),
dropdownMenuEntries: dropdownMenuEntries,
DropdownMenu<ColorLabel>(
initialSelection: ColorLabel.green,
controller: colorController,
label: const Text('Color'),
dropdownMenuEntries: colorEntries,
onSelected: (ColorLabel? color) {
setState(() {
selectedColor = color;
});
},
),
const SizedBox(width: 20),
DropdownMenu(
DropdownMenu<IconLabel>(
controller: iconController,
enableFilter: true,
leadingIcon: const Icon(Icons.search),
label: const Text('Label'),
dropdownMenuEntries: dropdownMenuEntries,
label: const Text('Icon'),
dropdownMenuEntries: iconEntries,
inputDecorationTheme: const InputDecorationTheme(filled: true),
onSelected: (IconLabel? icon) {
setState(() {
selectedIcon = icon;
});
},
)
],
),
),
if (selectedColor != null && selectedIcon != null)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('You selected a ${selectedColor?.label} ${selectedIcon?.label}'),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 5),
child: Icon(selectedIcon?.icon, color: selectedColor?.color,))
],
)
else const Text('Please select a color and an icon.')
],
)
),
),
......@@ -60,15 +96,25 @@ class DropdownMenuExample extends StatelessWidget {
}
}
enum EntryLabel {
item0('Item 0'),
item1('Item 1'),
item2('Item 2'),
item3('Item 3'),
item4('Item 4'),
item5('Item 5'),
item6('Item 6');
enum ColorLabel {
blue('Blue', Colors.blue),
pink('Pink', Colors.pink),
green('Green', Colors.green),
yellow('Yellow', Colors.yellow),
grey('Grey', Colors.grey);
const ColorLabel(this.label, this.color);
final String label;
final Color color;
}
enum IconLabel {
smile('Smile', Icons.sentiment_satisfied_outlined),
cloud('Cloud', Icons.cloud_outlined,),
brush('Brush', Icons.brush_outlined),
heart('Heart', Icons.favorite);
const EntryLabel(this.label);
const IconLabel(this.label, this.icon);
final String label;
final IconData icon;
}
......@@ -43,11 +43,11 @@ void main() {
theme: themeData,
home: const Scaffold(
body: Center(
child: DropdownMenu(
dropdownMenuEntries: <DropdownMenuEntry>[
DropdownMenuEntry(label: 'Item 0'),
DropdownMenuEntry(label: 'Item 1'),
DropdownMenuEntry(label: 'Item 2'),
child: DropdownMenu<int>(
dropdownMenuEntries: <DropdownMenuEntry<int>>[
DropdownMenuEntry<int>(value: 0, label: 'Item 0'),
DropdownMenuEntry<int>(value: 1, label: 'Item 1'),
DropdownMenuEntry<int>(value: 2, label: 'Item 2'),
],
),
),
......@@ -122,11 +122,11 @@ void main() {
theme: theme,
home: const Scaffold(
body: Center(
child: DropdownMenu(
dropdownMenuEntries: <DropdownMenuEntry>[
DropdownMenuEntry(label: 'Item 0'),
DropdownMenuEntry(label: 'Item 1'),
DropdownMenuEntry(label: 'Item 2'),
child: DropdownMenu<int>(
dropdownMenuEntries: <DropdownMenuEntry<int>>[
DropdownMenuEntry<int>(value: 0, label: 'Item 0'),
DropdownMenuEntry<int>(value: 1, label: 'Item 1'),
DropdownMenuEntry<int>(value: 2, label: 'Item 2'),
],
),
),
......@@ -223,11 +223,11 @@ void main() {
data: dropdownMenuTheme,
child: const Scaffold(
body: Center(
child: DropdownMenu(
dropdownMenuEntries: <DropdownMenuEntry>[
DropdownMenuEntry(label: 'Item 0'),
DropdownMenuEntry(label: 'Item 1'),
DropdownMenuEntry(label: 'Item 2'),
child: DropdownMenu<int>(
dropdownMenuEntries: <DropdownMenuEntry<int>>[
DropdownMenuEntry<int>(value: 0, label: 'Item 0'),
DropdownMenuEntry<int>(value: 1, label: 'Item 1'),
DropdownMenuEntry<int>(value: 2, label: 'Item 2'),
],
),
),
......@@ -326,7 +326,7 @@ void main() {
data: dropdownMenuTheme,
child: Scaffold(
body: Center(
child: DropdownMenu(
child: DropdownMenu<int>(
textStyle: TextStyle(
color: Colors.pink,
backgroundColor: Colors.cyan,
......@@ -345,10 +345,10 @@ void main() {
),
),
inputDecorationTheme: const InputDecorationTheme(filled: true, fillColor: Colors.deepPurple),
dropdownMenuEntries: const <DropdownMenuEntry>[
DropdownMenuEntry(label: 'Item 0'),
DropdownMenuEntry(label: 'Item 1'),
DropdownMenuEntry(label: 'Item 2'),
dropdownMenuEntries: const <DropdownMenuEntry<int>>[
DropdownMenuEntry<int>(value: 0, label: 'Item 0'),
DropdownMenuEntry<int>(value: 1, label: 'Item 1'),
DropdownMenuEntry<int>(value: 2, label: 'Item 2'),
],
),
),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment