2023-05-14 19:59:43 +08:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
2023-02-07 17:28:01 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2023-02-19 23:10:09 +08:00
|
|
|
import 'package:flutter/services.dart';
|
2023-02-07 17:28:01 +08:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
import 'package:go_router/go_router.dart';
|
2023-05-14 19:59:43 +08:00
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
2023-05-17 00:04:36 +08:00
|
|
|
import 'package:momo/lib/context_menu_region.dart';
|
2023-02-08 17:20:16 +08:00
|
|
|
import 'package:momo/models/image_list_resp.dart';
|
|
|
|
|
import 'package:momo/models/image_resp.dart';
|
2023-02-11 22:29:33 +08:00
|
|
|
import 'package:momo/provider/rerender.dart';
|
2023-02-08 23:48:25 +08:00
|
|
|
import 'package:momo/request/http_client.dart';
|
2023-02-08 17:20:16 +08:00
|
|
|
|
2023-02-19 23:10:09 +08:00
|
|
|
/// A builder that includes an Offset to draw the context menu at.
|
2023-05-17 00:04:36 +08:00
|
|
|
// typedef ContextMenuBuilder = Widget Function(
|
|
|
|
|
// BuildContext context, Offset offset);
|
2023-02-19 23:10:09 +08:00
|
|
|
|
2023-02-11 22:29:33 +08:00
|
|
|
class Gallery extends ConsumerWidget {
|
2023-02-07 17:28:01 +08:00
|
|
|
const Gallery({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
|
|
@override
|
2023-02-11 22:29:33 +08:00
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
|
UniqueKey key = ref.watch(uniqueIdProvider);
|
2023-02-12 23:39:41 +08:00
|
|
|
return Container(
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
child: ImageGrid(
|
|
|
|
|
key: key,
|
|
|
|
|
),
|
2023-02-11 22:29:33 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ImageGrid extends StatefulWidget {
|
|
|
|
|
const ImageGrid({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<ImageGrid> createState() => _ImageGridState();
|
2023-02-07 17:28:01 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:29:33 +08:00
|
|
|
class _ImageGridState extends State<ImageGrid> {
|
2023-02-09 17:07:14 +08:00
|
|
|
int pageSize = 10;
|
|
|
|
|
|
2023-05-14 19:59:43 +08:00
|
|
|
final PagingController<int, ImageResp> _pagingController =
|
|
|
|
|
PagingController(firstPageKey: 1);
|
|
|
|
|
|
|
|
|
|
Future<void> _fetchPage(int pageKey) async {
|
|
|
|
|
try {
|
|
|
|
|
final resp = await dio.get("/image/history",
|
|
|
|
|
queryParameters: {"page": pageKey, "size": pageSize});
|
|
|
|
|
ImageListResp imageListResp = ImageListResp.fromJson(resp.data);
|
|
|
|
|
final newItems = imageListResp.list;
|
|
|
|
|
final isLastPage = !imageListResp.hasNext;
|
|
|
|
|
if (isLastPage) {
|
|
|
|
|
_pagingController.appendLastPage(newItems);
|
|
|
|
|
} else {
|
|
|
|
|
final nextPageKey = pageKey + 1;
|
|
|
|
|
_pagingController.appendPage(newItems, nextPageKey);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
_pagingController.error = error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 23:48:25 +08:00
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
// TODO: implement initState
|
2023-05-14 19:59:43 +08:00
|
|
|
_pagingController.addPageRequestListener((pageKey) {
|
|
|
|
|
_fetchPage(pageKey);
|
|
|
|
|
});
|
2023-02-08 23:48:25 +08:00
|
|
|
super.initState();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 17:28:01 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-14 22:32:13 +08:00
|
|
|
return RefreshIndicator(
|
|
|
|
|
child: PagedGridView(
|
|
|
|
|
pagingController: _pagingController,
|
|
|
|
|
builderDelegate: PagedChildBuilderDelegate<ImageResp>(
|
|
|
|
|
itemBuilder: (context, item, index) => ImageItem(image: item)),
|
|
|
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
|
|
|
crossAxisCount: MediaQuery.of(context).size.width > 640 ? 5 : 3),
|
|
|
|
|
),
|
|
|
|
|
onRefresh: () => Future.sync(() => _pagingController.refresh()));
|
2023-02-19 23:10:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ImageItem extends ConsumerStatefulWidget {
|
|
|
|
|
const ImageItem({Key? key, required this.image}) : super(key: key);
|
|
|
|
|
final ImageResp image;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
ConsumerState<ImageItem> createState() => _ImageItemState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _ImageItemState extends ConsumerState<ImageItem> {
|
2023-05-17 00:04:36 +08:00
|
|
|
// final ContextMenuController _contextMenuController = ContextMenuController();
|
2023-02-19 23:10:09 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
// TODO: implement dispose
|
2023-05-17 00:04:36 +08:00
|
|
|
// if (_contextMenuController.isShown) {
|
|
|
|
|
// _contextMenuController.remove();
|
|
|
|
|
// }
|
2023-02-19 23:10:09 +08:00
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 00:04:36 +08:00
|
|
|
// 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: '查看详情',
|
|
|
|
|
// ),
|
|
|
|
|
// ],
|
|
|
|
|
// );
|
|
|
|
|
// },
|
|
|
|
|
// );
|
|
|
|
|
// }
|
2023-02-19 23:10:09 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-17 00:04:36 +08:00
|
|
|
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: '复制链接',
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
2023-05-14 22:32:13 +08:00
|
|
|
},
|
2023-05-17 00:04:36 +08:00
|
|
|
child: GestureDetector(
|
|
|
|
|
onTap: () {
|
|
|
|
|
context.go(Uri(
|
|
|
|
|
path: "/detail",
|
|
|
|
|
queryParameters: {"id": "${widget.image.id}"}).toString());
|
|
|
|
|
},
|
|
|
|
|
child: Image.network(
|
|
|
|
|
kIsWeb
|
|
|
|
|
? "/images-api/?url=${dio.options.baseUrl}/image/${widget.image.file_path}&w=512"
|
|
|
|
|
: 'https://raichi.hodokencho.com/images-api/?url=${dio.options.baseUrl}/image/${widget.image.file_path}&w=512',
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
errorBuilder: (BuildContext context, o, err) {
|
|
|
|
|
return const Icon(Icons.error_outline);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
));
|
2023-02-07 17:28:01 +08:00
|
|
|
}
|
|
|
|
|
}
|