feat(infinite-query): add useInfiniteQuery hook with example
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fl_query_hooks/fl_query_hooks.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
final infiniteQueryHookJob = InfiniteQueryJob<Map, void, int>(
|
||||
queryKey: "infinite-posts",
|
||||
initialParam: 1,
|
||||
task: (queryKey, pageParam, externalData) async {
|
||||
return jsonDecode(
|
||||
(await http.get(
|
||||
Uri.parse("https://jsonplaceholder.typicode.com/posts/$pageParam"),
|
||||
))
|
||||
.body,
|
||||
);
|
||||
},
|
||||
getNextPageParam: (lastPage, lastParam) {
|
||||
return lastParam + 1;
|
||||
},
|
||||
getPreviousPageParam: (lastPage, lastParam) {
|
||||
return lastParam - 1;
|
||||
},
|
||||
);
|
||||
|
||||
class BasicHookInfiniteQueryExample extends HookWidget {
|
||||
const BasicHookInfiniteQueryExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final infiniteQuery = useInfiniteQuery(
|
||||
job: infiniteQueryHookJob,
|
||||
externalData: null,
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Stack(
|
||||
children: [
|
||||
ListView.builder(
|
||||
itemCount: infiniteQuery.pages.length,
|
||||
itemBuilder: (context, index) {
|
||||
final page = infiniteQuery.pages[index];
|
||||
return ListTile(
|
||||
title: Text(page?["title"] ?? ""),
|
||||
subtitle: Text(page?["body"] ?? ""),
|
||||
);
|
||||
},
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.bottomRight,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh_rounded),
|
||||
onPressed: () => infiniteQuery.refetchPages(),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.get_app_rounded),
|
||||
onPressed: () => infiniteQuery.fetchNextPage(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:fl_query_hooks_example/components/basic_hook_infinite_query.dart';
|
||||
import 'package:fl_query_hooks_example/components/basic_hook_mutation.dart';
|
||||
import 'package:fl_query_hooks_example/components/basic_hook_query.dart';
|
||||
import 'package:fl_query_hooks_example/components/lazy_hook_query.dart';
|
||||
@@ -49,15 +50,26 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
children: const [
|
||||
BasicHookQueryExample(),
|
||||
QueryHookExternalDataExample(),
|
||||
LazyHookQueryExample(),
|
||||
QueryHookVariableKeyExample(),
|
||||
QueryHookPreviousDataExample(),
|
||||
Divider(),
|
||||
BasicHookMutationExample(),
|
||||
MutationHookVariableKeyExample(),
|
||||
children: [
|
||||
const BasicHookQueryExample(),
|
||||
const QueryHookExternalDataExample(),
|
||||
const LazyHookQueryExample(),
|
||||
const QueryHookVariableKeyExample(),
|
||||
const QueryHookPreviousDataExample(),
|
||||
ListTile(
|
||||
title: const Text("Infinite Query Example"),
|
||||
trailing: const Icon(Icons.open_in_new),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const BasicHookInfiniteQueryExample(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
const Divider(),
|
||||
const BasicHookMutationExample(),
|
||||
const MutationHookVariableKeyExample(),
|
||||
],
|
||||
),
|
||||
)),
|
||||
|
||||
Reference in New Issue
Block a user