buttons_demo.dart 6.47 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 add dimension to mostly flat layouts. They emphasize "
11 12
  "functions on busy or wide spaces.";

13
const String _raisedCode = 'buttons_raised';
14 15

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

20
const String _flatCode = 'buttons_flat';
21

22 23 24 25
const String _dropdownText =
  "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.";
26

27
const String _dropdownCode = 'buttons_dropdown';
28

29 30 31 32
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.";

33
const String _iconCode = 'buttons_icon';
34 35 36

const String _actionText =
  "Floating action buttons are used for a promoted action. They are "
37
  "distinguished by a circled icon floating above the UI and can have motion "
38 39 40
  "behaviors that include morphing, launching, and a transferring anchor "
  "point.";

41
const String _actionCode = 'buttons_action';
42

43
class ButtonsDemo extends StatefulWidget {
44 45
  static const String routeName = '/buttons';

46
  @override
47 48 49 50
  _ButtonsDemoState createState() => new _ButtonsDemoState();
}

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

86 87 88
    return new TabbedComponentDemoScaffold(
      title: 'Buttons',
      demos: demos
89 90 91
    );
  }

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

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

134 135 136
  // https://en.wikipedia.org/wiki/Free_Four
  String dropdown1Value = 'Free';
  String dropdown2Value = 'Four';
137 138

  Widget buildDropdownButton() {
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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()
            )
          )
        ]
189 190 191 192
      )
    );
  }

193 194 195 196 197 198
  bool iconButtonToggle = false;

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

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