radial_menu.dart
8.14 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:Parlando/widgets/radial/src/radial_menu_button.dart';
import 'package:Parlando/widgets/radial/src/radial_menu_center_button.dart';
import 'package:Parlando/widgets/radial/src/radial_menu_item.dart';
const double _radiansPerDegree = math.pi / 180;
const double _startAngle = -120.0 * _radiansPerDegree;
typedef ItemAngleCalculator = double Function(int index);
/// A radial menu for selecting from a list of items.
///
/// A radial menu lets the user select from a number of items. It displays a
/// button that opens the menu, showing its items arranged in an arc. Selecting
/// an item triggers the animation of a progress bar drawn at the specified
/// [radius] around the central menu button.
///
/// The type `T` is the type of the values the radial menu represents. All the
/// entries in a given menu must represent values with consistent types.
/// Typically, an enum is used. Each [RadialMenuItem] in [items] must be
/// specialized with that same type argument.
///
/// Requires one of its ancestors to be a [Material] widget.
///
/// See also:
///
/// * [RadialMenuItem], the widget used to represent the [items].
/// * [RadialMenuCenterButton], the button used to open and close the menu.
class RadialMenu<T> extends StatefulWidget {
/// Creates a dropdown button.
///
/// The [items] must have distinct values.
///
/// The [radius], [menuAnimationDuration], and [progressAnimationDuration]
/// arguments must not be null (they all have defaults, so do not need to be
/// specified).
const RadialMenu({
Key? key,
required this.items,
required this.onSelected,
this.radius = 100.0,
this.menuAnimationDuration = const Duration(milliseconds: 1000),
this.progressAnimationDuration = const Duration(milliseconds: 1000),
}) : super(key: key);
/// The list of possible items to select among.
final List<RadialMenuItem<T>> items;
/// Called when the user selects an item.
final Function onSelected; // TODO why Function? not ValueChanged?
/// The radius of the arc used to lay out the items and draw the progress bar.
///
/// Defaults to 100.0.
final double radius;
/// Duration of the menu opening/closing animation.
///
/// Defaults to 1000 milliseconds.
final Duration menuAnimationDuration;
/// Duration of the action activation progress arc animation.
///
/// Defaults to 1000 milliseconds.
final Duration progressAnimationDuration;
@override
RadialMenuState createState() => RadialMenuState();
}
class RadialMenuState extends State<RadialMenu> with TickerProviderStateMixin {
late AnimationController _menuAnimationController;
late AnimationController _progressAnimationController;
bool _isOpen = false;
int _activeItemIndex = -1;
// todo: xqwzts: allow users to pass in their own calculator as a param.
// and change this to the default: radialItemAngleCalculator.
double calculateItemAngle(int index) {
double _itemSpacing = 120.0 / widget.items.length;
return _startAngle + index * _itemSpacing * _radiansPerDegree;
}
@override
void initState() {
super.initState();
_menuAnimationController = AnimationController(
duration: widget.menuAnimationDuration,
vsync: this,
);
_progressAnimationController = AnimationController(
duration: widget.progressAnimationDuration,
vsync: this,
);
}
@override
void dispose() {
_menuAnimationController.dispose();
_progressAnimationController.dispose();
super.dispose();
}
void _openMenu() {
_menuAnimationController.forward();
setState(() => _isOpen = true);
}
void _closeMenu() {
_menuAnimationController.reverse();
setState(() => _isOpen = false);
}
Future<void> _activate(int itemIndex) async {
setState(() => _activeItemIndex = itemIndex);
await _progressAnimationController.forward().orCancel;
widget.onSelected(widget.items[itemIndex].value);
_closeMenu();
}
/// Resets the menu to its initial (closed) state.
void reset() {
_menuAnimationController.reset();
_progressAnimationController.reverse();
setState(() {
_isOpen = false;
_activeItemIndex = -1;
});
}
Widget _buildActionButton(int index) {
final RadialMenuItem item = widget.items[index];
return LayoutId(
id: '${_RadialMenuLayout.actionButton}$index',
child: RadialMenuButton(
child: item,
backgroundColor: item.backgroundColor,
onPressed: () => _activate(index),
),
);
}
Widget _buildCenterButton() {
return LayoutId(
id: _RadialMenuLayout.menuButton,
child: RadialMenuCenterButton(
openCloseAnimationController: _menuAnimationController.view,
activateAnimationController: _progressAnimationController.view,
isOpen: _isOpen,
onPressed: _isOpen ? _closeMenu : _openMenu,
),
);
}
@override
Widget build(BuildContext context) {
final List<Widget> children = <Widget>[];
for (int i = 0; i < widget.items.length; i++) {
if (_activeItemIndex != i) {
children.add(_buildActionButton(i));
}
}
children.add(_buildCenterButton());
return AnimatedBuilder(
animation: _menuAnimationController,
builder: (BuildContext context, Widget? child) {
return CustomMultiChildLayout(
delegate: _RadialMenuLayout(
itemCount: widget.items.length,
radius: widget.radius,
calculateItemAngle: calculateItemAngle,
controller: _menuAnimationController.view,
),
children: children,
);
},
);
}
}
class _RadialMenuLayout extends MultiChildLayoutDelegate {
static const String menuButton = 'menuButton';
static const String actionButton = 'actionButton';
static const String activeAction = 'activeAction';
final int itemCount;
final double radius;
final ItemAngleCalculator calculateItemAngle;
final Animation<double> controller;
final Animation<double> _progress;
_RadialMenuLayout({
required this.itemCount,
required this.radius,
required this.calculateItemAngle,
required this.controller,
}) : _progress = Tween<double>(begin: 0.0, end: radius).animate(
CurvedAnimation(
curve: Curves.elasticOut,
parent: controller,
),
);
late Offset center;
@override
void performLayout(Size size) {
center = Offset(size.width / 2, size.height / 2);
if (hasChild(menuButton)) {
Size menuButtonSize;
menuButtonSize = layoutChild(menuButton, BoxConstraints.loose(size));
// place the menubutton in the center
positionChild(
menuButton,
Offset(
center.dx - menuButtonSize.width / 2,
center.dy - menuButtonSize.height / 2,
),
);
}
for (int i = 0; i < itemCount; i++) {
final String actionButtonId = '$actionButton$i';
final String actionArcId = '$activeAction$i';
if (hasChild(actionArcId)) {
final Size arcSize = layoutChild(
actionArcId,
BoxConstraints.expand(
width: _progress.value * 2,
height: _progress.value * 2,
),
);
positionChild(
actionArcId,
Offset(
center.dx - arcSize.width / 2,
center.dy - arcSize.height / 2,
),
);
}
if (hasChild(actionButtonId)) {
final Size buttonSize =
layoutChild(actionButtonId, BoxConstraints.loose(size));
final double itemAngle = calculateItemAngle(i);
positionChild(
actionButtonId,
Offset(
(center.dx - buttonSize.width / 2) +
(_progress.value) * math.cos(itemAngle),
(center.dy - buttonSize.height / 2) +
(_progress.value) * math.sin(itemAngle),
),
);
}
}
}
@override
bool shouldRelayout(_RadialMenuLayout oldDelegate) =>
itemCount != oldDelegate.itemCount ||
radius != oldDelegate.radius ||
calculateItemAngle != oldDelegate.calculateItemAngle ||
controller != oldDelegate.controller ||
_progress != oldDelegate._progress;
}