selection_controls_demo.dart 5.81 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 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 _checkboxText =
10 11 12
  'Checkboxes allow the user to select multiple options from a set. '
  'A normal checkbox\'s value is true or false and a tristate checkbox\'s '
  'value can also be null.';
13

14
const String _checkboxCode = 'selectioncontrols_checkbox';
15 16

const String _radioText =
17 18 19
  'Radio buttons allow the user to select one option from a set. Use radio '
  'buttons for exclusive selection if you think that the user needs to see '
  'all available options side-by-side.';
20

21
const String _radioCode = 'selectioncontrols_radio';
22 23

const String _switchText =
24 25 26
  'On/off switches toggle the state of a single settings option. The option '
  'that the switch controls, as well as the state it’s in, should be made '
  'clear from the corresponding inline label.';
27

28
const String _switchCode = 'selectioncontrols_switch';
29

30
class SelectionControlsDemo extends StatefulWidget {
31
  static const String routeName = '/material/selection-controls';
32

33
  @override
34
  _SelectionControlsDemoState createState() => _SelectionControlsDemoState();
35 36
}

37
class _SelectionControlsDemoState extends State<SelectionControlsDemo> {
38 39
  @override
  Widget build(BuildContext context) {
40
    final List<ComponentDemoTabData> demos = <ComponentDemoTabData>[
41
      ComponentDemoTabData(
42
        tabName: 'CHECKBOX',
43
        description: _checkboxText,
44
        demoWidget: buildCheckbox(),
45 46
        exampleCodeTag: _checkboxCode,
        documentationUrl: 'https://docs.flutter.io/flutter/material/Checkbox-class.html',
47
      ),
48
      ComponentDemoTabData(
49
        tabName: 'RADIO',
50
        description: _radioText,
51
        demoWidget: buildRadio(),
52 53
        exampleCodeTag: _radioCode,
        documentationUrl: 'https://docs.flutter.io/flutter/material/Radio-class.html',
54
      ),
55
      ComponentDemoTabData(
56
        tabName: 'SWITCH',
57
        description: _switchText,
58
        demoWidget: buildSwitch(),
59 60
        exampleCodeTag: _switchCode,
        documentationUrl: 'https://docs.flutter.io/flutter/material/Switch-class.html',
61 62
      )
    ];
63

64
    return TabbedComponentDemoScaffold(
65
      title: 'Selection controls',
66 67
      demos: demos
    );
68 69
  }

70 71
  bool checkboxValueA = true;
  bool checkboxValueB = false;
72
  bool checkboxValueC;
73 74
  int radioValue = 0;
  bool switchValue = false;
75

76
  void handleRadioValueChanged(int value) {
77
    setState(() {
78
      radioValue = value;
79 80 81
    });
  }

82
  Widget buildCheckbox() {
83
    return Align(
84
      alignment: const Alignment(0.0, -0.2),
85
      child: Column(
86
        mainAxisSize: MainAxisSize.min,
87
        children: <Widget>[
88
          Row(
89
            mainAxisSize: MainAxisSize.min,
90
            children: <Widget>[
91
              Checkbox(
92 93 94 95 96 97 98
                value: checkboxValueA,
                onChanged: (bool value) {
                  setState(() {
                    checkboxValueA = value;
                  });
                },
              ),
99
              Checkbox(
100 101 102 103 104 105 106
                value: checkboxValueB,
                onChanged: (bool value) {
                  setState(() {
                    checkboxValueB = value;
                  });
                },
              ),
107
              Checkbox(
108 109 110 111 112 113 114 115 116
                value: checkboxValueC,
                tristate: true,
                onChanged: (bool value) {
                  setState(() {
                    checkboxValueC = value;
                  });
                },
              ),
            ],
117
          ),
118
          Row(
119
            mainAxisSize: MainAxisSize.min,
120
            children: const <Widget>[
121
              // Disabled checkboxes
122 123 124
              Checkbox(value: true, onChanged: null),
              Checkbox(value: false, onChanged: null),
              Checkbox(value: null, tristate: true, onChanged: null),
125 126 127 128 129 130 131 132
            ]
          )
        ]
      )
    );
  }

  Widget buildRadio() {
133
    return Align(
134
      alignment: const Alignment(0.0, -0.2),
135
      child: Column(
136
        mainAxisSize: MainAxisSize.min,
137
        children: <Widget>[
138
          Row(
139
            mainAxisSize: MainAxisSize.min,
140
            children: <Widget>[
141
              Radio<int>(
142 143 144 145
                value: 0,
                groupValue: radioValue,
                onChanged: handleRadioValueChanged
              ),
146
              Radio<int>(
147 148 149 150
                value: 1,
                groupValue: radioValue,
                onChanged: handleRadioValueChanged
              ),
151
              Radio<int>(
152 153 154 155 156
                value: 2,
                groupValue: radioValue,
                onChanged: handleRadioValueChanged
              )
            ]
157
          ),
158
          // Disabled radio buttons
159
          Row(
160
            mainAxisSize: MainAxisSize.min,
161
            children: const <Widget>[
162
              Radio<int>(
163
                value: 0,
164 165
                groupValue: 0,
                onChanged: null
166
              ),
167
              Radio<int>(
168
                value: 1,
169 170
                groupValue: 0,
                onChanged: null
171
              ),
172
              Radio<int>(
173
                value: 2,
174 175
                groupValue: 0,
                onChanged: null
176 177 178 179 180 181 182 183 184
              )
            ]
          )
        ]
      )
    );
  }

  Widget buildSwitch() {
185
    return Align(
186
      alignment: const Alignment(0.0, -0.2),
187
      child: Row(
188
        mainAxisSize: MainAxisSize.min,
189
        children: <Widget>[
190
          Switch.adaptive(
191 192 193
            value: switchValue,
            onChanged: (bool value) {
              setState(() {
194
                switchValue = value;
195 196 197
              });
            }
          ),
198
          // Disabled switches
199 200
          const Switch.adaptive(value: true, onChanged: null),
          const Switch.adaptive(value: false, onChanged: null),
201 202
        ],
      ),
203 204 205
    );
  }
}