2023-02-07 17:28:01 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:go_router/go_router.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-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-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
|
|
|
|
super.initState();
|
2023-02-09 17:07:14 +08:00
|
|
|
print("init");
|
2023-02-11 22:29:33 +08:00
|
|
|
|
2023-02-10 17:33:51 +08:00
|
|
|
_scrollController = ScrollController(initialScrollOffset: 5.0)
|
|
|
|
..addListener(() {
|
|
|
|
if (_scrollController.offset >=
|
|
|
|
_scrollController.position.maxScrollExtent &&
|
|
|
|
!_scrollController.position.outOfRange) {
|
|
|
|
print("到底了 controller $loading");
|
|
|
|
if (!loading) {
|
|
|
|
loadImages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-02-08 23:48:25 +08:00
|
|
|
loadImages();
|
|
|
|
}
|
|
|
|
|
2023-02-07 17:28:01 +08:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-08 17:20:16 +08:00
|
|
|
return GridView.builder(
|
|
|
|
itemCount: imageList.length,
|
2023-02-10 17:33:51 +08:00
|
|
|
controller: _scrollController,
|
2023-02-08 17:20:16 +08:00
|
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
|
|
crossAxisCount: MediaQuery.of(context).size.width > 640 ? 5 : 3),
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
return InkWell(
|
|
|
|
onTap: () {
|
2023-02-09 17:07:14 +08:00
|
|
|
context.go(Uri(
|
|
|
|
path: "/detail",
|
|
|
|
queryParameters: {"id": "${imageList[index].id}"})
|
|
|
|
.toString());
|
2023-02-08 17:20:16 +08:00
|
|
|
},
|
|
|
|
child: Image.network(
|
2023-02-08 23:48:25 +08:00
|
|
|
"${dio.options.baseUrl}/image/thumbnail/${imageList[index].file_path}",
|
2023-02-08 17:20:16 +08:00
|
|
|
fit: BoxFit.cover,
|
2023-02-09 17:07:14 +08:00
|
|
|
errorBuilder: (BuildContext context, o, err) {
|
|
|
|
return const Icon(Icons.error_outline);
|
|
|
|
},
|
2023-02-08 17:20:16 +08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2023-02-07 17:28:01 +08:00
|
|
|
}
|
|
|
|
}
|