import 'package:flutter/material.dart'; import '../domain/chat_attachment_picker.dart'; import 'files_drop.dart'; /// Makes [child] a drop target for host file drags: while a file drag is over /// it, a highlight names the folder the files would land in, and a drop hands /// the files to [onFiles]. Nothing is drawn when the host has no drop support /// or [folderLabel] is null (no folder is open yet). class FilesDropArea extends StatefulWidget { const FilesDropArea({ super.key, required this.drop, required this.folderLabel, required this.onFiles, required this.onError, required this.child, }); final FilesDrop drop; /// The shown folder's label, or null while there is no folder to drop into. final String? folderLabel; final ValueChanged> onFiles; final ValueChanged onError; final Widget child; @override State createState() => _FilesDropAreaState(); } class _FilesDropAreaState extends State { FilesDropUnsubscribe? _unsubscribe; bool _hovering = false; @override void initState() { super.initState(); if (widget.drop.isSupported) { _unsubscribe = widget.drop.listen( FilesDropListener( accepts: _accepts, onHover: (_) => _setHovering(true), onLeave: () => _setHovering(false), onDrop: _dropped, onError: (error) { if (mounted) widget.onError(error); }, ), ); } } @override void dispose() { _unsubscribe?.call(); super.dispose(); } bool _accepts(Offset position) { if (!mounted || widget.folderLabel == null) return false; final box = context.findRenderObject(); if (box is! RenderBox || !box.hasSize) return false; return box.paintBounds.contains(box.globalToLocal(position)); } void _setHovering(bool value) { if (!mounted || _hovering == value) return; setState(() => _hovering = value); } void _dropped(List files) { _setHovering(false); if (mounted && files.isNotEmpty) widget.onFiles(files); } @override Widget build(BuildContext context) { final scheme = Theme.of(context).colorScheme; return Stack( fit: StackFit.passthrough, children: [ widget.child, if (_hovering && widget.folderLabel != null) Positioned.fill( child: IgnorePointer( child: Semantics( label: 'Drop to upload to ${widget.folderLabel}', child: DecoratedBox( decoration: BoxDecoration( color: scheme.primaryContainer.withValues(alpha: .6), border: Border.all(color: scheme.primary, width: 2), ), child: Center( child: Padding( padding: const EdgeInsets.all(24), child: Text( 'Drop to upload to ${widget.folderLabel}', textAlign: TextAlign.center, style: Theme.of(context).textTheme.titleMedium, ), ), ), ), ), ), ), ], ); } }