search image in SauceNAO

This commit is contained in:
quantulr
2023-05-14 22:32:13 +08:00
parent c430ef1621
commit eb918803d4
7 changed files with 77 additions and 140 deletions

View File

@ -19,14 +19,12 @@ class MyMaterialApp extends ConsumerWidget {
// UniqueKey uniqueKey = ref.watch(uniqueIdProvider);
MyMaterialRouterConfig myMaterialRouterConfig =
MyMaterialRouterConfig(token);
return SafeArea(
child: MaterialApp.router(
return MaterialApp.router(
routerConfig: myMaterialRouterConfig.router,
debugShowCheckedModeBanner: false,
theme:
ThemeData(useMaterial3: true, scaffoldBackgroundColor: Colors.white),
));
);
// return FutureBuilder(
// future: loadToken(),
// builder: (BuildContext context, AsyncSnapshot<String?> snapshot) {

View File

@ -7,6 +7,7 @@ import 'package:go_router/go_router.dart';
import 'package:momo/models/image_resp.dart';
import 'package:momo/provider/rerender.dart';
import 'package:momo/request/http_client.dart';
import 'package:url_launcher/url_launcher.dart';
class ImageDetail extends ConsumerStatefulWidget {
const ImageDetail({Key? key}) : super(key: key);
@ -33,6 +34,16 @@ class _ImageDetailState extends ConsumerState<ImageDetail> {
// double dx=0;
double _scale = 1;
Future<void> _launchInBrowser(Uri url) async {
if (!await launchUrl(
url,
mode: LaunchMode.externalApplication,
)) {
throw Exception('Could not launch $url');
}
}
@override
Widget build(BuildContext context) {
String id = GoRouterState.of(context).queryParams["id"] ?? "";
@ -91,7 +102,17 @@ class _ImageDetailState extends ConsumerState<ImageDetail> {
onPressed: _scale < 10 ? () {} : null,
icon: const Icon(Icons.add_circle_outline)),
IconButton(
onPressed: () {
onPressed: () async {
final image = await loadImage(id);
final imageUrl =
"https://raichi.hodokencho.com/api/image/${image?.file_path}";
final launchUrl =
"https://saucenao.com/search.php?url=${Uri.encodeComponent(imageUrl)}";
_launchInBrowser(Uri.parse(launchUrl));
},
icon: const Icon(Icons.search)),
IconButton(
onPressed: () async {
setState(() {});
},
icon: const Icon(Icons.menu_rounded)),

View File

@ -37,13 +37,7 @@ class ImageGrid extends StatefulWidget {
}
class _ImageGridState extends State<ImageGrid> {
late ScrollController _scrollController;
List<ImageResp> imageList = [];
int pageNum = 1;
int pageSize = 10;
bool hasMore = true;
bool loading = false;
final PagingController<int, ImageResp> _pagingController =
PagingController(firstPageKey: 1);
@ -66,26 +60,6 @@ class _ImageGridState extends State<ImageGrid> {
}
}
Future<dynamic> loadImages() async {
loading = true;
if (!hasMore) {
loading = false;
return;
}
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;
if (mounted) {
setState(() {
imageList.addAll(imageListResp.list);
});
loading = false;
}
}
@override
void initState() {
// TODO: implement initState
@ -93,39 +67,19 @@ class _ImageGridState extends State<ImageGrid> {
_fetchPage(pageKey);
});
super.initState();
// print("init");
//
// _scrollController = ScrollController(initialScrollOffset: 5.0)
// ..addListener(() {
// if (_scrollController.offset >=
// _scrollController.position.maxScrollExtent &&
// !_scrollController.position.outOfRange) {
// print("到底了 controller $loading");
// if (!loading) {
// loadImages();
// }
// }
// });
// loadImages();
}
@override
Widget build(BuildContext context) {
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]);
// });
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()));
}
}
@ -146,7 +100,6 @@ class _ImageItemState extends ConsumerState<ImageItem> {
if (_contextMenuController.isShown) {
_contextMenuController.remove();
}
super.dispose();
}
@ -233,61 +186,24 @@ class _ImageItemState extends ConsumerState<ImageItem> {
@override
Widget build(BuildContext context) {
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);
},
),
));
return 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);
},
),
);
}
}

View File

@ -11,6 +11,8 @@ class MyMaterialRouterConfig {
final GlobalKey<NavigatorState> _rootNavigatorKey =
GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> _shellNavigatorKey =
GlobalKey<NavigatorState>();
MyMaterialRouterConfig(String? token) {
router = GoRouter(
@ -18,6 +20,7 @@ class MyMaterialRouterConfig {
initialLocation: "/",
routes: <RouteBase>[
ShellRoute(
navigatorKey: _shellNavigatorKey,
builder: (BuildContext context, GoRouterState state, Widget child) {
return HomePage(
content: child,
@ -25,29 +28,31 @@ class MyMaterialRouterConfig {
},
routes: <RouteBase>[
GoRoute(
path: "/",
name: "相册",
pageBuilder: (BuildContext context, GoRouterState state) =>
const NoTransitionPage(child: Gallery()),
redirect: (BuildContext context, GoRouterState state) {
if (token == null || token.isEmpty) {
return '/login';
}
return null;
},
// routes: <RouteBase>[
// GoRoute(
// path: "detail",
// name: "图片详情",
// builder: (BuildContext context, GoRouterState state) =>
// const ImageDetail())
// ]
path: "/",
name: "相册",
pageBuilder: (BuildContext context, GoRouterState state) =>
const NoTransitionPage(
child: Gallery(),
),
redirect: (BuildContext context, GoRouterState state) {
if (token == null || token.isEmpty) {
return '/login';
}
return null;
},
// routes: <RouteBase>[
// GoRoute(
// path: "detail",
// name: "图片详情",
// builder: (BuildContext context, GoRouterState state) =>
// const ImageDetail())
// ]
),
GoRoute(
path: "/detail",
name: "图片详情",
builder: (BuildContext context, GoRouterState state) =>
const ImageDetail()),
const ImageDetail()),
GoRoute(
path: "/profile",
name: "个人资料",

View File

@ -1,7 +1,3 @@
// import 'package:fluent_ui/fluent_ui.dart';
// import 'package:fluentui_system_icons/fluentui_system_icons.dart'
// as fluentui_system_icons;
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

View File

@ -900,7 +900,7 @@ packages:
source: hosted
version: "1.3.2"
url_launcher:
dependency: transitive
dependency: "direct main"
description:
name: url_launcher
sha256: eb1e00ab44303d50dd487aab67ebc575456c146c6af44422f9c13889984c00f3

View File

@ -54,6 +54,7 @@ dependencies:
git:
url: https://github.com/fluttercandies/extended_image.git
infinite_scroll_pagination: ^3.2.0
url_launcher: ^6.1.11
dev_dependencies:
flutter_test: