import "package:cross_file/cross_file.dart"; import "package:desktop_drop/desktop_drop.dart" as dd; import "package:flutter/widgets.dart"; import "theme/garage_theme.dart"; export "package:cross_file/cross_file.dart" show XFile; /// Somewhere files can be dragged onto from outside the app. /// /// Flutter has nothing for this, so its desktop_drop underneath - macOS, /// Windows, Linux and the web. What this adds is the Garage side of it: while /// something is being held over [child] it gets the focus ring, the same one /// a focused field gets, because "this is where that will land" is the same /// statement. /// /// Folders arent walked. A dropped folder comes through [onDropFolders] as /// its path and nothing else, so the caller decides whether thats an upload /// of everything in it or a polite no. class FileDropTarget extends StatefulWidget { const FileDropTarget({ super.key, required this.child, required this.onDrop, this.onDropFolders, this.enabled = true, }); final Widget child; final ValueChanged> onDrop; final ValueChanged>? onDropFolders; /// Off, and drags pass over it as if it wasnt there. final bool enabled; @override State createState() => _FileDropTargetState(); } class _FileDropTargetState extends State { bool _over = false; void _done(dd.DropDoneDetails details) { setState(() => _over = false); final files = []; final folders = []; for (final item in details.files) { if (item is dd.DropItemDirectory) { folders.add(item.path); } else { files.add(item); } } if (files.isNotEmpty) widget.onDrop(files); if (folders.isNotEmpty) widget.onDropFolders?.call(folders); } @override Widget build(BuildContext context) { final theme = GarageTheme.of(context); return dd.DropTarget( enable: widget.enabled, onDragEntered: (_) => setState(() => _over = true), onDragExited: (_) => setState(() => _over = false), onDragDone: _done, // foreground, so the ring is drawn over the child and takes no room. // As a border it would shove everything in by its own width the moment // a drag came in child: DecoratedBox( position: DecorationPosition.foreground, decoration: BoxDecoration( borderRadius: BorderRadius.circular(theme.radiusSm), border: _over ? Border.all( color: theme.colorScheme.ring, width: theme.density.controlBorderWidth * 2, ) : null, ), child: widget.child, ), ); } }