Files
pipelines/app/lib/features/chat/files/files_drop_area.dart
T
alan b8fb4d350c Upload files into the shown folder from the chat Files panel
The header gains an Upload button that uses the host file picker (disabled
with its reason where no picker exists), and the panel body becomes a drop
target: on web, document drag events are observed through a dart:js_interop
seam and a file drag over the panel shows a highlight naming the folder.
Each file is posted to POST /files?path=<folder> through an injectable
transport; refusals (exists, sign in, too large) stay visible until dismissed
and the folder reloads afterwards. The list/grid builders move to
files_entries.dart to keep the panel small.
2026-11-09 00:09:01 +00:00

112 lines
3.2 KiB
Dart

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<List<ChatPickedFile>> onFiles;
final ValueChanged<Object> onError;
final Widget child;
@override
State<FilesDropArea> createState() => _FilesDropAreaState();
}
class _FilesDropAreaState extends State<FilesDropArea> {
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<ChatPickedFile> 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,
),
),
),
),
),
),
),
],
);
}
}