Created
September 5, 2023 04:53
-
-
Save jamieastley/57adbe1be921468643d5ada98299f71e to your computer and use it in GitHub Desktop.
Dropdown example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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