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