• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6import 'package:flutter/widgets.dart';
7
8import 'controls_constants.dart';
9export 'controls_constants.dart';
10
11/// A test page with a checkbox, three radio buttons, and a switch.
12class SelectionControlsPage extends StatefulWidget {
13  @override
14  State<StatefulWidget> createState() => _SelectionControlsPageState();
15}
16
17class _SelectionControlsPageState extends State<SelectionControlsPage> {
18  static const ValueKey<String> checkbox1Key = ValueKey<String>(checkboxKeyValue);
19  static const ValueKey<String> checkbox2Key = ValueKey<String>(disabledCheckboxKeyValue);
20  static const ValueKey<String> radio1Key = ValueKey<String>(radio1KeyValue);
21  static const ValueKey<String> radio2Key = ValueKey<String>(radio2KeyValue);
22  static const ValueKey<String> radio3Key = ValueKey<String>(radio3KeyValue);
23  static const ValueKey<String> switchKey = ValueKey<String>(switchKeyValue);
24  static const ValueKey<String> labeledSwitchKey = ValueKey<String>(labeledSwitchKeyValue);
25  bool _isChecked = false;
26  bool _isOn = false;
27  bool _isLabeledOn = false;
28  int _radio = 0;
29
30  void _updateCheckbox(bool newValue) {
31    setState(() {
32      _isChecked = newValue;
33    });
34  }
35
36  void _updateRadio(int newValue) {
37    setState(() {
38      _radio = newValue;
39    });
40  }
41
42  void _updateSwitch(bool newValue) {
43    setState(() {
44      _isOn = newValue;
45    });
46  }
47
48  void _updateLabeledSwitch(bool newValue) {
49    setState(() {
50      _isLabeledOn = newValue;
51    });
52  }
53
54  @override
55  Widget build(BuildContext context) {
56    return Scaffold(
57      appBar: AppBar(leading: const BackButton(key: ValueKey<String>('back'))),
58      body: Material(
59        child: Column(children: <Widget>[
60          Row(
61            children: <Widget>[
62              Checkbox(
63                key: checkbox1Key,
64                value: _isChecked,
65                onChanged: _updateCheckbox,
66              ),
67              const Checkbox(
68                key: checkbox2Key,
69                value: false,
70                onChanged: null,
71              ),
72            ],
73          ),
74          const Spacer(),
75          Row(children: <Widget>[
76            Radio<int>(key: radio1Key, value: 0, groupValue: _radio, onChanged: _updateRadio),
77            Radio<int>(key: radio2Key, value: 1, groupValue: _radio, onChanged: _updateRadio),
78            Radio<int>(key: radio3Key, value: 2, groupValue: _radio, onChanged: _updateRadio),
79          ]),
80          const Spacer(),
81          Switch(
82            key: switchKey,
83            value: _isOn,
84            onChanged: _updateSwitch,
85          ),
86          const Spacer(),
87          MergeSemantics(
88            child: Row(
89              children: <Widget>[
90                const Text(switchLabel),
91                Switch(
92                  key: labeledSwitchKey,
93                  value: _isLabeledOn,
94                  onChanged: _updateLabeledSwitch,
95                ),
96              ],
97            ),
98          ),
99        ]),
100      ),
101    );
102  }
103}
104