import 'package:flutter/material.dart'; import '../data/directory_listing.dart'; import 'files_icons.dart'; import 'files_models.dart'; /// Moves [entry] into another listed folder by dragging it there: a press and /// hold starts the drag (so a touch scroll stays a scroll), the folder row /// under the pointer highlights, and releasing hands the pair to [onDrop]. /// /// Every entry is a drag source; a folder is also a target, unless the dragged /// entry is itself or one of its ancestors ([canDropEntryOn]). The server /// still refuses a same-named target, which the panel's notice then shows. class FilesDraggableEntry extends StatelessWidget { const FilesDraggableEntry({ super.key, required this.entry, required this.onDrop, required this.builder, }); final DirectoryEntry entry; final void Function(DirectoryEntry dragged, DirectoryEntry folder) onDrop; /// Builds the row or tile; [targeted] is true while an acceptable entry is /// held over this folder. final Widget Function(BuildContext context, bool targeted) builder; @override Widget build(BuildContext context) { if (!entry.isDir) return _source(context, false); return DragTarget( onWillAcceptWithDetails: (details) => canDropEntryOn(details.data, entry), onAcceptWithDetails: (details) => onDrop(details.data, entry), builder: (context, candidates, _) => _source(context, candidates.isNotEmpty), ); } Widget _source(BuildContext context, bool targeted) => LongPressDraggable( data: entry, feedback: _DragFeedback(entry: entry), childWhenDragging: Opacity( opacity: .4, child: builder(context, targeted), ), child: builder(context, targeted), ); } /// What travels under the pointer: the entry's icon and name on a raised /// surface, so it reads as the row being carried. class _DragFeedback extends StatelessWidget { const _DragFeedback({required this.entry}); final DirectoryEntry entry; @override Widget build(BuildContext context) => Material( elevation: 4, borderRadius: BorderRadius.circular(8), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), child: Row( mainAxisSize: MainAxisSize.min, children: [ Icon(fileIcon(entry)), const SizedBox(width: 8), Text(entry.name), ], ), ), ); }