import 'dart:async'; import 'package:flutter/material.dart'; /// Open, Copy and the file actions float over the top of the content: shown /// when the page opens and whenever the content is touched, hidden again after /// a few seconds without use, so nothing needs scrolling to reach them. class ArtifactControlsOverlay extends StatefulWidget { const ArtifactControlsOverlay({ super.key, required this.controls, required this.child, this.hideAfter = const Duration(seconds: 3), }); final List controls; final Widget child; final Duration hideAfter; @override State createState() => _ArtifactControlsOverlayState(); } class _ArtifactControlsOverlayState extends State { bool _shown = true; Timer? _hide; @override void initState() { super.initState(); _schedule(); } @override void dispose() { _hide?.cancel(); super.dispose(); } void _schedule() { _hide?.cancel(); _hide = Timer(widget.hideAfter, () { if (mounted) setState(() => _shown = false); }); } void _show() { if (!_shown) setState(() => _shown = true); _schedule(); } @override Widget build(BuildContext context) { final surface = Theme.of( context, ).colorScheme.surfaceContainerHigh.withValues(alpha: 0.94); return Stack( children: [ // Any touch on the content brings the controls back. Positioned.fill( child: Listener( behavior: HitTestBehavior.translucent, onPointerDown: (_) => _show(), child: widget.child, ), ), Positioned( left: 8, right: 8, top: 8, child: IgnorePointer( ignoring: !_shown, child: AnimatedOpacity( key: const Key('artifact-viewer-controls'), opacity: _shown ? 1 : 0, duration: const Duration(milliseconds: 200), child: Listener( // Using a control keeps the bar up. onPointerDown: (_) => _schedule(), child: DecoratedBox( decoration: BoxDecoration( color: surface, borderRadius: BorderRadius.circular(12), boxShadow: const [ BoxShadow(color: Color(0x22000000), blurRadius: 6), ], ), child: Padding( padding: const EdgeInsets.symmetric( horizontal: 4, vertical: 2, ), child: Wrap( alignment: WrapAlignment.center, crossAxisAlignment: WrapCrossAlignment.center, children: widget.controls, ), ), ), ), ), ), ), ], ); } }