From 016ce7d9ca610e4fac07b9a91d7588a4148a2436 Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 21 Nov 2024 19:13:16 +0100 Subject: [PATCH] [FIX] Bug with bottom nav bar index --- .../app_code/lib/components/bottomnavbar.dart | 28 ++++------ .../app_code/lib/pages/home/home_state.dart | 56 ++++++++----------- .../app_code/test/bottomnavbar_test.dart | 4 +- 3 files changed, 36 insertions(+), 52 deletions(-) diff --git a/Application/mobile/app_code/lib/components/bottomnavbar.dart b/Application/mobile/app_code/lib/components/bottomnavbar.dart index 42f34f2c2..99462e27e 100644 --- a/Application/mobile/app_code/lib/components/bottomnavbar.dart +++ b/Application/mobile/app_code/lib/components/bottomnavbar.dart @@ -28,28 +28,24 @@ class BottomNavBar extends StatelessWidget { label: AppLocalizations.of(context)!.search, ), ]; - if (userInformation != null) { - items.add( - BottomNavigationBarItem( - icon: const Icon(Icons.shopping_basket), - label: AppLocalizations.of(context)!.myRents, - ), - ); - } + items.add( + BottomNavigationBarItem( + icon: const Icon(Icons.shopping_basket), + label: AppLocalizations.of(context)!.myRents, + ), + ); items.add( BottomNavigationBarItem( icon: const Icon(Icons.map), label: AppLocalizations.of(context)!.map, ), ); - if (userInformation != null) { - items.add( - BottomNavigationBarItem( - icon: const Icon(Icons.favorite), - label: AppLocalizations.of(context)!.favorites, - ), - ); - } + items.add( + BottomNavigationBarItem( + icon: const Icon(Icons.favorite), + label: AppLocalizations.of(context)!.favorites, + ), + ); if (userInformation == null) { items.add(BottomNavigationBarItem( icon: const Icon(Icons.settings), diff --git a/Application/mobile/app_code/lib/pages/home/home_state.dart b/Application/mobile/app_code/lib/pages/home/home_state.dart index 401a490e0..fc0d23f84 100644 --- a/Application/mobile/app_code/lib/pages/home/home_state.dart +++ b/Application/mobile/app_code/lib/pages/home/home_state.dart @@ -21,6 +21,7 @@ import 'package:risu/pages/rent/rentals/rentals_page.dart'; import 'package:risu/pages/reset_password/reset_password_page.dart'; import 'package:risu/pages/settings/settings_page.dart'; import 'package:risu/pages/signup/signup_page.dart'; +import 'package:risu/utils/check_signin.dart'; import 'package:risu/utils/errors.dart'; import 'package:risu/utils/providers/theme.dart'; @@ -31,7 +32,7 @@ import 'home_page.dart'; /// This class is the state of the HomePage class /// It contains the logic of the HomePage class class HomePageState extends State { - int _currentIndex = 1; + int _currentIndex = 2; late List _pages; bool didAskForProfile = false; int? containerId; @@ -55,38 +56,20 @@ class HomePageState extends State { configProfile(context); } }); - if (userInformation == null) { - _pages = [ - ContainerPage( - onDirectionClicked: (id) { - setState(() { - _currentIndex = 1; - containerId = id; - }); - }, - ), - const MapPage(), - const ProfilePage(), - ]; - } else { - setState(() { - _currentIndex = 2; - }); - _pages = [ - ContainerPage( - onDirectionClicked: (id) { - setState(() { - _currentIndex = 2; - containerId = id; - }); - }, - ), - const RentalPage(appbar: false), - const MapPage(), - const FavoritePage(appbar: false), - const ProfilePage(), - ]; - } + _pages = [ + ContainerPage( + onDirectionClicked: (id) { + setState(() { + _currentIndex = 2; + containerId = id; + }); + }, + ), + const RentalPage(appbar: false), + const MapPage(), + const FavoritePage(appbar: false), + const ProfilePage(), + ]; } Future redirectFromUri(Uri uri, String link) async { @@ -279,13 +262,18 @@ class HomePageState extends State { currentIndex: _currentIndex, onTap: (index) async { setState(() { - if ((index == 2 || index == 4) && userInformation == null) { + if (index == 4 && userInformation == null) { Navigator.push( context, MaterialPageRoute(builder: (context) { return const SettingsPage(); }), ); + } else if ((index == 1 || index == 3) && + userInformation == null) { + if (checkSignin(context)) { + _currentIndex = index; + } } else { _currentIndex = index; } diff --git a/Application/mobile/app_code/test/bottomnavbar_test.dart b/Application/mobile/app_code/test/bottomnavbar_test.dart index 54d849c59..7e8f29a51 100644 --- a/Application/mobile/app_code/test/bottomnavbar_test.dart +++ b/Application/mobile/app_code/test/bottomnavbar_test.dart @@ -42,11 +42,11 @@ void main() { // Verify that the initial currentIndex is 0. expect(currentIndex, 0); - // Tap the second item (index 1) in the BottomNavigationBar. + // Tap the second item (index 2) in the BottomNavigationBar. await tester.tap(find.byType(BottomNavBar)); await tester.pumpAndSettle(); // Verify that the currentIndex has changed to 1. - expect(currentIndex, 1); + expect(currentIndex, 2); }); }