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-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.
|
|
|
|
typedef ContextMenuBuilder = Widget Function(
|
|
|
|
BuildContext context, Offset offset);
|
|
|
|
|
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-10 17:33:51 +08:00
|
|
|
late ScrollController _scrollController;
|
|
|
|
|
2023-02-09 17:07:14 +08:00
|
|
|
List<ImageResp> imageList = [];
|
|
|
|
int pageNum = 1;
|
|
|
|
int pageSize = 10;
|
2023-02-10 17:33:51 +08:00
|
|
|
bool hasMore = true;
|
|
|
|
bool loading = false;
|
2023-02-09 17:07:14 +08:00
|
|
|
|
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
|
|
|
Future<dynamic> loadImages() async {
|
2023-02-09 17:07:14 +08:00
|
|
|
loading = true;
|
2023-02-10 17:33:51 +08:00
|
|
|
if (!hasMore) {
|
|
|
|
loading = false;
|
|
|
|
return;
|
|
|
|
}
|
2023-02-09 17:07:14 +08:00
|
|
|
print(pageNum);
|
|
|
|
var resp = await dio.get("/image/history",
|
|
|
|
queryParameters: {"page": pageNum, "size": pageSize});
|
|
|
|
ImageListResp imageListResp = ImageListResp.fromJson(resp.data);
|
|
|
|
hasMore = imageListResp.hasNext;
|
|
|
|
pageNum = pageNum + 1;
|
2023-02-08 23:48:25 +08:00
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
2023-02-09 17:07:14 +08:00
|
|
|
imageList.addAll(imageListResp.list);
|
2023-02-08 23:48:25 +08:00
|
|
|
});
|
2023-02-09 17:07:14 +08:00
|
|
|
loading = false;
|
2023-02-08 23:48:25 +08:00
|
|
|
}
|
2023-02-08 17:20:16 +08:00
|
|
|
}
|
|
|
|
|
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-05-14 19:59:43 +08:00
|
|
|
// print("init");
|
|
|
|
//
|
|
|
|
// _scrollController = ScrollController(initialScrollOffset: 5.0)
|
|
|
|
// ..addListener(() {
|
|
|
|
// if (_scrollController.offset >=
|
|
|
|
// _scrollController.position.maxScrollExtent &&
|
|
|
|
// !_scrollController.position.outOfRange) {
|
|
|
|
// print("到底了 controller $loading");
|
|
|
|
// if (!loading) {
|
|
|
|
// loadImages();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// loadImages();
|
2023-02-08 23:48:25 +08:00
|
|
|
}
|
|
|
|
|
2023-02-07 17:28:01 +08:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-14 19:59:43 +08:00
|
|
|
return PagedGridView(
|
|
|
|
pagingController: _pagingController,
|
|
|
|
builderDelegate: PagedChildBuilderDelegate<ImageResp>(
|
|
|
|
itemBuilder: (context, item, index) => ImageItem(image: item)),
|
|
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
|
|
crossAxisCount: MediaQuery.of(context).size.width > 640 ? 5 : 3),
|
|
|
|
);
|
|
|
|
// return GridView.builder(
|
|
|
|
// itemCount: imageList.length,
|
|
|
|
// controller: _scrollController,
|
|
|
|
// gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
|
|
// crossAxisCount: MediaQuery.of(context).size.width > 640 ? 5 : 3),
|
|
|
|
// itemBuilder: (BuildContext context, int index) {
|
|
|
|
// return ImageItem(image: imageList[index]);
|
|
|
|
// });
|
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> {
|
|
|
|
final ContextMenuController _contextMenuController = ContextMenuController();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
// TODO: implement dispose
|
|
|
|
if (_contextMenuController.isShown) {
|
|
|
|
_contextMenuController.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-02-09 17:07:14 +08:00
|
|
|
},
|
2023-02-19 23:10:09 +08:00
|
|
|
label: '删除',
|
2023-02-08 17:20:16 +08:00
|
|
|
),
|
2023-02-19 23:10:09 +08:00
|
|
|
ContextMenuButtonItem(
|
|
|
|
onPressed: () {
|
|
|
|
ContextMenuController.removeAny();
|
|
|
|
// _showDialog(context);
|
|
|
|
Clipboard.setData(ClipboardData(
|
|
|
|
text:
|
|
|
|
"${dio.options.baseUrl}/image/${widget.image.file_path}"));
|
2023-05-14 19:59:43 +08:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
2023-02-19 23:10:09 +08:00
|
|
|
content: Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
2023-05-14 19:59:43 +08:00
|
|
|
children: [
|
2023-02-19 23:10:09 +08:00
|
|
|
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) {
|
2023-05-14 19:59:43 +08:00
|
|
|
return CupertinoContextMenu(
|
|
|
|
actions: [
|
|
|
|
CupertinoContextMenuAction(
|
|
|
|
trailingIcon: CupertinoIcons.delete,
|
|
|
|
child: const Text('删除'),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
// Navigator.pop(context);
|
|
|
|
// showDialog(
|
|
|
|
// context: context,
|
|
|
|
// builder: (BuildContext dialogContext) => AlertDialog(
|
|
|
|
// title: const Text('删除图片'),
|
|
|
|
// content: const Text("确认删除图片"),
|
|
|
|
// actions: [
|
|
|
|
// TextButton(
|
|
|
|
// onPressed: () =>
|
|
|
|
// Navigator.pop(dialogContext, 'Cancel'),
|
|
|
|
// child: const Text('Cancel'),
|
|
|
|
// ),
|
|
|
|
// TextButton(
|
|
|
|
// onPressed: () {
|
|
|
|
// Navigator.pop(dialogContext, 'OK');
|
|
|
|
// // Navigator.pop(context);
|
|
|
|
// dio
|
|
|
|
// .delete("/image/delete/${widget.image.id}")
|
|
|
|
// .then((resp) {
|
|
|
|
// ref.read(uniqueIdProvider.notifier).updateId();
|
|
|
|
// // context.go("/");
|
|
|
|
// });
|
|
|
|
// },
|
|
|
|
// child: const Text('OK'),
|
|
|
|
// ),
|
|
|
|
// ],
|
|
|
|
// ));
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
child: GestureDetector(
|
|
|
|
onSecondaryTapUp: (TapUpDetails details) {
|
|
|
|
_showContextMenu(details.globalPosition);
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|