owlnext_dropzone 1.2.1+1
owlnext_dropzone: ^1.2.1+1 copied to clipboard
Olwnext dropzone widget
owlnext-dropzone #
Owlnext dropzone wrapper
FileUtils.downloadFile() -> Breaking change on web #
if you want to use FileUtils.downloadFile() on web context, you will need to inject a specific behavior from your app. create a file in your app, loaded only in library.html context (web) and call
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:universal_html/html.dart';
import 'package:owlnext_dropzone/owlnext_dropzone.dart';
/// Utility to enable file downloads on Flutter Web when using `owlnext_dropzone`.
///
/// # Why
/// By default, `owlnext_dropzone` cannot handle web downloads without
/// depending on `universal_html`. This class injects a web-only implementation
/// so your app controls the download behavior.
///
/// # How
/// - Call [InitWebDownload.setup] in `main.dart`.
/// - On Web, it registers a custom download function that:
/// 1. Encodes file bytes to Base64
/// 2. Creates a temporary `<a>` element with a `data:` URI
/// 3. Triggers the browser’s native download
/// 4. Cleans up the element
///
/// # Example
/// ```dart
/// void main() {
/// InitWebDownload.setup();
/// runApp(MyApp());
/// }
/// ```
class InitWebDownload {
static void setup() {
if (kIsWeb) {
DownloadFileWebBypass().setDownloadFileWebFunction(_downloadFileWebImplementation);
}
}
Future<void> _downloadFileWebImplementation(Uint8List bytes, String fileName, String extension) async {
final base64 = base64Encode(bytes);
final anchor = AnchorElement(href: 'data:application/octet-stream;base64,$base64')
..target = 'blank'
..download = fileName;
document.body?.append(anchor);
anchor.click();
anchor.remove();
}
}
Pasting from clipboard #
to continue to be able to paste file from clipboard on dropzone (except in web context), add this in your main.dart
InitPasteFromClipboardInjection.setup();
Here is an implementation of the injection using super_clipboard: ^0.9.1
import 'package:flutter/foundation.dart';
import 'package:owlnext_dropzone/owlnext_dropzone.dart';
import 'package:super_clipboard/super_clipboard.dart';
class InitPasteFromClipboardInjection {
static void setup() {
ClipboardFilesBypass().setGetClipboardFunction(_getClipboardFilesFromSuperClipboard);
}
static Future<List<({Uint8List bytes, String extension})>> _getClipboardFilesFromSuperClipboard() async {
try {
final clipboard = SystemClipboard.instance!;
if (kDebugMode) print("reading clipboard");
final reader = await clipboard.read();
final results = <({Uint8List bytes, String extension})>[];
// Formats pris en charge
final formats = <SimpleFileFormat, String>{
Formats.png: 'png',
Formats.jpeg: 'jpeg',
Formats.zip: 'zip',
Formats.pdf: 'pdf',
Formats.doc: 'doc',
Formats.docx: 'docx',
Formats.xlsx: 'xlsx',
Formats.xls: 'xls',
Formats.mp4: 'mp4',
Formats.mov: 'mov',
};
final futures = <Future<void>>[];
for (final entry in formats.entries) {
final future = Future<void>(() {
final completer = Completer<void>();
// Lance la lecture
reader.getFile(entry.key, (DataReaderFile file) async {
if (kDebugMode) print("Got a file of type ${entry.key}");
final bytes = await file.readAll();
results.add((bytes: bytes, extension: entry.value));
if (kDebugMode) print("File added to results (${results.length})");
completer.complete();
});
// Timeout court pour gérer le cas où aucun fichier n’est trouvé
Future.delayed(const Duration(milliseconds: 50), () {
if (!completer.isCompleted) completer.complete();
});
return completer.future;
});
futures.add(future);
}
await Future.wait(futures);
if (kDebugMode) print("returning ${results.length} results");
return results;
} catch (e, s) {
if (kDebugMode) {
print("Error _getClipboardFilesFromSuperClipboard: $e");
print("Trace _getClipboardFilesFromSuperClipboard: $s");
}
rethrow;
}
}
}
Debugging #
to see debug messages (in debugModeOnly), put this in your app main.dart
OwlnextDropzoneDebuger.kDebugOwlnextDropzone = true;