menu_demo.dart 7.87 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hans Muller's avatar
Hans Muller committed
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

7 8
import '../../gallery/demo.dart';

9
class MenuDemo extends StatefulWidget {
10
  const MenuDemo({ super.key });
Hans Muller's avatar
Hans Muller committed
11

12
  static const String routeName = '/material/menu';
13

14
  @override
15
  MenuDemoState createState() => MenuDemoState();
Hans Muller's avatar
Hans Muller committed
16 17 18 19 20 21 22
}

class MenuDemoState extends State<MenuDemo> {

  final String _simpleValue1 = 'Menu item value one';
  final String _simpleValue2 = 'Menu item value two';
  final String _simpleValue3 = 'Menu item value three';
23
  String? _simpleValue;
Hans Muller's avatar
Hans Muller committed
24 25 26 27 28

  final String _checkedValue1 = 'One';
  final String _checkedValue2 = 'Two';
  final String _checkedValue3 = 'Free';
  final String _checkedValue4 = 'Four';
29
  late List<String> _checkedValues;
Hans Muller's avatar
Hans Muller committed
30

31
  @override
Hans Muller's avatar
Hans Muller committed
32 33 34 35 36 37 38
  void initState() {
    super.initState();
    _simpleValue = _simpleValue2;
    _checkedValues = <String>[_checkedValue3];
  }

  void showInSnackBar(String value) {
39
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
40
     content: Text(value),
Hans Muller's avatar
Hans Muller committed
41 42 43 44
    ));
  }

  void showMenuSelection(String value) {
45
    if (<String>[_simpleValue1, _simpleValue2, _simpleValue3].contains(value)) {
46
      setState(() => _simpleValue = value);
47
    }
Hans Muller's avatar
Hans Muller committed
48 49 50 51
    showInSnackBar('You selected: $value');
  }

  void showCheckedMenuSelections(String value) {
52
    if (_checkedValues.contains(value)) {
Hans Muller's avatar
Hans Muller committed
53
      _checkedValues.remove(value);
54
    } else {
Hans Muller's avatar
Hans Muller committed
55
      _checkedValues.add(value);
56
    }
Hans Muller's avatar
Hans Muller committed
57 58 59 60 61 62

    showInSnackBar('Checked $_checkedValues');
  }

  bool isChecked(String value) => _checkedValues.contains(value);

63
  @override
Hans Muller's avatar
Hans Muller committed
64
  Widget build(BuildContext context) {
65 66
    return Scaffold(
      appBar: AppBar(
67
        title: const Text('Menus'),
68
        actions: <Widget>[
69
          MaterialDemoDocumentationButton(MenuDemo.routeName),
70
          PopupMenuButton<String>(
Hans Muller's avatar
Hans Muller committed
71
            onSelected: showMenuSelection,
72
            itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
73
              const PopupMenuItem<String>(
74
                value: 'Toolbar menu',
75
                child: Text('Toolbar menu'),
Hans Muller's avatar
Hans Muller committed
76
              ),
77
              const PopupMenuItem<String>(
78
                value: 'Right here',
79
                child: Text('Right here'),
Hans Muller's avatar
Hans Muller committed
80
              ),
81
              const PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
82
                value: 'Hooray!',
83
                child: Text('Hooray!'),
Hans Muller's avatar
Hans Muller committed
84
              ),
Ian Hickson's avatar
Ian Hickson committed
85 86 87
            ],
          ),
        ],
Hans Muller's avatar
Hans Muller committed
88
      ),
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
      body: ListTileTheme(
        iconColor: Theme.of(context).brightness == Brightness.light
            ? Colors.grey[600]
            : Colors.grey[500],
        child: ListView(
          padding: kMaterialListPadding,
          children: <Widget>[
            // Pressing the PopupMenuButton on the right of this item shows
            // a simple menu with one disabled item. Typically the contents
            // of this "contextual menu" would reflect the app's state.
            ListTile(
              title: const Text('An item with a context menu button'),
              trailing: PopupMenuButton<String>(
                padding: EdgeInsets.zero,
                onSelected: showMenuSelection,
                itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
                  PopupMenuItem<String>(
                    value: _simpleValue1,
                    child: const Text('Context menu item one'),
                  ),
                  const PopupMenuItem<String>(
                    enabled: false,
                    child: Text('A disabled menu item'),
                  ),
                  PopupMenuItem<String>(
                    value: _simpleValue3,
                    child: const Text('Context menu item three'),
                  ),
                ],
              ),
            ),
            // Pressing the PopupMenuButton on the right of this item shows
            // a menu whose items have text labels and icons and a divider
            // That separates the first three items from the last one.
            ListTile(
              title: const Text('An item with a sectioned menu'),
              trailing: PopupMenuButton<String>(
                padding: EdgeInsets.zero,
                onSelected: showMenuSelection,
                itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
                  const PopupMenuItem<String>(
                    value: 'Preview',
                    child: ListTile(
                      leading: Icon(Icons.visibility),
                      title: Text('Preview'),
                    ),
                  ),
                  const PopupMenuItem<String>(
                    value: 'Share',
                    child: ListTile(
                      leading: Icon(Icons.person_add),
                      title: Text('Share'),
                    ),
                  ),
                  const PopupMenuItem<String>(
                    value: 'Get Link',
                    child: ListTile(
                      leading: Icon(Icons.link),
                      title: Text('Get link'),
                    ),
                  ),
                  const PopupMenuDivider(),
                  const PopupMenuItem<String>(
                    value: 'Remove',
                    child: ListTile(
                      leading: Icon(Icons.delete),
                      title: Text('Remove'),
                    ),
                  ),
                ],
              ),
            ),
            // This entire list item is a PopupMenuButton. Tapping anywhere shows
            // a menu whose current value is highlighted and aligned over the
            // list item's center line.
            PopupMenuButton<String>(
165
              padding: EdgeInsets.zero,
166
              initialValue: _simpleValue,
Hans Muller's avatar
Hans Muller committed
167
              onSelected: showMenuSelection,
168 169
              child: ListTile(
                title: const Text('An item with a simple menu'),
170
                subtitle: Text(_simpleValue!),
171
              ),
172
              itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
173
                PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
174
                  value: _simpleValue1,
175
                  child: Text(_simpleValue1),
Hans Muller's avatar
Hans Muller committed
176
                ),
177 178 179
                PopupMenuItem<String>(
                  value: _simpleValue2,
                  child: Text(_simpleValue2),
Hans Muller's avatar
Hans Muller committed
180
                ),
181
                PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
182
                  value: _simpleValue3,
183
                  child: Text(_simpleValue3),
Hans Muller's avatar
Hans Muller committed
184
                ),
185 186
              ],
            ),
187 188 189 190 191 192 193 194 195 196 197 198
            // Pressing the PopupMenuButton on the right of this item shows a menu
            // whose items have checked icons that reflect this app's state.
            ListTile(
              title: const Text('An item with a checklist menu'),
              trailing: PopupMenuButton<String>(
                padding: EdgeInsets.zero,
                onSelected: showCheckedMenuSelections,
                itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
                  CheckedPopupMenuItem<String>(
                    value: _checkedValue1,
                    checked: isChecked(_checkedValue1),
                    child: Text(_checkedValue1),
199
                  ),
200 201 202 203 204
                  CheckedPopupMenuItem<String>(
                    value: _checkedValue2,
                    enabled: false,
                    checked: isChecked(_checkedValue2),
                    child: Text(_checkedValue2),
205
                  ),
206 207 208 209
                  CheckedPopupMenuItem<String>(
                    value: _checkedValue3,
                    checked: isChecked(_checkedValue3),
                    child: Text(_checkedValue3),
210
                  ),
211 212 213 214
                  CheckedPopupMenuItem<String>(
                    value: _checkedValue4,
                    checked: isChecked(_checkedValue4),
                    child: Text(_checkedValue4),
215
                  ),
216
                ],
Hans Muller's avatar
Hans Muller committed
217
              ),
218
            ),
219 220
          ],
        ),
221
      ),
Hans Muller's avatar
Hans Muller committed
222 223 224
    );
  }
}