yubioath-flutter/lib/widgets/file_drop_target.dart

60 lines
1.4 KiB
Dart
Raw Normal View History

2022-03-23 12:46:35 +03:00
import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter/material.dart';
class FileDropTarget extends StatefulWidget {
final Widget child;
final Function(List<int> filedata) onFileDropped;
final Widget? overlay;
const FileDropTarget({
2022-05-12 10:56:55 +03:00
super.key,
2022-03-23 12:46:35 +03:00
required this.child,
required this.onFileDropped,
this.overlay,
2022-05-12 10:56:55 +03:00
});
2022-03-23 12:46:35 +03:00
@override
State<StatefulWidget> createState() => _FileDropTargetState();
}
class _FileDropTargetState extends State<FileDropTarget> {
bool _hovering = false;
Widget _buildDefaultOverlay() => Positioned.fill(
child: Container(
color: Colors.blue.withOpacity(0.4),
child: Icon(
Icons.upload_file,
size: 200,
color: Colors.black.withOpacity(0.6),
),
),
);
@override
Widget build(BuildContext context) => DropTarget(
onDragEntered: (_) {
setState(() {
_hovering = true;
});
},
onDragExited: (_) {
setState(() {
_hovering = false;
});
},
onDragDone: (details) async {
for (final file in details.files) {
widget.onFileDropped(await file.readAsBytes());
}
},
child: Stack(
alignment: Alignment.center,
children: [
widget.child,
if (_hovering) widget.overlay ?? _buildDefaultOverlay(),
],
),
);
}