buttons_demo.dart 6.56 KB
Newer Older
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
import '../gallery/demo.dart';
8 9

const String _raisedText =
10
  "# Raised buttons\n"
11
  "Raised buttons add dimension to mostly flat layouts. They emphasize "
12 13
  "functions on busy or wide spaces.";

14
const String _raisedCode = 'buttons_raised';
15 16

const String _flatText =
17
  "# Flat buttons\n"
18
  "A flat button displays an ink splash on press "
19 20 21
  "but does not lift. Use flat buttons on toolbars, in dialogs and "
  "inline with padding";

22
const String _flatCode = 'buttons_flat';
23

24 25 26 27 28
const String _dropdownText =
  "# Dropdown buttons\n"
  "A dropdown button displays a menu that's used to select a value from a "
  "small set of values. The button displays the current value and a down "
  "arrow.";
29

30
const String _dropdownCode = 'buttons_dropdown';
31

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

36
const String _iconCode = 'buttons_icon';
37 38 39 40

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

45
const String _actionCode = 'buttons_action';
46

47
class ButtonsDemo extends StatefulWidget {
48 49
  static const String routeName = '/buttons';

50
  @override
51 52 53 54
  _ButtonsDemoState createState() => new _ButtonsDemoState();
}

class _ButtonsDemoState extends State<ButtonsDemo> {
55
  @override
56 57 58 59 60 61
  Widget build(BuildContext context) {
    List<ComponentDemoTabData> demos = <ComponentDemoTabData>[
      new ComponentDemoTabData(
        tabName: 'RAISED',
        description: _raisedText,
        widget: buildRaisedButton(),
62
        exampleCodeTag: _raisedCode
63 64 65 66 67
      ),
      new ComponentDemoTabData(
        tabName: 'FLAT',
        description: _flatText,
        widget: buildFlatButton(),
68
        exampleCodeTag: _flatCode
69 70 71 72 73
      ),
      new ComponentDemoTabData(
        tabName: 'DROPDOWN',
        description: _dropdownText,
        widget: buildDropdownButton(),
74
        exampleCodeTag: _dropdownCode
75 76 77 78 79
      ),
      new ComponentDemoTabData(
        tabName: 'ICON',
        description: _iconText,
        widget: buildIconButton(),
80
        exampleCodeTag: _iconCode
81 82 83 84 85
      ),
      new ComponentDemoTabData(
        tabName: 'ACTION',
        description: _actionText,
        widget: buildActionButton(),
86
        exampleCodeTag: _actionCode
87
      ),
88 89
    ];

90 91 92
    return new TabbedComponentDemoScaffold(
      title: 'Buttons',
      demos: demos
93 94 95
    );
  }

96 97 98
  Widget buildRaisedButton() {
    return new Align(
      alignment: new FractionalOffset(0.5, 0.4),
99
      child: new ButtonBar(
100
        mainAxisSize: MainAxisSize.min,
101 102
        children: <Widget>[
          new RaisedButton(
103
            child: new Text('RAISED BUTTON'),
104
            onPressed: () {
105
              // Perform some action
106 107 108
            }
          ),
          new RaisedButton(
109 110
            child: new Text('DISABLED'),
            onPressed: null
111 112 113 114 115 116
          )
        ]
      )
    );
  }

117 118 119
  Widget buildFlatButton() {
    return new Align(
      alignment: new FractionalOffset(0.5, 0.4),
120
      child: new ButtonBar(
121
        mainAxisSize: MainAxisSize.min,
122 123 124 125 126 127 128 129
        children: <Widget>[
          new FlatButton(
            child: new Text('FLAT BUTTON'),
            onPressed: () {
              // Perform some action
            }
          ),
          new FlatButton(
130 131
            child: new Text('DISABLED'),
            onPressed: null
132 133
          )
        ]
134 135 136 137
      )
    );
  }

138 139 140
  // https://en.wikipedia.org/wiki/Free_Four
  String dropdown1Value = 'Free';
  String dropdown2Value = 'Four';
141 142

  Widget buildDropdownButton() {
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    return new Padding(
      padding: const EdgeInsets.all(24.0),
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          new ListItem(
            title: new Text('Scrollable dropdown:'),
            trailing: new DropDownButton<String>(
              value: dropdown1Value,
              onChanged: (String newValue) {
                setState(() {
                  if (newValue != null)
                    dropdown1Value = newValue;
                });
              },
              items: <String>[
                  'One', 'Two', 'Free', 'Four', 'Can', 'I', 'Have', 'A', 'Little',
                  'Bit', 'More', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten'
                 ]
                .map((String value) {
                  return new DropDownMenuItem<String>(
                    value: value,
                    child: new Text(value));
                })
                .toList()
             )
          ),
          new SizedBox(
            height: 24.0
          ),
          new ListItem(
            title: new Text('Simple dropdown:'),
            trailing: new DropDownButton<String>(
              value: dropdown2Value,
              onChanged: (String newValue) {
                setState(() {
                  if (newValue != null)
                    dropdown2Value = newValue;
                });
              },
              items: <String>['One', 'Two', 'Free', 'Four']
                .map((String value) {
                  return new DropDownMenuItem<String>(
                    value: value,
                    child: new Text(value));
                })
                .toList()
            )
          )
        ]
193 194 195 196
      )
    );
  }

197 198 199 200 201 202
  bool iconButtonToggle = false;

  Widget buildIconButton() {
    return new Align(
      alignment: new FractionalOffset(0.5, 0.4),
      child: new Row(
203
        mainAxisSize: MainAxisSize.min,
204 205
        children: <Widget>[
          new IconButton(
Ian Hickson's avatar
Ian Hickson committed
206
            icon: new Icon(Icons.thumb_up),
207 208 209 210 211 212
            onPressed: () {
              setState(() => iconButtonToggle = !iconButtonToggle);
            },
            color: iconButtonToggle ? Theme.of(context).primaryColor : null
          ),
          new IconButton(
Ian Hickson's avatar
Ian Hickson committed
213
            icon: new Icon(Icons.thumb_up),
214
            onPressed: null
215 216
          )
        ]
217 218
        .map((Widget button) => new SizedBox(width: 64.0, height: 64.0, child: button))
        .toList()
219
      )
220 221 222
    );
  }

223 224 225 226
  Widget buildActionButton() {
    return new Align(
      alignment: new FractionalOffset(0.5, 0.4),
      child: new FloatingActionButton(
Ian Hickson's avatar
Ian Hickson committed
227
        child: new Icon(Icons.add),
228 229 230
        onPressed: () {
          // Perform some action
        }
231 232 233 234
      )
    );
  }
}