buttons_demo.dart 12.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
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
import '../../gallery/demo.dart';
8

9 10
const String _elevatedText =
    'Elevated buttons add dimension to mostly flat layouts. They emphasize '
11
    'functions on busy or wide spaces.';
12

13
const String _elevatedCode = 'buttons_elevated';
14

15 16
const String _textText = 'A text button displays an ink splash on press '
    'but does not lift. Use text buttons on toolbars, in dialogs and '
17
    'inline with padding';
18

19
const String _textCode = 'buttons_text';
20

21 22 23
const String _outlinedText =
    'Outlined buttons become opaque and elevate when pressed. They are often '
    'paired with elevated buttons to indicate an alternative, secondary action.';
24

25
const String _outlinedCode = 'buttons_outlined';
26

27
const String _dropdownText =
28
    "A dropdown button displays a menu that's used to select a value from a "
29 30
    'small set of values. The button displays the current value and a down '
    'arrow.';
31

32
const String _dropdownCode = 'buttons_dropdown';
33

34
const String _iconText =
35
    'IconButtons are appropriate for toggle buttons that allow a single choice '
36
    "to be selected or deselected, such as adding or removing an item's star.";
37

38
const String _iconCode = 'buttons_icon';
39 40

const String _actionText =
41 42 43 44
    'Floating action buttons are used for a promoted action. They are '
    'distinguished by a circled icon floating above the UI and can have motion '
    'behaviors that include morphing, launching, and a transferring anchor '
    'point.';
45

46
const String _actionCode = 'buttons_action';
47

48
class ButtonsDemo extends StatefulWidget {
49 50
  const ButtonsDemo({Key? key}) : super(key: key);

51
  static const String routeName = '/material/buttons';
52

53
  @override
54
  State<ButtonsDemo> createState() => _ButtonsDemoState();
55 56 57
}

class _ButtonsDemoState extends State<ButtonsDemo> {
58
  OutlinedBorder? _buttonShape;
59

60
  @override
61
  Widget build(BuildContext context) {
62
    final List<ComponentDemoTabData> demos = <ComponentDemoTabData>[
63
      ComponentDemoTabData(
64 65 66 67
        tabName: 'ELEVATED',
        description: _elevatedText,
        demoWidget: buildElevatedButton(_buttonShape),
        exampleCodeTag: _elevatedCode,
68
        documentationUrl: 'https://api.flutter.dev/flutter/material/ElevatedButton-class.html',
69
      ),
70
      ComponentDemoTabData(
71 72 73 74
        tabName: 'TEXT',
        description: _textText,
        demoWidget: buildTextButton(_buttonShape),
        exampleCodeTag: _textCode,
75
        documentationUrl: 'https://api.flutter.dev/flutter/material/TextButton-class.html',
76
      ),
77
      ComponentDemoTabData(
78 79 80 81
        tabName: 'OUTLINED',
        description: _outlinedText,
        demoWidget: buildOutlinedButton(_buttonShape),
        exampleCodeTag: _outlinedCode,
82
        documentationUrl: 'https://api.flutter.dev/flutter/material/OutlinedButton-class.html',
83
      ),
84
      ComponentDemoTabData(
85 86
        tabName: 'DROPDOWN',
        description: _dropdownText,
87
        demoWidget: buildDropdownButton(),
88
        exampleCodeTag: _dropdownCode,
89
        documentationUrl: 'https://api.flutter.dev/flutter/material/DropdownButton-class.html',
90
      ),
91
      ComponentDemoTabData(
92 93
        tabName: 'ICON',
        description: _iconText,
94
        demoWidget: buildIconButton(),
95
        exampleCodeTag: _iconCode,
96
        documentationUrl: 'https://api.flutter.dev/flutter/material/IconButton-class.html',
97
      ),
98
      ComponentDemoTabData(
99 100
        tabName: 'ACTION',
        description: _actionText,
101
        demoWidget: buildActionButton(),
102
        exampleCodeTag: _actionCode,
103
        documentationUrl: 'https://api.flutter.dev/flutter/material/FloatingActionButton-class.html',
104
      ),
105 106
    ];

107
    return TabbedComponentDemoScaffold(
108
      title: 'Buttons',
109
      demos: demos,
110
      actions: <Widget>[
111
        IconButton(
112
          icon: const Icon(Icons.sentiment_very_satisfied, semanticLabel: 'Update shape'),
113 114 115 116 117 118 119
          onPressed: () {
            setState(() {
              _buttonShape = _buttonShape == null ? const StadiumBorder() : null;
            });
          },
        ),
      ],
120 121 122
    );
  }

123
  Widget buildElevatedButton(OutlinedBorder? shape) {
124
    final ButtonStyle style = ElevatedButton.styleFrom(shape: shape);
125
    return Align(
126
      alignment: const Alignment(0.0, -0.2),
127
      child: Column(
128
        mainAxisSize: MainAxisSize.min,
129
        children: <Widget>[
130
          ButtonBar(
131 132
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
133 134 135
              ElevatedButton(
                style: style,
                child: const Text('ELEVATED BUTTON', semanticsLabel: 'ELEVATED BUTTON 1'),
136 137 138 139
                onPressed: () {
                  // Perform some action
                },
              ),
140
              const ElevatedButton(
141
                onPressed: null,
142
                child: Text('DISABLED', semanticsLabel: 'DISABLED BUTTON 1'),
143 144 145
              ),
            ],
          ),
146
          ButtonBar(
147 148
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
149 150
              ElevatedButton.icon(
                style: style,
151
                icon: const Icon(Icons.add, size: 18.0),
152
                label: const Text('ELEVATED BUTTON', semanticsLabel: 'ELEVATED BUTTON 2'),
153 154 155 156
                onPressed: () {
                  // Perform some action
                },
              ),
157 158
              ElevatedButton.icon(
                style: style,
159
                icon: const Icon(Icons.add, size: 18.0),
160
                label: const Text('DISABLED', semanticsLabel: 'DISABLED BUTTON 2'),
161
                onPressed: () {},
162 163
              ),
            ],
164
          ),
165 166
        ],
      ),
167 168 169
    );
  }

170
  Widget buildTextButton(OutlinedBorder? shape) {
171
    final ButtonStyle style = ElevatedButton.styleFrom(shape: shape);
172
    return Align(
173
      alignment: const Alignment(0.0, -0.2),
174
      child: Column(
175 176
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
177
          ButtonBar(
178 179
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
180 181 182
              TextButton(
                style: style,
                child: const Text('TEXT BUTTON', semanticsLabel: 'TEXT BUTTON 1'),
183 184 185 186
                onPressed: () {
                  // Perform some action
                },
              ),
187
              const TextButton(
188
                onPressed: null,
189
                child: Text('DISABLED', semanticsLabel: 'DISABLED BUTTON 3',),
190 191 192
              ),
            ],
          ),
193
          ButtonBar(
194 195
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
196 197
              TextButton.icon(
                style: style,
198
                icon: const Icon(Icons.add_circle_outline, size: 18.0),
199
                label: const Text('TEXT BUTTON', semanticsLabel: 'TEXT BUTTON 2'),
200 201 202 203
                onPressed: () {
                  // Perform some action
                },
              ),
204 205
              TextButton.icon(
                style: style,
206
                icon: const Icon(Icons.add_circle_outline, size: 18.0),
207
                label: const Text('DISABLED', semanticsLabel: 'DISABLED BUTTON 4'),
208
                onPressed: () {},
209 210 211 212 213 214 215 216
              ),
            ],
          ),
        ],
      ),
    );
  }

217
  Widget buildOutlinedButton(OutlinedBorder? shape) {
218
    final ButtonStyle style = ElevatedButton.styleFrom(shape: shape);
219
    return Align(
220
      alignment: const Alignment(0.0, -0.2),
221
      child: Column(
222
        mainAxisSize: MainAxisSize.min,
223
        children: <Widget>[
224
          ButtonBar(
225 226
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
227 228 229
              OutlinedButton(
                style: style,
                child: const Text('OUTLINED BUTTON', semanticsLabel: 'OUTLINED BUTTON 1'),
230 231 232 233
                onPressed: () {
                  // Perform some action
                },
              ),
234 235
              OutlinedButton(
                style: style,
236
                onPressed: null,
237
                child: const Text('DISABLED', semanticsLabel: 'DISABLED BUTTON 5'),
238 239 240
              ),
            ],
          ),
241
          ButtonBar(
242 243
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
244 245
              OutlinedButton.icon(
                style: style,
246
                icon: const Icon(Icons.add, size: 18.0),
247
                label: const Text('OUTLINED BUTTON', semanticsLabel: 'OUTLINED BUTTON 2'),
248 249 250 251
                onPressed: () {
                  // Perform some action
                },
              ),
252
              OutlinedButton.icon(
253
                icon: const Icon(Icons.add, size: 18.0),
254
                label: const Text('DISABLED', semanticsLabel: 'DISABLED BUTTON 6'),
255 256 257
                onPressed: null,
              ),
            ],
258
          ),
259 260
        ],
      ),
261 262 263
    );
  }

264
  // https://en.wikipedia.org/wiki/Free_Four
265 266 267
  String? dropdown1Value = 'Free';
  String? dropdown2Value;
  String? dropdown3Value = 'Four';
268 269

  Widget buildDropdownButton() {
270
    return Padding(
271
      padding: const EdgeInsets.all(24.0),
272
      child: Column(
273 274
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
275
          ListTile(
276
            title: const Text('Simple dropdown:'),
277
            trailing: DropdownButton<String>(
278
              value: dropdown1Value,
279
              onChanged: (String? newValue) {
280
                setState(() {
281
                  dropdown1Value = newValue;
282 283
                });
              },
284
              items: <String>['One', 'Two', 'Free', 'Four'].map<DropdownMenuItem<String>>((String value) {
285
                return DropdownMenuItem<String>(
286
                  value: value,
287
                  child: Text(value),
288 289 290
                );
              }).toList(),
            ),
291
          ),
292
          const SizedBox(
293
            height: 24.0,
294
          ),
295
          ListTile(
296
            title: const Text('Dropdown with a hint:'),
297
            trailing: DropdownButton<String>(
298
              value: dropdown2Value,
299
              hint: const Text('Choose'),
300
              onChanged: (String? newValue) {
301
                setState(() {
302
                  dropdown2Value = newValue;
303 304
                });
              },
305
              items: <String>['One', 'Two', 'Free', 'Four'].map<DropdownMenuItem<String>>((String value) {
306
                return DropdownMenuItem<String>(
307
                  value: value,
308
                  child: Text(value),
309 310 311
                );
              }).toList(),
            ),
312
          ),
313
          const SizedBox(
314 315
            height: 24.0,
          ),
316
          ListTile(
317
            title: const Text('Scrollable dropdown:'),
318
            trailing: DropdownButton<String>(
319
              value: dropdown3Value,
320
              onChanged: (String? newValue) {
321 322 323 324 325 326
                setState(() {
                  dropdown3Value = newValue;
                });
              },
              items: <String>[
                  'One', 'Two', 'Free', 'Four', 'Can', 'I', 'Have', 'A', 'Little',
327
                  'Bit', 'More', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten',
328
                 ]
329
                .map<DropdownMenuItem<String>>((String value) {
330
                  return DropdownMenuItem<String>(
331
                    value: value,
332
                    child: Text(value),
333 334 335 336 337
                  );
                })
                .toList(),
             ),
          ),
338 339
        ],
      ),
340 341 342
    );
  }

343 344 345
  bool iconButtonToggle = false;

  Widget buildIconButton() {
346
    return Align(
347
      alignment: const Alignment(0.0, -0.2),
348
      child: Row(
349
        mainAxisSize: MainAxisSize.min,
350
        children: <Widget>[
351
          IconButton(
352 353 354 355
            icon: const Icon(
              Icons.thumb_up,
              semanticLabel: 'Thumbs up',
            ),
356 357 358
            onPressed: () {
              setState(() => iconButtonToggle = !iconButtonToggle);
            },
359
            color: iconButtonToggle ? Theme.of(context).primaryColor : null,
360
          ),
361
          const IconButton(
362
            icon: Icon(
363
              Icons.thumb_up,
364
              semanticLabel: 'Thumbs not up',
365
            ),
366
            onPressed: null,
367
          ),
368
        ]
369
        .map<Widget>((Widget button) => SizedBox(width: 64.0, height: 64.0, child: button))
370 371
        .toList(),
      ),
372 373 374
    );
  }

375
  Widget buildActionButton() {
376
    return Align(
377
      alignment: const Alignment(0.0, -0.2),
378
      child: FloatingActionButton(
379
        tooltip: 'floating action button',
380
        child: const Icon(Icons.add),
381 382
        onPressed: () {
          // Perform some action
383 384
        },
      ),
385 386 387
    );
  }
}