After logging in to the Flutter app, I am redirected to the home screen, but I'm unable to navigate to other screens such as Messages or Profile. The navigation bar appears correctly, but when I tap on other navigation items, the respective screens don't render. However, when I restart the app, the issue disappears temporarily, only to reappear upon logging in again.
I've checked the code, and it seems that the navigation logic is correctly implemented. When debugging, I found that the correct screens are returned from the _buildHome method in main.dart. However, the screens don't render on the UI. Upon logging in, I expect to be able to navigate to different screens smoothly without any issues.
Here are the relevant parts of the code:
main.dart
Future main() async {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => SettingsProvider()),
ChangeNotifierProvider(create: (context) => UserProvider()),
],
child: MainApp(),
),
);
}
class MainApp extends StatelessWidget {
MainApp({super.key});
final ThemeData lightTheme = ThemeData(
colorScheme: lightColorScheme,
useMaterial3: true,
);
final ThemeData darkTheme = ThemeData(
colorScheme: darkColorScheme,
useMaterial3: true,
);
@override
Widget build(BuildContext context) {
var settingsProvider = Provider.of<SettingsProvider>(context);
var userProvider = Provider.of<UserProvider>(context);
bool isUserLoggedIn = userProvider.isLoggedIn();
return MaterialApp(
theme: settingsProvider.isDarkMode ? darkTheme : lightTheme,
home: isUserLoggedIn
? _buildHome(settingsProvider.currentPageIndex)
: const LoginScreen(),
routes: {
'/login': (context) => const LoginScreen(),
'/register': (context) => const RegisterScreen(),
'/home': (context) => const HomeScreen(),
'/profile': (context) => const MyProfileScreen(),
'/messages': (context) => const MessagesScreen(),
'/chat': (context) => const ChatScreen(),
});
}
Widget _buildHome(int currentPageIndex) {
switch (currentPageIndex) {
case 0:
return const HomeScreen();
case 1:
return const MessagesScreen();
case 2:
return const MyProfileScreen();
default:
return const HomeScreen();
}
}
}
LoginScreen.dart loginFunc
Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) {
return const HomeScreen();
}), (route) => false);
UserProvider.dart - it's working right
bool isLoggedIn() {
return _user.token.isNotEmpty;
}
InNavigationBar.dart
class InNavigationBar extends StatelessWidget implements PreferredSizeWidget {
const InNavigationBar({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var settingsProvider = Provider.of<SettingsProvider>(context);
return NavigationBar(
onDestinationSelected: (int index) {
settingsProvider.setCurrentPageIndex(index);
},
backgroundColor: Theme.of(context).colorScheme.surfaceVariant,
indicatorColor: Theme.of(context).colorScheme.primary,
selectedIndex: settingsProvider.currentPageIndex,
destinations: const <Widget>[
NavigationDestination(
selectedIcon: Icon(Icons.home),
icon: Icon(Icons.home_outlined),
label: 'Üyeler',
),
NavigationDestination(
icon: Badge(
label: Text('2'),
child: Icon(Icons.messenger_sharp),
),
label: 'Mesajlar',
),
NavigationDestination(
icon: Icon(Icons.person_2),
label: 'Profil',
),
],
);
}
@override
Size get preferredSize => const Size.fromHeight(kBottomNavigationBarHeight);
}