From 11d61e163fafbf5d222a5dd4489cd5c2c2e999b9 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Tue, 6 Oct 2020 21:33:10 +0800 Subject: [PATCH] feat: avoid reloading widget on material bottom tab switch closes #42 --- lib/home.dart | 129 ++++++++++++++++++++------------------------------ 1 file changed, 51 insertions(+), 78 deletions(-) diff --git a/lib/home.dart b/lib/home.dart index f7d2e80b..a6cbb01c 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -110,81 +110,6 @@ class _HomeState extends State { ); } - List get _navigationItems { - switch (Provider.of(context).activeAccount.platform) { - case PlatformType.github: - return [ - BottomNavigationBarItem( - icon: Icon(Icons.rss_feed), - label: 'News', - ), - BottomNavigationBarItem( - icon: _buildNotificationIcon(context, false), - activeIcon: _buildNotificationIcon(context, true), - label: 'Notification', - ), - BottomNavigationBarItem( - icon: Icon(Icons.whatshot), - label: 'Trending', - ), - BottomNavigationBarItem( - icon: Icon(Icons.search), - label: 'Search', - ), - BottomNavigationBarItem( - icon: Icon(Icons.person), - activeIcon: Icon(Icons.person), - label: 'Me', - ), - ]; - case PlatformType.gitlab: - return [ - BottomNavigationBarItem( - icon: Icon(Icons.explore), - label: 'Explore', - ), - BottomNavigationBarItem( - icon: Icon(Icons.group), - label: 'Groups', - ), - BottomNavigationBarItem( - icon: Icon(Icons.search), - label: 'Search', - ), - BottomNavigationBarItem( - icon: Icon(Icons.person), - label: 'Me', - ), - ]; - case PlatformType.bitbucket: - return [ - BottomNavigationBarItem( - icon: Icon(Icons.explore), - label: 'Explore', - ), - BottomNavigationBarItem( - icon: Icon(Icons.group), - label: 'Teams', - ), - BottomNavigationBarItem( - icon: Icon(Icons.person), - label: 'Me', - ), - ]; - case PlatformType.gitea: - return [ - BottomNavigationBarItem( - icon: Icon(Icons.group), - label: 'Organizations', - ), - BottomNavigationBarItem( - icon: Icon(Icons.person), - label: 'Me', - ), - ]; - } - } - GlobalKey getNavigatorKey(int index) { switch (index) { case 0: @@ -210,6 +135,49 @@ class _HomeState extends State { return LoginScreen(); } + List navigationItems; + + switch (auth.activeAccount.platform) { + case PlatformType.github: + navigationItems = [ + BottomNavigationBarItem(icon: Icon(Icons.rss_feed), label: 'News'), + BottomNavigationBarItem( + icon: _buildNotificationIcon(context, false), + activeIcon: _buildNotificationIcon(context, true), + label: 'Notification'), + BottomNavigationBarItem( + icon: Icon(Icons.whatshot), label: 'Trending'), + BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'), + BottomNavigationBarItem( + icon: Icon(Icons.person), + activeIcon: Icon(Icons.person), + label: 'Me'), + ]; + break; + case PlatformType.gitlab: + navigationItems = [ + BottomNavigationBarItem(icon: Icon(Icons.explore), label: 'Explore'), + BottomNavigationBarItem(icon: Icon(Icons.group), label: 'Groups'), + BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'), + BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Me'), + ]; + break; + case PlatformType.bitbucket: + navigationItems = [ + BottomNavigationBarItem(icon: Icon(Icons.explore), label: 'Explore'), + BottomNavigationBarItem(icon: Icon(Icons.group), label: 'Teams'), + BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Me'), + ]; + break; + case PlatformType.gitea: + navigationItems = [ + BottomNavigationBarItem( + icon: Icon(Icons.group), label: 'Organizations'), + BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Me'), + ]; + break; + } + switch (theme.theme) { case AppThemeType.cupertino: return WillPopScope( @@ -228,7 +196,7 @@ class _HomeState extends State { ); }, tabBar: CupertinoTabBar( - items: _navigationItems, + items: navigationItems, currentIndex: auth.activeTab, onTap: (index) { if (auth.activeTab == index) { @@ -244,10 +212,15 @@ class _HomeState extends State { ); default: return Scaffold( - body: _buildScreen(auth.activeTab), + body: IndexedStack( + index: auth.activeTab, + children: [ + for (var i = 0; i < navigationItems.length; i++) _buildScreen(i) + ], + ), bottomNavigationBar: BottomNavigationBar( selectedItemColor: theme.palette.primary, - items: _navigationItems, + items: navigationItems, currentIndex: auth.activeTab, type: BottomNavigationBarType.fixed, onTap: (int index) {