-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popover.dart
449 lines (395 loc) · 11.9 KB
/
popover.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import 'dart:async';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
class PopoverController {
Future<void> showPopover(Widget popover) async {
await __state?.showPopover(popover);
}
void hidePopover() {
__state?.hidePopover();
}
set _state(_PopoverAnchorState? state) {
if (__state != null && state != null && __state != state) {
throw StateError('PopoverController is already attached to a state.');
}
__state = state;
}
_PopoverAnchorState? get _state => __state;
_PopoverAnchorState? __state;
}
abstract class PopoverDelegate {
/// Computes the constraints for the popover.
///
/// - [bounds] is the entire area available for the popover.
/// - [safeAreaInsets] is the area of bounds that is covered by system UI elements
/// - [anchor] is the rectangle that the popover should be anchored to
/// represented in same coordinate space as [bounds].
BoxConstraints computeConstraints(
Rect bounds,
EdgeInsets safeAreaInsets,
Rect anchor,
);
/// Computes the position for the popover.
///
/// - [bounds] is the entire area available for the popover.
/// - [safeAreaInsets] is the area of bounds that is covered by system UI elements
/// - [anchor] is the rectangle that the popover should be anchored to.
/// - [popoverSize] is the size of the popover.
Offset computePosition(
Rect bounds,
EdgeInsets safeAreaInsets,
Rect anchor,
Size popoverSize,
);
/// Builds the scaffold widget containing the popover. The scaffold is responsible
/// for animating the popover in and out, painting popover shadow and clipping
/// the child.
///
/// Once the popover animation is complete the method will be called again with
/// animation status set to [AnimationStatus.completed] in order for delegate
/// to be able to remove unnecessary widgets from the tree.
///
/// When showing popover this method will be called before [computePosition] and
/// then also the next frame.
Widget buildScaffold(
BuildContext context,
Widget child,
Animation<double> animation,
);
Widget buildVeil(
BuildContext context,
Animation<double> animation,
VoidCallback dismissPopover,
);
void setTickerProvider(TickerProvider tickerProvider) {}
/// Called when the popover has been hidden and this delegate is no longer needed.
void dispose() {}
}
class PopoverAnchor extends StatefulWidget {
const PopoverAnchor({
super.key,
required this.controller,
required this.child,
required this.delegate,
this.animationDuration = Duration.zero,
this.animationReverseDuration,
});
final PopoverController controller;
final Widget child;
final PopoverDelegate Function() delegate;
final Duration animationDuration;
final Duration? animationReverseDuration;
@override
State<StatefulWidget> createState() => _PopoverAnchorState();
}
class _SizeMonitor extends SingleChildRenderObjectWidget {
const _SizeMonitor({
required Widget child,
required this.onSizeChanged,
}) : super(child: child);
final ValueChanged<Size> onSizeChanged;
@override
RenderObject createRenderObject(BuildContext context) {
return _SizeMonitorRenderBox(onSizeChanged);
}
@override
void updateRenderObject(
BuildContext context,
covariant RenderObject renderObject,
) {
(renderObject as _SizeMonitorRenderBox).onSizeChanged = onSizeChanged;
}
}
class _SizeMonitorRenderBox extends RenderProxyBox {
_SizeMonitorRenderBox(this.onSizeChanged);
ValueChanged<Size> onSizeChanged;
@override
void performLayout() {
super.performLayout();
onSizeChanged(size);
}
}
class _PopoverAnchorState extends State<PopoverAnchor>
with TickerProviderStateMixin {
final _overlayPortalController = OverlayPortalController();
Widget _popoverChild = const SizedBox();
final _focusScopeNode = FocusScopeNode(
debugLabel: 'PopoverFocusScope',
);
Completer? _completer;
Size _anchorSize = Size.zero;
final _relayout = _SimpleNotifier();
late final AnimationController _animation;
// The layout callback on delegate has not been called yet.
bool _pendingLayout = true;
PopoverDelegate? _delegate;
@override
void initState() {
super.initState();
_focusScopeNode.addListener(() {
if (_completer != null && !_focusScopeNode.hasFocus) {
_completer!.complete();
}
});
_addPersistentFrameCallback(_onFrame);
_animation = AnimationController(
vsync: this,
duration: widget.animationDuration,
reverseDuration: widget.animationReverseDuration,
);
_animation.addStatusListener(_animationStatusChanged);
widget.controller._state = this;
}
@override
void didUpdateWidget(covariant PopoverAnchor oldWidget) {
assert(oldWidget.controller._state == this);
oldWidget.controller._state = null;
widget.controller._state = this;
super.didUpdateWidget(oldWidget);
}
@override
void dispose() {
_focusScopeNode.dispose();
_relayout.dispose();
_removePersistentFrameCallback(_onFrame);
widget.controller._state = null;
_completer?.complete();
super.dispose();
}
void _onFrame(Duration _) {
_relayout.notify();
}
void _onLayoutComplete() {
if (_pendingLayout) {
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {});
});
_pendingLayout = false;
}
}
final _childKey = GlobalKey();
Widget _buildPopover(BuildContext context) {
final child = _delegate!.buildScaffold(
context,
KeyedSubtree(key: _childKey, child: _popoverChild),
_animation,
);
if (_pendingLayout) {
return Visibility(
visible: false,
maintainSize: true,
maintainState: true,
maintainAnimation: true,
child: child,
);
}
return child;
}
Widget _buildChild(BuildContext context) {
if (_delegate == null) {
_delegate = widget.delegate();
_delegate!.setTickerProvider(this);
}
return _CustomMultiChildLayout(
delegate: _PopoverLayout(
anchor: this.context,
safeAreaInsets: MediaQuery.of(context).padding,
anchorSize: () => _anchorSize,
relayout: _relayout,
delegate: _delegate!,
onLayoutComplete: _onLayoutComplete,
),
children: [
LayoutId(
id: _Slot.veil,
child: _delegate!.buildVeil(
context,
_animation,
() => _completer?.complete(),
),
),
LayoutId(
id: _Slot.popover,
child: Actions(
actions: {
DismissIntent: CallbackAction(onInvoke: (_) {
hidePopover();
return null;
}),
},
child: FocusTraversalGroup(
policy: ReadingOrderTraversalPolicy(
requestFocusCallback: (node,
{alignment, alignmentPolicy, curve, duration}) {
// Buggy flutter will find scrollable in element tree that isn't part
// of the render tree :-/
node.requestFocus();
},
),
child: FocusScope(
node: _focusScopeNode,
child: Builder(builder: (context) {
return _buildPopover(context);
}),
),
),
),
),
],
);
}
@override
Widget build(BuildContext context) {
return OverlayPortal(
controller: _overlayPortalController,
overlayChildBuilder: _buildChild,
child: _SizeMonitor(
onSizeChanged: (size) {
_anchorSize = size;
},
child: widget.child,
),
);
}
void _animationStatusChanged(AnimationStatus status) {
if (status == AnimationStatus.dismissed) {
_overlayPortalController.hide();
_pendingLayout = true;
_delegate?.dispose();
_delegate = null;
} else {
// Force rebuild - we'll want to remove the transition from the tree.
setState(() {});
}
}
Future<void> showPopover(Widget overlay) async {
_pendingLayout = true;
_popoverChild = overlay;
_completer ??= Completer();
_overlayPortalController.show();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_focusScopeNode.requestFocus();
});
_animation.forward();
await _completer?.future;
_completer = null;
if (mounted) {
// might have been disposed in the meanwhile
_animation.reverse();
}
}
void hidePopover() {
_completer?.complete();
}
}
enum _Slot {
popover,
veil,
}
class _PopoverLayout extends MultiChildLayoutDelegate {
_PopoverLayout({
required this.anchor,
required this.safeAreaInsets,
required this.anchorSize,
required this.delegate,
required this.onLayoutComplete,
super.relayout,
});
final BuildContext anchor;
final ValueGetter<Size> anchorSize;
final EdgeInsets safeAreaInsets;
final PopoverDelegate delegate;
final VoidCallback onLayoutComplete;
@override
void performLayout(Size boundsSize) {
if (hasChild(_Slot.popover)) {
final bounds = Offset.zero & boundsSize;
final renderObject = anchor.findRenderObject() as RenderBox;
final anchorToWindow = renderObject.getTransformTo(null);
final windowToUs = _RenderCustomMultiChildLayoutBox._currentLayout!
.getTransformTo(null)
..invert();
final anchorRect = Offset.zero & anchorSize();
final transformed = MatrixUtils.transformRect(
windowToUs,
MatrixUtils.transformRect(anchorToWindow, anchorRect),
);
final constraints = delegate.computeConstraints(
bounds,
safeAreaInsets,
transformed,
);
final size = layoutChild(_Slot.popover, constraints);
final offset = delegate.computePosition(
bounds,
safeAreaInsets,
transformed,
size,
);
positionChild(_Slot.popover, offset);
onLayoutComplete();
}
if (hasChild(_Slot.veil)) {
layoutChild(_Slot.veil, BoxConstraints.loose(boundsSize));
positionChild(_Slot.veil, Offset.zero);
}
}
@override
bool shouldRelayout(covariant MultiChildLayoutDelegate oldDelegate) {
return false;
}
}
class _SimpleNotifier extends ChangeNotifier {
void notify() {
notifyListeners();
}
}
final _frameCallbacks = <FrameCallback>[];
bool _frameCallbacksScheduled = false;
void _addPersistentFrameCallback(FrameCallback callback) {
if (!_frameCallbacksScheduled) {
// Microtask otherwise FlutterTest fails with ConcurrentModificationError.
Future.microtask(() {
SchedulerBinding.instance.addPersistentFrameCallback((timestamp) {
final callbacks = List.of(_frameCallbacks);
for (final callback in callbacks) {
callback(timestamp);
}
});
});
_frameCallbacksScheduled = true;
}
_frameCallbacks.add(callback);
}
void _removePersistentFrameCallback(FrameCallback callback) {
_frameCallbacks.remove(callback);
// There is no way to unschedule persistent frame callback in Flutter.
}
class _CustomMultiChildLayout extends CustomMultiChildLayout {
const _CustomMultiChildLayout({
required super.delegate,
required super.children,
});
@override
RenderCustomMultiChildLayoutBox createRenderObject(BuildContext context) {
return _RenderCustomMultiChildLayoutBox(delegate: delegate);
}
}
class _RenderCustomMultiChildLayoutBox extends RenderCustomMultiChildLayoutBox {
_RenderCustomMultiChildLayoutBox({
required super.delegate,
});
static RenderBox? _currentLayout;
@override
void performLayout() {
_currentLayout = this;
// Relax layout check - we need to be able to get transform to root
// during layout call, which may involve querying size of unrelated
// widgets.
invokeLayoutCallback((constraints) {
super.performLayout();
});
_currentLayout = null;
}
}