mirror of
https://github.com/lifegpc/eh_downloader_flutter.git
synced 2026-06-14 09:54:17 +08:00
Add support to copy image url to clipboard
This commit is contained in:
@@ -1,25 +1,43 @@
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:super_clipboard/super_clipboard.dart';
|
||||
import 'package:super_context_menu/super_context_menu.dart';
|
||||
|
||||
final _log = Logger("ImageWithContextMenu");
|
||||
|
||||
class ImageWithContextMenu extends StatelessWidget {
|
||||
const ImageWithContextMenu(this.data, {Key? key}) : super(key: key);
|
||||
const ImageWithContextMenu(this.data, {Key? key, this.uri}) : super(key: key);
|
||||
final Uint8List data;
|
||||
final String? uri;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ContextMenuWidget(
|
||||
menuProvider: (_) {
|
||||
return Menu(children: [
|
||||
var list = [
|
||||
MenuAction(
|
||||
title: AppLocalizations.of(context)!.copyImage,
|
||||
callback: () {
|
||||
final item = DataWriterItem();
|
||||
item.add(Formats.jpeg(data));
|
||||
ClipboardWriter.instance.write([item]);
|
||||
}),
|
||||
]);
|
||||
ClipboardWriter.instance.write([item]).catchError((err) {
|
||||
_log.warning("Failed to copy image to clipboard:", err);
|
||||
});
|
||||
})
|
||||
];
|
||||
if (uri != null) {
|
||||
list.add(MenuAction(
|
||||
title: AppLocalizations.of(context)!.copyImgUrl,
|
||||
callback: () {
|
||||
final item = DataWriterItem();
|
||||
item.add(Formats.plainText(uri!));
|
||||
ClipboardWriter.instance.write([item]).catchError((err) {
|
||||
_log.warning("Failed to copy image to clipboard:", err);
|
||||
});
|
||||
}));
|
||||
}
|
||||
return Menu(children: list);
|
||||
},
|
||||
child: Image.memory(data));
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ class _Thumbnail extends State<Thumbnail> {
|
||||
Object? _error;
|
||||
int? _fileId;
|
||||
bool _showNsfw = false;
|
||||
String? _uri;
|
||||
Future<void> _fetchData() async {
|
||||
try {
|
||||
_isLoading = true;
|
||||
@@ -63,6 +64,7 @@ class _Thumbnail extends State<Thumbnail> {
|
||||
throw Exception(
|
||||
'Failed to get thumbnail: ${re.response.statusCode} ${re.response.statusMessage}');
|
||||
}
|
||||
_uri = re.response.realUri.toString();
|
||||
final data = Uint8List.fromList(re.data);
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
@@ -84,6 +86,7 @@ class _Thumbnail extends State<Thumbnail> {
|
||||
_error = null;
|
||||
_fileId = widget._fileId;
|
||||
_showNsfw = false;
|
||||
_uri = null;
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@@ -111,7 +114,8 @@ class _Thumbnail extends State<Thumbnail> {
|
||||
sigmaX: 10,
|
||||
sigmaY: 10,
|
||||
tileMode: TileMode.decal),
|
||||
child: ImageWithContextMenu(_data!))),
|
||||
child:
|
||||
ImageWithContextMenu(_data!, uri: _uri))),
|
||||
SizedBox(
|
||||
width: widget.width.toDouble(),
|
||||
height: widget.height.toDouble(),
|
||||
@@ -127,7 +131,7 @@ class _Thumbnail extends State<Thumbnail> {
|
||||
))
|
||||
],
|
||||
)
|
||||
: ImageWithContextMenu(_data!)
|
||||
: ImageWithContextMenu(_data!, uri: _uri)
|
||||
: Text("Error $_error"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,5 +27,6 @@
|
||||
"read": "Read",
|
||||
"download": "Download",
|
||||
"colon": ":",
|
||||
"copyImage": "Copy image to clipboard"
|
||||
"copyImage": "Copy image to clipboard",
|
||||
"copyImgUrl": "Copy image URL to clipboard"
|
||||
}
|
||||
|
||||
@@ -27,5 +27,6 @@
|
||||
"read": "阅读",
|
||||
"download": "下载",
|
||||
"colon": ":",
|
||||
"copyImage": "复制图片到剪贴板"
|
||||
"copyImage": "复制图片到剪贴板",
|
||||
"copyImgUrl": "复制图片链接到剪贴板"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user