100 lines
3.0 KiB
Dart
100 lines
3.0 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
/// A layer of floating windows painted in the app's root overlay, above the
|
|
/// page, while it keeps its place in the tree (its inherited widgets, focus
|
|
/// and state stay where it is built). Every [FloatingLayer] shares that one
|
|
/// overlay, so windows from different features (the terminal, a floating
|
|
/// chat) stack by use: the layer last pressed, focused or opened is on top.
|
|
///
|
|
/// [builder] returns the windows as [Positioned] children of a [Stack] the
|
|
/// size of [bounds]: this widget's own box, or the whole overlay when
|
|
/// [coverOverlay] is set. Space between windows takes no input. Without an
|
|
/// overlay (a bare widget test) the layer paints in place.
|
|
class FloatingLayer extends StatefulWidget {
|
|
const FloatingLayer({
|
|
super.key,
|
|
required this.builder,
|
|
this.open = true,
|
|
this.coverOverlay = false,
|
|
});
|
|
|
|
final List<Widget> Function(BuildContext context, Size bounds) builder;
|
|
|
|
/// Turning true brings the layer to the top, as its window opens.
|
|
final bool open;
|
|
|
|
final bool coverOverlay;
|
|
|
|
@override
|
|
State<FloatingLayer> createState() => _FloatingLayerState();
|
|
}
|
|
|
|
class _FloatingLayerState extends State<FloatingLayer> {
|
|
final OverlayPortalController _portal = OverlayPortalController(
|
|
debugLabel: 'floating layer',
|
|
);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_portal.show();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant FloatingLayer oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (widget.open && !oldWidget.open) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => _raise());
|
|
}
|
|
}
|
|
|
|
void _raise() {
|
|
if (mounted) _portal.show();
|
|
}
|
|
|
|
Widget _layer(BuildContext context, Size bounds) => Listener(
|
|
behavior: HitTestBehavior.deferToChild,
|
|
onPointerDown: (_) => _raise(),
|
|
child: Focus(
|
|
canRequestFocus: false,
|
|
skipTraversal: true,
|
|
onFocusChange: (focused) {
|
|
if (focused) _raise();
|
|
},
|
|
child: SizedBox.fromSize(
|
|
size: bounds,
|
|
child: Stack(
|
|
clipBehavior: Clip.none,
|
|
children: widget.builder(context, bounds),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (Overlay.maybeOf(context, rootOverlay: true) == null) {
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) => _layer(context, constraints.biggest),
|
|
);
|
|
}
|
|
return OverlayPortal.overlayChildLayoutBuilder(
|
|
controller: _portal,
|
|
overlayLocation: OverlayChildLocation.rootOverlay,
|
|
overlayChildBuilder: (context, info) => widget.coverOverlay
|
|
? Positioned.fill(child: _layer(context, info.overlaySize))
|
|
: Positioned(
|
|
left: 0,
|
|
top: 0,
|
|
width: info.childSize.width,
|
|
height: info.childSize.height,
|
|
child: Transform(
|
|
transform: info.childPaintTransform,
|
|
child: _layer(context, info.childSize),
|
|
),
|
|
),
|
|
child: const SizedBox.expand(),
|
|
);
|
|
}
|
|
}
|