menu_demo.dart 7.17 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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
class MenuDemo extends StatefulWidget {
Hans Muller's avatar
Hans Muller committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  MenuDemo({ Key key }) : super(key: key);

  MenuDemoState createState() => new MenuDemoState();
}

class MenuDemoState extends State<MenuDemo> {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  final String _simpleValue1 = 'Menu item value one';
  final String _simpleValue2 = 'Menu item value two';
  final String _simpleValue3 = 'Menu item value three';
  String _simpleValue;

  final String _checkedValue1 = 'One';
  final String _checkedValue2 = 'Two';
  final String _checkedValue3 = 'Free';
  final String _checkedValue4 = 'Four';
  List<String> _checkedValues;

  void initState() {
    super.initState();
    _simpleValue = _simpleValue2;
    _checkedValues = <String>[_checkedValue3];
  }

  void showInSnackBar(String value) {
    _scaffoldKey.currentState.showSnackBar(new SnackBar(
     content: new Text(value)
    ));
  }

  void showMenuSelection(String value) {
    if (<String>[_simpleValue1, _simpleValue2, _simpleValue3].contains(value))
      _simpleValue = value;
    showInSnackBar('You selected: $value');
  }

  void showCheckedMenuSelections(String value) {
    if (_checkedValues.contains(value))
      _checkedValues.remove(value);
    else
      _checkedValues.add(value);

    showInSnackBar('Checked $_checkedValues');
  }

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

  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
59 60 61
      appBar: new AppBar(
        title: new Text('Menus'),
        actions: <Widget>[
Hans Muller's avatar
Hans Muller committed
62 63
          new PopupMenuButton<String>(
            onSelected: showMenuSelection,
Hans Muller's avatar
Hans Muller committed
64 65
            items: <PopupMenuItem<String>>[
              new PopupMenuItem<String>(
66 67
                value: 'AppBar Menu',
                child: new Text('AppBar Menu')
Hans Muller's avatar
Hans Muller committed
68
              ),
Hans Muller's avatar
Hans Muller committed
69
              new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
70 71 72
                value: 'Right Here',
                child: new Text('Right Here')
              ),
Hans Muller's avatar
Hans Muller committed
73
              new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
74 75 76 77 78 79 80 81
                value: 'Hooray!',
                child: new Text('Hooray!')
              ),
            ]
          )
        ]
      ),
      body: new Block(
82
        padding: const EdgeInsets.all(8.0),
Hans Muller's avatar
Hans Muller committed
83 84 85 86 87
        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.
          new ListItem(
88 89
            title: new Text('An item with a context menu button'),
            trailing: new PopupMenuButton<String>(
Hans Muller's avatar
Hans Muller committed
90
              onSelected: showMenuSelection,
Hans Muller's avatar
Hans Muller committed
91 92
              items: <PopupMenuItem<String>>[
                new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
93 94 95
                  value: _simpleValue1,
                  child: new Text('Context menu item one')
                ),
Hans Muller's avatar
Hans Muller committed
96
                new PopupMenuItem<String>(
97
                  enabled: false,
Hans Muller's avatar
Hans Muller committed
98 99
                  child: new Text('A disabled menu item')
                ),
Hans Muller's avatar
Hans Muller committed
100
                new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
101 102 103 104 105 106 107 108 109 110
                  value: _simpleValue3,
                  child: new 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.
          new ListItem(
111 112
            title: new Text('An item with a sectioned menu'),
            trailing: new PopupMenuButton<String>(
Hans Muller's avatar
Hans Muller committed
113
              onSelected: showMenuSelection,
Hans Muller's avatar
Hans Muller committed
114
              items: <PopupMenuEntry<String>>[
Hans Muller's avatar
Hans Muller committed
115
                new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
116 117
                  value: 'Preview',
                  child: new ListItem(
118 119
                    leading: new Icon(icon: Icons.visibility),
                    title: new Text('Preview')
Hans Muller's avatar
Hans Muller committed
120 121
                  )
                ),
Hans Muller's avatar
Hans Muller committed
122
                new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
123 124
                  value: 'Share',
                  child: new ListItem(
125 126
                    leading: new Icon(icon: Icons.person_add),
                    title: new Text('Share')
Hans Muller's avatar
Hans Muller committed
127 128
                  )
                ),
Hans Muller's avatar
Hans Muller committed
129
                new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
130 131
                  value: 'Get Link',
                  child: new ListItem(
132 133
                    leading: new Icon(icon: Icons.link),
                    title: new Text('Get Link')
Hans Muller's avatar
Hans Muller committed
134 135
                  )
                ),
Hans Muller's avatar
Hans Muller committed
136
                new PopupMenuDivider(),
Hans Muller's avatar
Hans Muller committed
137
                new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
138 139
                  value: 'Remove',
                  child: new ListItem(
140 141
                    leading: new Icon(icon: Icons.delete),
                    title: new Text('Remove')
Hans Muller's avatar
Hans Muller committed
142 143 144 145 146 147 148 149 150 151 152 153
                  )
                )
              ]
            )
          ),
          // 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.
          new PopupMenuButton<String>(
            initialValue: _simpleValue,
            onSelected: showMenuSelection,
            child: new ListItem(
154 155
              title: new Text('An item with a simple menu'),
              subtitle: new Text(_simpleValue)
Hans Muller's avatar
Hans Muller committed
156
            ),
Hans Muller's avatar
Hans Muller committed
157 158
            items: <PopupMenuItem<String>>[
              new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
159 160 161
                value: _simpleValue1,
                child: new Text(_simpleValue1)
              ),
Hans Muller's avatar
Hans Muller committed
162
              new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
163 164 165
                value: _simpleValue2,
                child: new Text(_simpleValue2)
              ),
Hans Muller's avatar
Hans Muller committed
166
              new PopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
167 168 169 170 171 172 173 174
                value: _simpleValue3,
                child: new Text(_simpleValue3)
              )
            ]
          ),
          // Pressing the PopupMenuButton on the right of this item shows a menu
          // whose items have checked icons that reflect this app's state.
          new ListItem(
175 176
            title: new Text('An item with a checklist menu'),
            trailing: new PopupMenuButton<String>(
Hans Muller's avatar
Hans Muller committed
177
              onSelected: showCheckedMenuSelections,
Hans Muller's avatar
Hans Muller committed
178
              items: <PopupMenuItem<String>>[
179
                new CheckedPopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
180
                  value: _checkedValue1,
181
                  checked: isChecked(_checkedValue1),
182
                  child: new Text(_checkedValue1)
Hans Muller's avatar
Hans Muller committed
183
                ),
Hans Muller's avatar
Hans Muller committed
184 185
                new CheckedPopupMenuItem<String>(
                  value: _checkedValue2,
186 187
                  enabled: false,
                  checked: isChecked(_checkedValue2),
188
                  child: new Text(_checkedValue2)
Hans Muller's avatar
Hans Muller committed
189
                ),
Hans Muller's avatar
Hans Muller committed
190
                new CheckedPopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
191
                  value: _checkedValue3,
192
                  checked: isChecked(_checkedValue3),
193
                  child: new Text(_checkedValue3)
Hans Muller's avatar
Hans Muller committed
194
                ),
Hans Muller's avatar
Hans Muller committed
195
                new CheckedPopupMenuItem<String>(
Hans Muller's avatar
Hans Muller committed
196
                  value: _checkedValue4,
197
                  checked: isChecked(_checkedValue4),
198 199
                  child: new Text(_checkedValue4)
                )
Hans Muller's avatar
Hans Muller committed
200 201 202 203 204 205 206 207
              ]
            )
          )
        ]
      )
    );
  }
}