cupertino context menu

This commit is contained in:
quantulr
2023-05-18 22:40:36 +08:00
parent bfaff7f7fa
commit 96818db2fc

View File

@ -5,15 +5,11 @@ import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:momo/lib/context_menu_region.dart';
import 'package:momo/models/image_list_resp.dart';
import 'package:momo/models/image_resp.dart';
import 'package:momo/provider/rerender.dart';
import 'package:momo/request/http_client.dart';
/// A builder that includes an Offset to draw the context menu at.
// typedef ContextMenuBuilder = Widget Function(
// BuildContext context, Offset offset);
import 'package:url_launcher/url_launcher.dart';
class Gallery extends ConsumerWidget {
const Gallery({Key? key}) : super(key: key);
@ -93,141 +89,70 @@ class ImageItem extends ConsumerStatefulWidget {
}
class _ImageItemState extends ConsumerState<ImageItem> {
// final ContextMenuController _contextMenuController = ContextMenuController();
@override
void dispose() {
// TODO: implement dispose
// if (_contextMenuController.isShown) {
// _contextMenuController.remove();
// }
super.dispose();
Future<void> _launchInBrowser(Uri url) async {
if (!await launchUrl(
url,
mode: LaunchMode.externalApplication,
)) {
throw Exception('Could not launch $url');
}
}
// void _showContextMenu(Offset position) {
// _contextMenuController.show(
// context: context,
// contextMenuBuilder: (BuildContext context) {
// return AdaptiveTextSelectionToolbar.buttonItems(
// anchors: TextSelectionToolbarAnchors(
// primaryAnchor: position,
// ),
// buttonItems: <ContextMenuButtonItem>[
// ContextMenuButtonItem(
// onPressed: () {
// ContextMenuController.removeAny();
// showDialog(
// context: context,
// builder: (BuildContext context) =>
// AlertDialog(
// title: const Text('删除图片'),
// content: const Text("确认删除图片"),
// actions: [
// TextButton(
// onPressed: () => Navigator.pop(context, 'Cancel'),
// child: const Text('Cancel'),
// ),
// TextButton(
// onPressed: () {
// Navigator.pop(context, 'OK');
// dio
// .delete("/image/delete/${widget.image.id}")
// .then((resp) {
// ref
// .read(uniqueIdProvider.notifier)
// .updateId();
// // context.go("/");
// });
// },
// child: const Text('OK'),
// ),
// ],
// ));
// // _showDialog(context);
// },
// label: '删除',
// ),
// ContextMenuButtonItem(
// onPressed: () {
// ContextMenuController.removeAny();
// // _showDialog(context);
// Clipboard.setData(ClipboardData(
// text:
// "${dio.options.baseUrl}/image/${widget.image.file_path}"));
// ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
// content: Row(
// crossAxisAlignment: CrossAxisAlignment.center,
// children: [
// Icon(
// Icons.check_circle,
// color: Colors.white,
// ),
// SizedBox(
// width: 10,
// ),
// Text("已拷贝到剪贴板")
// ],
// ),
// backgroundColor: Colors.lightGreen,
// ));
// },
// label: '复制链接',
// ),
// ContextMenuButtonItem(
// onPressed: () {
// ContextMenuController.removeAny();
// // _showDialog(context);
// },
// label: '查看详情',
// ),
// ],
// );
// },
// );
// }
@override
Widget build(BuildContext context) {
return ContextMenuRegion(
contextMenuBuilder: (BuildContext context, Offset offset) {
return AdaptiveTextSelectionToolbar.buttonItems(
anchors: TextSelectionToolbarAnchors(
primaryAnchor: offset,
),
buttonItems: <ContextMenuButtonItem>[
ContextMenuButtonItem(
onPressed: () {
ContextMenuController.removeAny();
Clipboard.setData(ClipboardData(
text:
"https://raichi.hodokencho.com/api/image/${widget.image.file_path}"));
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.check_circle,
color: Colors.white,
),
SizedBox(
width: 10,
),
Text("已拷贝到剪贴板")
],
),
backgroundColor: Colors.lightGreen,
));
},
label: '复制链接',
return CupertinoContextMenu.builder(
enableHapticFeedback: true,
actions: [
CupertinoContextMenuAction(
trailingIcon: CupertinoIcons.delete,
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
Clipboard.setData(ClipboardData(
text:
"https://raichi.hodokencho.com/api/image/${widget.image.file_path}"));
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.check_circle,
color: Colors.white,
),
SizedBox(
width: 10,
),
Text("已拷贝到剪贴板")
],
),
],
);
},
child: GestureDetector(
backgroundColor: Colors.lightGreen,
));
},
child: const Text('复制链接'),
),
CupertinoContextMenuAction(
trailingIcon: CupertinoIcons.search,
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
final imageUrl =
"https://raichi.hodokencho.com/api/image/${widget.image.file_path}";
final launchUrl =
"https://saucenao.com/search.php?url=${Uri.encodeComponent(imageUrl)}";
_launchInBrowser(Uri.parse(launchUrl));
},
child: const Text('在SauceNAO中搜索'),
),
],
builder: (BuildContext context, Animation<double> animation) {
return GestureDetector(
onTap: () {
context.go(Uri(
path: "/detail",
queryParameters: {"id": "${widget.image.id}"}).toString());
Navigator.of(
context,
).maybePop();
Future.delayed(const Duration(milliseconds: 120)).then((value) {
context.go(Uri(
path: "/detail",
queryParameters: {"id": "${widget.image.id}"}).toString());
});
},
child: Image.network(
kIsWeb
@ -238,6 +163,8 @@ class _ImageItemState extends ConsumerState<ImageItem> {
return const Icon(Icons.error_outline);
},
),
));
);
},
);
}
}