From b06c28dc873a355d8c88271433be2f5aeaf88ebc Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 4 May 2019 09:21:40 +0200 Subject: [PATCH] Use RefreshIndicator for conversations list --- lib/ui/screens/conversations_list_screen.dart | 62 +++++++------------ 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/lib/ui/screens/conversations_list_screen.dart b/lib/ui/screens/conversations_list_screen.dart index ee21611..542bc24 100644 --- a/lib/ui/screens/conversations_list_screen.dart +++ b/lib/ui/screens/conversations_list_screen.dart @@ -25,7 +25,8 @@ class _ConversationScreenState extends State { final UsersHelper _usersHelper = UsersHelper(); ConversationsList _list; LoadErrorLevel _error = LoadErrorLevel.NONE; - bool _loading = true; + final GlobalKey _refreshIndicatorKey = + GlobalKey(); @override void didChangeDependencies() { @@ -35,14 +36,11 @@ class _ConversationScreenState extends State { @override void setState(fn) { - if(mounted) - super.setState(fn); + if (mounted) super.setState(fn); } void setError(LoadErrorLevel err) => setState(() => _error = err); - void setLoading(bool loading) => setState(() => _loading = loading); - /// Get the list of conversations, once from the cache, once from the server Future _loadConversations() async { await _loadConversationsList(true); @@ -50,14 +48,12 @@ class _ConversationScreenState extends State { } void _gotLoadingError() { - setLoading(false); setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR); } /// Load the list of conversations Future _loadConversationsList(bool cached) async { setError(LoadErrorLevel.NONE); - setLoading(true); //Get the list of conversations var list; @@ -73,13 +69,10 @@ class _ConversationScreenState extends State { if (list.users == null) return _gotLoadingError(); - //Save list setState(() { _list = list; }); - - setLoading(false); } /// Build an error card @@ -88,7 +81,7 @@ class _ConversationScreenState extends State { tr("Could not retrieve the list of conversations!"), actions: [ FlatButton( - onPressed: () => _loadConversationsList(false), + onPressed: () => _refreshIndicatorKey.currentState.show(), child: Text( tr("Retry").toUpperCase(), style: TextStyle( @@ -177,20 +170,25 @@ class _ConversationScreenState extends State { child: _error == LoadErrorLevel.MINOR ? _buildErrorCard() : null, ), Expanded( - child: ListView.builder( - controller: ScrollController(), - itemBuilder: (context, index) { - return ConversationTile( - conversation: _list.elementAt(index), - usersList: _list.users, - onOpen: (c) { - _openConversation(context, c.id); - }, - onRequestUpdate: _updateConversation, - onRequestDelete: _requestDeleteConversation, - ); - }, - itemCount: _list.length, + child: RefreshIndicator( + onRefresh: () => _loadConversationsList(false), + key: _refreshIndicatorKey, + child: ListView.builder( + physics: AlwaysScrollableScrollPhysics(), + controller: ScrollController(), + itemBuilder: (context, index) { + return ConversationTile( + conversation: _list.elementAt(index), + usersList: _list.users, + onOpen: (c) { + _openConversation(context, c.id); + }, + onRequestUpdate: _updateConversation, + onRequestDelete: _requestDeleteConversation, + ); + }, + itemCount: _list.length, + ), ), ), ], @@ -205,20 +203,6 @@ class _ConversationScreenState extends State { child: Icon(Icons.add), ), ), - - // Loading indicator - Positioned( - top: 8.0, - left: 0.0, - right: 0.0, - child: Container( - child: !_loading - ? null - : Center( - child: CircularProgressIndicator(), - ), - ), - ) ], ); }