|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +/// Flutter code sample for [DropdownMenu]s. The first dropdown menu has an outlined border |
| 6 | +/// which is the default configuration, and the second one has a filled input decoration. |
| 7 | +
|
| 8 | +import 'package:flutter/material.dart'; |
| 9 | + |
| 10 | +void main() => runApp(const DropdownMenuExample()); |
| 11 | + |
| 12 | +class DropdownMenuExample extends StatelessWidget { |
| 13 | + const DropdownMenuExample({super.key}); |
| 14 | + |
| 15 | + List<DropdownMenuEntry> getEntryList() { |
| 16 | + final List<DropdownMenuEntry> entries = <DropdownMenuEntry>[]; |
| 17 | + |
| 18 | + for (int index = 0; index < EntryLabel.values.length; index++) { |
| 19 | + // Disabled item 1, 2 and 6. |
| 20 | + final bool enabled = index != 1 && index != 2 && index != 6; |
| 21 | + entries.add(DropdownMenuEntry(label: EntryLabel.values[index].label, enabled: enabled)); |
| 22 | + } |
| 23 | + return entries; |
| 24 | + } |
| 25 | + |
| 26 | + @override |
| 27 | + Widget build(BuildContext context) { |
| 28 | + final List<DropdownMenuEntry> dropdownMenuEntries = getEntryList(); |
| 29 | + |
| 30 | + return MaterialApp( |
| 31 | + theme: ThemeData( |
| 32 | + useMaterial3: true, |
| 33 | + colorSchemeSeed: Colors.green |
| 34 | + ), |
| 35 | + home: Scaffold( |
| 36 | + body: SafeArea( |
| 37 | + child: Padding( |
| 38 | + padding: const EdgeInsets.only(top: 20), |
| 39 | + child: Row( |
| 40 | + mainAxisAlignment: MainAxisAlignment.center, |
| 41 | + children: <Widget>[ |
| 42 | + DropdownMenu( |
| 43 | + label: const Text('Label'), |
| 44 | + dropdownMenuEntries: dropdownMenuEntries, |
| 45 | + ), |
| 46 | + const SizedBox(width: 20), |
| 47 | + DropdownMenu( |
| 48 | + enableFilter: true, |
| 49 | + leadingIcon: const Icon(Icons.search), |
| 50 | + label: const Text('Label'), |
| 51 | + dropdownMenuEntries: dropdownMenuEntries, |
| 52 | + inputDecorationTheme: const InputDecorationTheme(filled: true), |
| 53 | + ) |
| 54 | + ], |
| 55 | + ), |
| 56 | + ) |
| 57 | + ), |
| 58 | + ), |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +enum EntryLabel { |
| 64 | + item0('Item 0'), |
| 65 | + item1('Item 1'), |
| 66 | + item2('Item 2'), |
| 67 | + item3('Item 3'), |
| 68 | + item4('Item 4'), |
| 69 | + item5('Item 5'), |
| 70 | + item6('Item 6'); |
| 71 | + |
| 72 | + const EntryLabel(this.label); |
| 73 | + final String label; |
| 74 | +} |
0 commit comments