From 789506876d3f4b7967b5641c4e07d935b46cf77a Mon Sep 17 00:00:00 2001 From: violet-dev Date: Fri, 17 Jul 2020 21:47:08 +0900 Subject: [PATCH] (search) Load more search result when 3/4 scroll --- lib/pages/search/search_page.dart | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/pages/search/search_page.dart b/lib/pages/search/search_page.dart index c7c3acc78..c025c48a1 100644 --- a/lib/pages/search/search_page.dart +++ b/lib/pages/search/search_page.dart @@ -79,10 +79,26 @@ class _SearchPageState extends State queryResult = List(); await loadNextQuery(); }); + + _scroll.addListener(() { + if (scrollOnce || queryEnd) return; + if (_scroll.offset > _scroll.position.maxScrollExtent / 4 * 3) { + scrollOnce = true; + Future.delayed(Duration(milliseconds: 100)).then((value) async { + print('qqqq'); + await loadNextQuery(); + scrollOnce = false; + }); + } + }); } + bool scrollOnce = false; + Tuple2 latestQuery; + ScrollController _scroll = ScrollController(); + // https://stackoverflow.com/questions/60643355/is-it-possible-to-have-both-expand-and-contract-effects-with-the-slivers-in @override Widget build(BuildContext context) { @@ -92,6 +108,7 @@ class _SearchPageState extends State padding: EdgeInsets.only(top: statusBarHeight), child: GestureDetector( child: CustomScrollView( + controller: _scroll, physics: const BouncingScrollPhysics(), slivers: [ SliverPersistentHeader( @@ -197,6 +214,7 @@ class _SearchPageState extends State isOr = false; tagStates = Map(); groupStates = Map(); + queryEnd = false; await loadNextQuery(); }); // print(latestQuery); @@ -363,9 +381,15 @@ class _SearchPageState extends State ObjectKey key = ObjectKey(Uuid().v4()); + bool queryEnd = false; + Future loadNextQuery() async { + if (queryEnd) return; var nn = await latestQuery.item1.next(); - if (nn.length == 0) return; + if (nn.length == 0) { + queryEnd = true; + return; + } setState(() { queryResult.addAll(nn); });