@@ -2,6 +2,7 @@ import 'package:bus_reservation/pages/add_schedule_page.dart';
22import 'package:bus_reservation/pages/my_reservation_page.dart' ;
33import 'package:bus_reservation/pages/sign_up_page.dart' ;
44import 'package:bus_reservation/pages/user_details_page.dart' ;
5+ import 'package:bus_reservation/utils/helper_functions.dart' ;
56import 'package:flutter_easyloading/flutter_easyloading.dart' ;
67import 'package:bus_reservation/pages/add_bus_page.dart' ;
78import 'package:bus_reservation/pages/add_route_page.dart' ;
@@ -15,47 +16,76 @@ import 'package:bus_reservation/pages/seat_plan_page.dart';
1516import 'package:bus_reservation/utils/constants.dart' ;
1617import 'package:flutter/material.dart' ;
1718import 'package:provider/provider.dart' ;
18-
1919import 'pages/add_city.dart' ;
2020import 'providers/app_data_provider.dart' ;
2121
22+
2223void main () {
2324 runApp (ChangeNotifierProvider (
24- create: (context) => AppDataProvider (),
25- child: const MyApp ()));
25+ create: (context) => AppDataProvider (),
26+ child: const MyApp (),
27+ ));
2628}
2729
2830class MyApp extends StatelessWidget {
2931 const MyApp ({super .key});
3032
3133 @override
3234 Widget build (BuildContext context) {
33- return MaterialApp (
34- debugShowCheckedModeBanner: false ,
35- title: 'Flutter Demo' ,
36- builder: EasyLoading .init (),
37- theme: ThemeData (
38- primarySwatch: Colors .lightGreen,
39- brightness: Brightness .dark,
40- ),
41- initialRoute: routeNameHome,
42- // initialRoute: routeNameScheduleListPage,
43- routes: {
44- routeNameHome : (context) => const SearchPage (),
45- routeNameSearchResultPage : (context) => const SearchResultPage (),
46- routeNameSeatPlanPage : (context) => const SeatPlanPage (),
47- routeNameBookingConfirmationPage : (context) => const BookingConfirmationPage (),
48- routeNameAddBusPage : (context) => const AddBusPage (),
49- routeNameAddRoutePage : (context) => const AddRoutePage (),
50- routeNameScheduleListPage : (context) => const ScheduleListPage (),
51- routeNameAddSchedulePage : (context) => const AddSchedulePage (),
52- routeNameReservationPage : (context) => const ReservationPage (),
53- routeNameLoginPage : (context) => const LoginPage (),
54- routeNameAddCityPage : (context) => const AddCity (),
55- routeNameSignUpPage : (context) => const SignUpPage (),
56- routeNameUserDetailsPage : (context) => const UserDetailsPage (),
57- routeNameMyReservation : (context) => const MyReservationPage (),
35+ return FutureBuilder (
36+ future: _getInitialRoute (),
37+ builder: (context, snapshot) {
38+ // Show a loading spinner while waiting for future
39+ if (snapshot.connectionState == ConnectionState .waiting) {
40+ return const CircularProgressIndicator ();
41+ }
42+
43+ if (snapshot.hasData) {
44+ return MaterialApp (
45+ debugShowCheckedModeBanner: false ,
46+ title: 'Flutter Demo' ,
47+ builder: EasyLoading .init (),
48+ theme: ThemeData (
49+ primarySwatch: Colors .lightGreen,
50+ brightness: Brightness .dark,
51+ ),
52+ initialRoute: snapshot.data as String ,
53+ routes: {
54+ routeNameHome : (context) => const SearchPage (),
55+ routeNameSearchResultPage : (context) => const SearchResultPage (),
56+ routeNameSeatPlanPage : (context) => const SeatPlanPage (),
57+ routeNameBookingConfirmationPage : (context) => const BookingConfirmationPage (),
58+ routeNameAddBusPage : (context) => const AddBusPage (),
59+ routeNameAddRoutePage : (context) => const AddRoutePage (),
60+ routeNameScheduleListPage : (context) => const ScheduleListPage (),
61+ routeNameAddSchedulePage : (context) => const AddSchedulePage (),
62+ routeNameReservationPage : (context) => const ReservationPage (),
63+ routeNameLoginPage : (context) => const LoginPage (),
64+ routeNameAddCityPage : (context) => const AddCity (),
65+ routeNameSignUpPage : (context) => const SignUpPage (),
66+ routeNameUserDetailsPage : (context) => const UserDetailsPage (),
67+ routeNameMyReservation : (context) => const MyReservationPage (),
68+ },
69+ );
70+ } else {
71+ return const MaterialApp (
72+ home: Scaffold (
73+ body: Center (child: Text ('Error determining initial route' )),
74+ ),
75+ );
76+ }
5877 },
5978 );
6079 }
80+
81+ Future <String > _getInitialRoute () async {
82+ bool loggedIn = await isUserLoggedIn ();
83+ if (loggedIn) {
84+ String role = await getLoggedInUserRole ();
85+ if (role == 'Admin' ) {
86+ return routeNameScheduleListPage;
87+ }
88+ }
89+ return routeNameHome;
90+ }
6191}
0 commit comments