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 14
  "functions on busy or wide spaces.";

const String _raisedCode =
15
"""// Create a raised button.
16 17 18 19 20 21 22
new RaisedButton(
  child: new Text('BUTTON TITLE'),
  onPressed: () {
    // Perform some action
  }
);

23 24 25
// Create a disabled button.
// Buttons are disabled when onPressed isn't
// specified or is null.
26 27 28
new RaisedButton(
  child: new Text('BUTTON TITLE')
);""";
29 30

const String _flatText =
31
  "# Flat buttons\n"
32
  "A flat button displays an ink splash on press "
33 34 35
  "but does not lift. Use flat buttons on toolbars, in dialogs and "
  "inline with padding";

36
const String _flatCode =
37
"""// Create a flat button.
38 39 40 41
new FlatButton(
  child: new Text('BUTTON TITLE'),
  onPressed: () {
    // Perform some action
42
  }
43
);
44

45 46 47
// Create a disabled button.
// Buttons are disabled when onPressed isn't
// specified or is null.
48 49 50
new FlatButton(
  child: new Text('BUTTON TITLE')
);""";
51

52 53 54 55 56
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.";
57

58
const String _dropdownCode =
59
"""// Member variable holding value.
60
String dropdownValue
61

62
// Drop down button with string values.
63 64 65 66
new DropDownButton<String>(
  value: dropdownValue,
  onChanged: (String newValue) {
    // null indicates the user didn't select a
67
    // new value.
68 69 70 71 72 73 74 75 76 77 78 79 80
    setState(() {
      if (newValue != null)
        dropdownValue = newValue;
    });
  },
  items: <String>['One', 'Two', 'Free', 'Four']
    .map((String value) {
      return new DropDownMenuItem<String>(
        value: value,
        child: new Text(value));
    })
    .toList()
)""";
81

82 83 84 85 86
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.";

const String _iconCode =
87 88
"""// Member variable holding toggle value.
bool value;
89

90
// Toggleable icon button.
91 92 93
new IconButton(
  icon: Icons.thumb_up,
  onPressed: () {
94
    setState(() => value = !value);
95
  },
96
  color: value ? Theme.of(context).primaryColor : null
97 98 99 100 101
)""";

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

const String _actionCode =
107
"""// Floating action button in Scaffold.
108 109 110 111 112 113 114 115
new Scaffold(
  appBar: new AppBar(
    title: new Text('Demo')
  ),
  floatingActionButton: new FloatingActionButton(
    child: new Icon(icon: Icons.add)
  )
);""";
116

117
class ButtonsDemo extends StatefulWidget {
118
  @override
119 120 121 122
  _ButtonsDemoState createState() => new _ButtonsDemoState();
}

class _ButtonsDemoState extends State<ButtonsDemo> {
123
  @override
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
  Widget build(BuildContext context) {
    List<ComponentDemoTabData> demos = <ComponentDemoTabData>[
      new ComponentDemoTabData(
        tabName: 'RAISED',
        description: _raisedText,
        widget: buildRaisedButton(),
        exampleCode: _raisedCode
      ),
      new ComponentDemoTabData(
        tabName: 'FLAT',
        description: _flatText,
        widget: buildFlatButton(),
        exampleCode: _flatCode
      ),
      new ComponentDemoTabData(
        tabName: 'DROPDOWN',
        description: _dropdownText,
        widget: buildDropdownButton(),
        exampleCode:
        _dropdownCode
      ),
      new ComponentDemoTabData(
        tabName: 'ICON',
        description: _iconText,
        widget: buildIconButton(),
        exampleCode: _iconCode
      ),
      new ComponentDemoTabData(
        tabName: 'ACTION',
        description: _actionText,
        widget: buildActionButton(),
        exampleCode: _actionCode
      ),
157 158
    ];

159 160 161
    return new TabbedComponentDemoScaffold(
      title: 'Buttons',
      demos: demos
162 163 164
    );
  }

165 166 167 168 169
  Widget buildRaisedButton() {
    return new Align(
      alignment: new FractionalOffset(0.5, 0.4),
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.collapse,
170 171
        children: <Widget>[
          new RaisedButton(
172
            child: new Text('RAISED BUTTON'),
173
            onPressed: () {
174
              // Perform some action
175 176 177
            }
          ),
          new RaisedButton(
178
            child: new Text('DISABLED')
179 180 181 182 183 184
          )
        ]
      )
    );
  }

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  Widget buildFlatButton() {
    return new Align(
      alignment: new FractionalOffset(0.5, 0.4),
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.collapse,
        children: <Widget>[
          new FlatButton(
            child: new Text('FLAT BUTTON'),
            onPressed: () {
              // Perform some action
            }
          ),
          new FlatButton(
            child: new Text('DISABLED')
          )
        ]
201 202 203 204
      )
    );
  }

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  String dropdownValue = 'Free';

  Widget buildDropdownButton() {
    return new Align(
      alignment: new FractionalOffset(0.5, 0.4),
      child: new DropDownButton<String>(
        value: dropdownValue,
        onChanged: (String newValue) {
          setState(() {
            if (newValue != null)
              dropdownValue = newValue;
          });
        },
        items: <String>['One', 'Two', 'Free', 'Four']
          .map((String value) {
            return new DropDownMenuItem<String>(
              value: value,
              child: new Text(value));
          })
          .toList()
225 226 227 228
      )
    );
  }

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  bool iconButtonToggle = false;

  Widget buildIconButton() {
    return new Align(
      alignment: new FractionalOffset(0.5, 0.4),
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.collapse,
        children: <Widget>[
          new IconButton(
            icon: Icons.thumb_up,
            onPressed: () {
              setState(() => iconButtonToggle = !iconButtonToggle);
            },
            color: iconButtonToggle ? Theme.of(context).primaryColor : null
          ),
          new IconButton(
            icon: Icons.thumb_up
          )
        ]
      )
249 250 251
    );
  }

252 253 254 255 256 257 258 259
  Widget buildActionButton() {
    return new Align(
      alignment: new FractionalOffset(0.5, 0.4),
      child: new FloatingActionButton(
        child: new Icon(icon: Icons.add),
        onPressed: () {
          // Perform some action
        }
260 261 262 263
      )
    );
  }
}