yubioath-flutter/lib/widgets/file_drop_target.dart

82 lines
2.1 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
* Copyright (C) 2022 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2024-01-10 18:06:12 +03:00
import 'dart:io';
2022-03-23 12:46:35 +03:00
import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter/material.dart';
2023-10-19 09:20:18 +03:00
import '../core/state.dart';
2022-03-23 12:46:35 +03:00
class FileDropTarget extends StatefulWidget {
final Widget child;
2024-01-10 18:06:12 +03:00
final Function(File file) onFileDropped;
final Widget overlay;
2022-03-23 12:46:35 +03:00
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,
required 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;
@override
2023-12-22 18:46:29 +03:00
Widget build(BuildContext context) {
return DropTarget(
onDragEntered: (_) {
// Multiple FileDropTarget widgets can be in the tree at the same
// time. We only want to use the top-most.
2023-12-22 18:46:29 +03:00
if (ModalRoute.of(context)!.isCurrent) {
2022-03-23 12:46:35 +03:00
setState(() {
_hovering = true;
});
2023-12-22 18:46:29 +03:00
}
},
onDragExited: (_) {
setState(() {
_hovering = false;
});
},
onDragDone: (details) async {
if (ModalRoute.of(context)!.isCurrent) {
2022-03-23 12:46:35 +03:00
for (final file in details.files) {
2024-01-10 18:06:12 +03:00
widget.onFileDropped(File(file.path));
2022-03-23 12:46:35 +03:00
}
2023-12-22 18:46:29 +03:00
}
},
enable: !isAndroid,
child: Stack(
2024-01-09 14:50:26 +03:00
fit: StackFit.expand,
2023-12-22 18:46:29 +03:00
children: [
widget.child,
if (_hovering)
2024-01-09 14:50:26 +03:00
Padding(
padding: const EdgeInsets.all(8.0),
child: widget.overlay,
),
2023-12-22 18:46:29 +03:00
],
),
);
}
2022-03-23 12:46:35 +03:00
}