|
| 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 | +import 'package:flutter/material.dart'; |
| 6 | + |
| 7 | +/// Flutter code sample for [DropdownMenu]. |
| 8 | +
|
| 9 | +const List<String> list = <String>['One', 'Two', 'Three', 'Four']; |
| 10 | + |
| 11 | +void main() => runApp(const DropdownMenuApp()); |
| 12 | + |
| 13 | +class DropdownMenuApp extends StatelessWidget { |
| 14 | + const DropdownMenuApp({super.key}); |
| 15 | + |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return MaterialApp( |
| 19 | + home: Scaffold( |
| 20 | + appBar: AppBar(title: const Text('DropdownMenu Sample')), |
| 21 | + body: const Center( |
| 22 | + child: DropdownMenuExample(), |
| 23 | + ), |
| 24 | + ), |
| 25 | + ); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class DropdownMenuExample extends StatefulWidget { |
| 30 | + const DropdownMenuExample({super.key}); |
| 31 | + |
| 32 | + @override |
| 33 | + State<DropdownMenuExample> createState() => _DropdownMenuExampleState(); |
| 34 | +} |
| 35 | + |
| 36 | +class _DropdownMenuExampleState extends State<DropdownMenuExample> { |
| 37 | + String dropdownValue = list.first; |
| 38 | + |
| 39 | + @override |
| 40 | + Widget build(BuildContext context) { |
| 41 | + final ColorScheme colorScheme = Theme.of(context).colorScheme; |
| 42 | + |
| 43 | + return ListView( |
| 44 | + children: <Widget>[ |
| 45 | + ListTile( |
| 46 | + tileColor: colorScheme.primaryContainer, |
| 47 | + title: const Column( |
| 48 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 49 | + children: <Widget>[ |
| 50 | + Text('enabled: true'), |
| 51 | + Text('requestFocusOnTap: true'), |
| 52 | + ], |
| 53 | + ), |
| 54 | + subtitle: Column( |
| 55 | + children: <Widget>[ |
| 56 | + DropdownMenu<String>( |
| 57 | + requestFocusOnTap: true, |
| 58 | + initialSelection: list.first, |
| 59 | + expandedInsets: EdgeInsets.zero, |
| 60 | + onSelected: (String? value) { |
| 61 | + setState(() { |
| 62 | + dropdownValue = value!; |
| 63 | + }); |
| 64 | + }, |
| 65 | + dropdownMenuEntries: |
| 66 | + list.map<DropdownMenuEntry<String>>((String value) { |
| 67 | + return DropdownMenuEntry<String>(value: value, label: value); |
| 68 | + }).toList(), |
| 69 | + ), |
| 70 | + const Text('Text cursor is shown when hovering over the DropdownMenu.'), |
| 71 | + ], |
| 72 | + ), |
| 73 | + ), |
| 74 | + const SizedBox(height: 20), |
| 75 | + ListTile( |
| 76 | + tileColor: colorScheme.primaryContainer, |
| 77 | + title: const Column( |
| 78 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 79 | + children: <Widget>[ |
| 80 | + Text('enabled: true'), |
| 81 | + Text('requestFocusOnTap: false'), |
| 82 | + ], |
| 83 | + ), |
| 84 | + subtitle: Column( |
| 85 | + children: <Widget>[ |
| 86 | + DropdownMenu<String>( |
| 87 | + requestFocusOnTap: false, |
| 88 | + initialSelection: list.first, |
| 89 | + expandedInsets: EdgeInsets.zero, |
| 90 | + onSelected: (String? value) { |
| 91 | + setState(() { |
| 92 | + dropdownValue = value!; |
| 93 | + }); |
| 94 | + }, |
| 95 | + dropdownMenuEntries: |
| 96 | + list.map<DropdownMenuEntry<String>>((String value) { |
| 97 | + return DropdownMenuEntry<String>(value: value, label: value); |
| 98 | + }).toList(), |
| 99 | + ), |
| 100 | + const Text('Clickable cursor is shown when hovering over the DropdownMenu.'), |
| 101 | + ], |
| 102 | + ), |
| 103 | + ), |
| 104 | + const SizedBox(height: 20), |
| 105 | + ListTile( |
| 106 | + tileColor: colorScheme.onInverseSurface, |
| 107 | + title: const Column( |
| 108 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 109 | + children: <Widget>[ |
| 110 | + Text('enabled: false'), |
| 111 | + Text('requestFocusOnTap: true'), |
| 112 | + ], |
| 113 | + ), |
| 114 | + subtitle: Column( |
| 115 | + children: <Widget>[ |
| 116 | + DropdownMenu<String>( |
| 117 | + enabled: false, |
| 118 | + requestFocusOnTap: true, |
| 119 | + initialSelection: list.first, |
| 120 | + expandedInsets: EdgeInsets.zero, |
| 121 | + onSelected: (String? value) { |
| 122 | + setState(() { |
| 123 | + dropdownValue = value!; |
| 124 | + }); |
| 125 | + }, |
| 126 | + dropdownMenuEntries: |
| 127 | + list.map<DropdownMenuEntry<String>>((String value) { |
| 128 | + return DropdownMenuEntry<String>(value: value, label: value); |
| 129 | + }).toList(), |
| 130 | + ), |
| 131 | + const Text('Default cursor is shown when hovering over the DropdownMenu.'), |
| 132 | + ], |
| 133 | + ), |
| 134 | + ), |
| 135 | + const SizedBox(height: 20), |
| 136 | + ListTile( |
| 137 | + tileColor: colorScheme.onInverseSurface, |
| 138 | + title: const Column( |
| 139 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 140 | + children: <Widget>[ |
| 141 | + Text('enabled: false'), |
| 142 | + Text('requestFocusOnTap: false'), |
| 143 | + ], |
| 144 | + ), |
| 145 | + subtitle: Column( |
| 146 | + children: <Widget>[ |
| 147 | + DropdownMenu<String>( |
| 148 | + enabled: false, |
| 149 | + requestFocusOnTap: false, |
| 150 | + initialSelection: list.first, |
| 151 | + expandedInsets: EdgeInsets.zero, |
| 152 | + onSelected: (String? value) { |
| 153 | + setState(() { |
| 154 | + dropdownValue = value!; |
| 155 | + }); |
| 156 | + }, |
| 157 | + dropdownMenuEntries: |
| 158 | + list.map<DropdownMenuEntry<String>>((String value) { |
| 159 | + return DropdownMenuEntry<String>(value: value, label: value); |
| 160 | + }).toList(), |
| 161 | + ), |
| 162 | + const Text('Default cursor is shown when hovering over the DropdownMenu.'), |
| 163 | + ], |
| 164 | + ), |
| 165 | + ), |
| 166 | + ], |
| 167 | + ); |
| 168 | + } |
| 169 | +} |
0 commit comments