From 4567b2cb21d72be497979095194593d366546135 Mon Sep 17 00:00:00 2001 From: Felipe Bueno Date: Wed, 28 Jun 2023 09:38:03 -0300 Subject: [PATCH] 1.1.0+9: More subs + Load More Itens --- lib/data/sn_api.dart | 2 +- lib/views/widgets/base_tab.dart | 112 ++++++++++++++++++++++---------- pubspec.yaml | 2 +- 3 files changed, 80 insertions(+), 36 deletions(-) diff --git a/lib/data/sn_api.dart b/lib/data/sn_api.dart index b96dd7c..ffb7a28 100644 --- a/lib/data/sn_api.dart +++ b/lib/data/sn_api.dart @@ -29,7 +29,7 @@ final class Api { ); } // START Posts / Items - Future> fetchPosts(PostType postType) async { + Future> fetchInitialPosts(PostType postType) async { String endpoint = postType.endpoint; String? currCommit = await _getCurrBuildId(); diff --git a/lib/views/widgets/base_tab.dart b/lib/views/widgets/base_tab.dart index 6dd8544..d13b6a5 100644 --- a/lib/views/widgets/base_tab.dart +++ b/lib/views/widgets/base_tab.dart @@ -27,7 +27,7 @@ class _BaseTabState extends State with AutomaticKeepAliveClientMixin { final Api _api = Api(); Future> _fetchInitialPosts() async => - await _api.fetchPosts(widget.postType); + await _api.fetchInitialPosts(widget.postType); @override Widget build(BuildContext context) { @@ -43,6 +43,7 @@ class _BaseTabState extends State with AutomaticKeepAliveClientMixin { if (snapshot.hasError || !snapshot.hasData) { final err = snapshot.error.toString(); Utils.showError(context, err); + return PostListError(err); } @@ -55,46 +56,89 @@ class _BaseTabState extends State with AutomaticKeepAliveClientMixin { onRefresh: _fetchInitialPosts, child: posts.isEmpty ? const Center(child: Text('No posts found')) - : ListView.separated( - itemBuilder: (context, index) { - if (index == posts.length) { - return const Center( - child: Padding( - padding: EdgeInsets.all(8.0), - child: CircularProgressIndicator(), - ), - ); - } - - return PostItem( - posts[index], - idx: index + 1, - ); - }, - separatorBuilder: (context, index) => const Divider(), - itemCount: posts.length, + : PostList( + posts, + postType: widget.postType, ), ), ), - if (widget.postType != PostType.job) - Padding( - padding: const EdgeInsets.all(8.0), - child: SizedBox( - width: double.infinity, - child: ElevatedButton.icon( - icon: const Icon(Icons.add), - label: const Text('MORE'), - onPressed: () async { - final items = await _api.fetchMorePosts(widget.postType); - - print(items); - }, - ), + ], + ); + }, + ); + } +} + +class PostList extends StatefulWidget { + const PostList( + this.posts, { + required this.postType, + super.key, + }); + + final List posts; + final PostType postType; + + @override + State createState() => _PostListState(); +} + +class _PostListState extends State { + final _scrollController = ScrollController(); + bool _loadingMore = false; + + @override + void initState() { + super.initState(); + + if (widget.postType != PostType.job) { + _scrollController.addListener(() async { + if (_scrollController.position.pixels == + _scrollController.position.maxScrollExtent) { + setState(() { + _loadingMore = true; + }); + + final items = await Api().fetchMorePosts(widget.postType); + + setState(() { + widget.posts.addAll(items); + _loadingMore = false; + }); + } + }); + } + } + + @override + Widget build(BuildContext context) { + return ListView.separated( + controller: _scrollController, + itemBuilder: (context, index) { + if (((index + 1) == widget.posts.length) && _loadingMore) { + return Column( + children: [ + PostItem( + widget.posts[index], + idx: index + 1, + ), + const Center( + child: Padding( + padding: EdgeInsets.all(8.0), + child: CircularProgressIndicator(), ), ), - ], + ], + ); + } + + return PostItem( + widget.posts[index], + idx: index + 1, ); }, + separatorBuilder: (context, index) => const Divider(), + itemCount: widget.posts.length, ); } } diff --git a/pubspec.yaml b/pubspec.yaml index 51361e2..638bed1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,7 +11,7 @@ description: A new Flutter project. # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 1.0.5+8 +version: 1.1.0+9 environment: sdk: '>=3.0.1 <4.0.0'