forked from flutter/assets-for-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toggle_buttons.dart
192 lines (174 loc) · 6.24 KB
/
toggle_buttons.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
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
// Copyright 2019 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 'dart:async';
import 'dart:io';
import 'package:diagram_capture/diagram_capture.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'diagram_step.dart';
const String _toggleButtonsSimple = 'toggle_buttons_simple';
const String _toggleButtonsRequired = 'toggle_buttons_required';
const String _toggleButtonsMutuallyExclusive = 'toggle_buttons_mutually_exclusive';
const String _toggleButtonsRequiredMutuallyExclusive = 'toggle_buttons_required_mutually_exclusive';
const double _kAnimationFrameRate = 60.0;
const Map<String, List<int>> tapSteps = <String, List<int>>{
_toggleButtonsSimple: <int>[0, 2, 0, 2],
_toggleButtonsRequired: <int>[0, 2, 0],
_toggleButtonsMutuallyExclusive: <int>[0, 2, 2],
_toggleButtonsRequiredMutuallyExclusive: <int>[0, 2, 2],
};
final List<GlobalKey> _iconKeys = <GlobalKey>[
GlobalKey(),
GlobalKey(),
GlobalKey(),
];
class ToggleButtonsDiagram extends StatefulWidget implements DiagramMetadata {
const ToggleButtonsDiagram(this.name, { Key? key }) : super(key: key);
@override
final String name;
@override
_ToggleButtonsDiagramState createState() => _ToggleButtonsDiagramState();
}
class _ToggleButtonsDiagramState extends State<ToggleButtonsDiagram> {
final List<bool> isSelected = <bool>[false, false, true];
@override
Widget build(BuildContext context) {
Widget returnWidget;
switch (widget.name) {
case _toggleButtonsSimple:
returnWidget = ToggleButtons(
children: <Widget>[
Icon(Icons.ac_unit, key: _iconKeys[0]),
Icon(Icons.call, key: _iconKeys[1]),
Icon(Icons.cake, key: _iconKeys[2]),
],
onPressed: (int index) {
setState(() {
isSelected[index] = !isSelected[index];
});
},
isSelected: isSelected,
);
break;
case _toggleButtonsRequired:
returnWidget = ToggleButtons(
children: <Widget>[
Icon(Icons.ac_unit, key: _iconKeys[0]),
Icon(Icons.call, key: _iconKeys[1]),
Icon(Icons.cake, key: _iconKeys[2]),
],
onPressed: (int index) {
int count = 0;
for (int index = 0; index < isSelected.length; index++) {
if (isSelected[index])
count += 1;
}
if (isSelected[index] && count < 2)
return;
setState(() {
isSelected[index] = !isSelected[index];
});
},
isSelected: isSelected,
);
break;
case _toggleButtonsMutuallyExclusive:
returnWidget = ToggleButtons(
children: <Widget>[
Icon(Icons.ac_unit, key: _iconKeys[0]),
Icon(Icons.call, key: _iconKeys[1]),
Icon(Icons.cake, key: _iconKeys[2]),
],
onPressed: (int index) {
setState(() {
for (int buttonIndex = 0; buttonIndex < isSelected.length; buttonIndex++) {
if (buttonIndex == index) {
isSelected[buttonIndex] = !isSelected[buttonIndex];
} else {
isSelected[buttonIndex] = false;
}
}
});
},
isSelected: isSelected,
);
break;
case _toggleButtonsRequiredMutuallyExclusive:
returnWidget = ToggleButtons(
children: <Widget>[
Icon(Icons.ac_unit, key: _iconKeys[0]),
Icon(Icons.call, key: _iconKeys[1]),
Icon(Icons.cake, key: _iconKeys[2]),
],
onPressed: (int index) {
setState(() {
for (int buttonIndex = 0; buttonIndex < isSelected.length; buttonIndex++) {
if (buttonIndex == index) {
isSelected[buttonIndex] = true;
} else {
isSelected[buttonIndex] = false;
}
}
});
},
isSelected: isSelected,
);
break;
default:
returnWidget = const Text('Error');
break;
}
return ConstrainedBox(
key: UniqueKey(),
constraints: BoxConstraints.tight(const Size(400, 100)),
child: Container(
alignment: FractionalOffset.center,
padding: const EdgeInsets.all(5.0),
color: Colors.white,
child: returnWidget,
),
);
}
}
class ToggleButtonsDiagramStep extends DiagramStep<ToggleButtonsDiagram> {
ToggleButtonsDiagramStep(DiagramController controller) : super(controller);
String _testName = '';
int _stepCount = 0;
@override
final String category = 'material';
@override
Future<List<ToggleButtonsDiagram>> get diagrams async => <ToggleButtonsDiagram>[
ToggleButtonsDiagram(_toggleButtonsSimple, key: UniqueKey()),
ToggleButtonsDiagram(_toggleButtonsRequired, key: UniqueKey()),
ToggleButtonsDiagram(_toggleButtonsMutuallyExclusive, key: UniqueKey()),
ToggleButtonsDiagram(_toggleButtonsRequiredMutuallyExclusive, key: UniqueKey()),
];
Future<void> tapIcons(DiagramController controller, Duration now) async {
RenderBox target;
if (now.inMilliseconds % 2000 == 0) {
final List<int> steps = tapSteps[_testName]!;
if (_stepCount < steps.length) {
final int targetIcon = steps[_stepCount];
target = _iconKeys[targetIcon].currentContext!.findRenderObject() as RenderBox;
final Offset targetOffset = target.localToGlobal(target.size.center(Offset.zero));
final TestGesture gesture = await controller.startGesture(targetOffset);
Future<void>.delayed(const Duration(milliseconds: 500), gesture.up);
_stepCount += 1;
}
}
}
@override
Future<File> generateDiagram(ToggleButtonsDiagram diagram) async {
_stepCount = 0;
_testName = diagram.name;
controller.builder = (BuildContext context) => diagram;
return await controller.drawAnimatedDiagramToFiles(
end: Duration(seconds: tapSteps[_testName]!.length * 2),
frameRate: _kAnimationFrameRate,
name: _testName,
category: category,
gestureCallback: tapIcons,
);
}
}