Skip to content

Instantly share code, notes, and snippets.

@jamieastley
Created September 5, 2023 04:53
Show Gist options
  • Select an option

  • Save jamieastley/57adbe1be921468643d5ada98299f71e to your computer and use it in GitHub Desktop.

Select an option

Save jamieastley/57adbe1be921468643d5ada98299f71e to your computer and use it in GitHub Desktop.
Dropdown example
import 'package:flutter/material.dart';
// change '20' to item count you want to test behaviour
final List<String> list = List.generate(20, (i) => '$i');
void main() => runApp(const DropdownMenuApp());
class DropdownMenuApp extends StatelessWidget {
const DropdownMenuApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(useMaterial3: true),
home: Scaffold(
appBar: AppBar(title: const Text('DropdownMenu Sample')),
body: const Center(
child: DropdownMenuExample(),
),
),
);
}
}
class DropdownMenuExample extends StatefulWidget {
const DropdownMenuExample({super.key});
@override
State<DropdownMenuExample> createState() => _DropdownMenuExampleState();
}
class _DropdownMenuExampleState extends State<DropdownMenuExample> {
String dropdownValue = list.first;
@override
Widget build(BuildContext context) {
return DropdownMenu<String>(
initialSelection: list.first,
onSelected: (String? value) {
setState(() {
dropdownValue = value!;
});
},
dropdownMenuEntries: list.map<DropdownMenuEntry<String>>((String value) {
return DropdownMenuEntry<String>(value: value, label: value);
}).toList(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment