From f41983304a054c3befcbcd4b26be372605282027 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 8 May 2020 09:23:02 +0200 Subject: [PATCH] Improve user page performances --- lib/ui/screens/user_page_screen.dart | 57 ++++++++------------------- lib/ui/widgets/posts_list_widget.dart | 11 +++++- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/lib/ui/screens/user_page_screen.dart b/lib/ui/screens/user_page_screen.dart index 6e65d96..b686c4e 100644 --- a/lib/ui/screens/user_page_screen.dart +++ b/lib/ui/screens/user_page_screen.dart @@ -6,7 +6,6 @@ import 'package:comunic/ui/routes/main_route/main_route.dart'; import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/ui/widgets/post_create_form_widget.dart'; import 'package:comunic/ui/widgets/posts_list_widget.dart'; -import 'package:comunic/ui/widgets/scroll_watcher.dart'; import 'package:comunic/utils/conversations_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; @@ -42,19 +41,8 @@ class _UserPageScreenState extends State { GlobalKey _refreshIndicatorKey = GlobalKey(); - // Scroll detection (to load more user posts automatically) - final _postListKey = GlobalKey(); - ScrollWatcher _scrollWatcher; - _setStatus(_PageStatus s) => setState(() => _status = s); - @override - void initState() { - _scrollWatcher = ScrollWatcher( - onReachBottom: () => _postListKey.currentState.reachedPostsBottom()); - super.initState(); - } - @override void didChangeDependencies() { super.didChangeDependencies(); @@ -92,13 +80,7 @@ class _UserPageScreenState extends State { return Scaffold( body: RefreshIndicator( key: _refreshIndicatorKey, - child: ListView( - children: [] - ..add(_buildHeader()) - ..addAll(_buildBody()), - physics: AlwaysScrollableScrollPhysics(), - controller: _scrollWatcher, - ), + child: _buildBody(), onRefresh: _getUserInfo, ), ); @@ -161,27 +143,22 @@ class _UserPageScreenState extends State { ); } - List _buildBody() { - return [ - // Posts create form - _userInfo.canPostTexts - ? PostCreateFormWidget( - postTarget: PostTarget.USER_PAGE, - targetID: _userInfo.id, - onCreated: _postCreated, - ) - : Container(), - - // Posts list - PostsListWidget( - key: _postListKey, - getPostsList: () => _postsHelper.getUserPosts(widget.userID), - getOlder: (from) => - _postsHelper.getUserPosts(widget.userID, from: from), - showPostsTarget: false, - buildListView: false, - ), - ]; + Widget _buildBody() { + return PostsListWidget( + topWidgets: [ + _buildHeader(), + _userInfo.canPostTexts + ? PostCreateFormWidget( + postTarget: PostTarget.USER_PAGE, + targetID: _userInfo.id, + onCreated: _postCreated, + ) + : Container() + ], + getPostsList: () => _postsHelper.getUserPosts(widget.userID), + getOlder: (from) => _postsHelper.getUserPosts(widget.userID, from: from), + showPostsTarget: false, + ); } /// Method called each time a menu option is selected diff --git a/lib/ui/widgets/posts_list_widget.dart b/lib/ui/widgets/posts_list_widget.dart index d17a9d5..7cfe8b7 100644 --- a/lib/ui/widgets/posts_list_widget.dart +++ b/lib/ui/widgets/posts_list_widget.dart @@ -22,6 +22,7 @@ import 'package:flutter/material.dart'; /// @author Pierre HUBERT class PostsListWidget extends StatefulWidget { + final List topWidgets; final Future Function() getPostsList; final Future Function(int from) getOlder; final bool showPostsTarget; @@ -35,6 +36,7 @@ class PostsListWidget extends StatefulWidget { this.userNamesClickable = true, this.buildListView = true, this.getOlder, + this.topWidgets, }) : assert(getPostsList != null), assert(showPostsTarget != null), assert(buildListView != null), @@ -62,6 +64,9 @@ class PostsListWidgetState extends SafeState { set error(ErrorLevel err) => setState(() => _error = err); + int get _numberTopWidgets => + widget.topWidgets == null ? 0 : widget.topWidgets.length; + @override void initState() { super.initState(); @@ -187,7 +192,7 @@ class PostsListWidgetState extends SafeState { Widget _buildListView() { return RefreshIndicator( child: ListView.builder( - itemCount: _list.length, + itemCount: _list.length + _numberTopWidgets, itemBuilder: _buildItem, controller: _scrollController, ), @@ -205,8 +210,10 @@ class PostsListWidgetState extends SafeState { } Widget _buildItem(BuildContext context, int index) { + if (index < _numberTopWidgets) return widget.topWidgets[index]; + return PostTile( - post: _list[index], + post: _list[index - _numberTopWidgets], usersInfo: _users, groupsInfo: _groups, onDeletedPost: _removePost,