diff --git a/lib/pages/home/bloc/home_bloc.dart b/lib/pages/home/bloc/home_bloc.dart index 1b65adfe..fda76728 100644 --- a/lib/pages/home/bloc/home_bloc.dart +++ b/lib/pages/home/bloc/home_bloc.dart @@ -1,3 +1,4 @@ +import 'package:flutter/widgets.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:go_router/go_router.dart'; @@ -51,6 +52,8 @@ class HomeBloc extends Bloc { await const FlutterSecureStorage().read(key: UserModel.userUuidKey); user = await HomeApi().fetchUserInfo(uuid); add(FetchTermEvent()); + add(FetchPolicyEvent()); + emit(HomeInitial()); } catch (e) { return; @@ -61,7 +64,8 @@ class HomeBloc extends Bloc { try { emit(LoadingHome()); terms = await HomeApi().fetchTerms(); - add(FetchPolicyEvent()); + emit(HomeInitial()); + // emit(PolicyAgreement()); } catch (e) { return; @@ -72,8 +76,11 @@ class HomeBloc extends Bloc { try { emit(LoadingHome()); policy = await HomeApi().fetchPolicy(); - // emit(PolicyAgreement()); + debugPrint("Fetched policy: $policy"); + // Emit a state to trigger the UI update + emit(HomeInitial()); } catch (e) { + debugPrint("Error fetching policy: $e"); return; } } diff --git a/lib/pages/home/view/home_page_web.dart b/lib/pages/home/view/home_page_web.dart index 89ff0da4..baf18fc2 100644 --- a/lib/pages/home/view/home_page_web.dart +++ b/lib/pages/home/view/home_page_web.dart @@ -9,105 +9,126 @@ import 'package:syncrow_web/pages/home/view/home_card.dart'; import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/web_layout/web_scaffold.dart'; -class HomeWebPage extends StatelessWidget { +class HomeWebPage extends StatefulWidget { const HomeWebPage({super.key}); + @override + State createState() => _HomeWebPageState(); +} + +class _HomeWebPageState extends State { + // Flag to track whether the dialog is already shown. + bool _dialogShown = false; + + @override + void initState() { + super.initState(); + final homeBloc = BlocProvider.of(context); + homeBloc.add(FetchUserInfo()); + } + @override Widget build(BuildContext context) { Size size = MediaQuery.of(context).size; final homeBloc = BlocProvider.of(context); return PopScope( - canPop: false, - onPopInvoked: (didPop) => false, - child: BlocConsumer( - listener: (BuildContext context, state) { - if (state is PolicyAgreement) { - if (homeBloc.user!.hasAcceptedWebAgreement == false) { - Future.delayed(const Duration(seconds: 1), () { - showDialog( - context: context, - barrierDismissible: false, - builder: (BuildContext context) { - return AgreementAndPrivacyDialog( - terms: homeBloc.terms, - policy: homeBloc.policy, - ); - }, - ).then((v) { - if (v != null) { - homeBloc.add(ConfirmUserAgreementEvent()); - homeBloc.add(const FetchUserInfo()); - } - }); + canPop: false, + onPopInvoked: (didPop) => false, + child: BlocConsumer( + listener: (BuildContext context, state) { + print('state=$state'); + if (state is HomeInitial) { + if (homeBloc.user!.hasAcceptedWebAgreement == false && + !_dialogShown) { + _dialogShown = + true; // Set the flag to true to indicate the dialog is showing. + Future.delayed(const Duration(seconds: 1), () { + showDialog( + context: context, + barrierDismissible: false, + builder: (BuildContext context) { + return AgreementAndPrivacyDialog( + terms: homeBloc.terms, + policy: homeBloc.policy, + ); + }, + ).then((v) { + _dialogShown = false; + if (v != null) { + homeBloc.add(ConfirmUserAgreementEvent()); + homeBloc.add(const FetchUserInfo()); + } }); - } + }); } - }, - builder: (context, state) { - return WebScaffold( - enableMenuSidebar: false, - appBarTitle: Row( + } + }, + builder: (context, state) { + return WebScaffold( + enableMenuSidebar: false, + appBarTitle: Row( + children: [ + SvgPicture.asset( + Assets.loginLogo, + width: 150, + ), + ], + ), + scaffoldBody: SizedBox( + height: size.height, + width: size.width, + child: Row( + mainAxisAlignment: MainAxisAlignment.center, children: [ - SvgPicture.asset( - Assets.loginLogo, - width: 150, + Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + SizedBox(height: size.height * 0.1), + Text( + 'ACCESS YOUR APPS', + style: Theme.of(context) + .textTheme + .headlineLarge! + .copyWith(color: Colors.black, fontSize: 40), + ), + const SizedBox(height: 30), + Expanded( + flex: 4, + child: SizedBox( + height: size.height * 0.6, + width: size.width * 0.68, + child: GridView.builder( + itemCount: 3, // Change this count if needed. + gridDelegate: + const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 3, // Adjust as needed. + crossAxisSpacing: 20.0, + mainAxisSpacing: 20.0, + childAspectRatio: 1.5, + ), + itemBuilder: (context, index) { + return HomeCard( + index: index, + active: homeBloc.homeItems[index].active!, + name: homeBloc.homeItems[index].title!, + img: homeBloc.homeItems[index].icon!, + onTap: () => + homeBloc.homeItems[index].onPress(context), + ); + }, + ), + ), + ), + ], ), ], ), - scaffoldBody: SizedBox( - height: size.height, - width: size.width, - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - SizedBox(height: size.height * 0.1), - Text( - 'ACCESS YOUR APPS', - style: Theme.of(context) - .textTheme - .headlineLarge! - .copyWith(color: Colors.black, fontSize: 40), - ), - const SizedBox(height: 30), - Expanded( - flex: 4, - child: SizedBox( - height: size.height * 0.6, - width: size.width * 0.68, - child: GridView.builder( - itemCount: 3, //8 - gridDelegate: - const SliverGridDelegateWithFixedCrossAxisCount( - crossAxisCount: 3, //4 - crossAxisSpacing: 20.0, - mainAxisSpacing: 20.0, - childAspectRatio: 1.5, - ), - itemBuilder: (context, index) { - return HomeCard( - index: index, - active: homeBloc.homeItems[index].active!, - name: homeBloc.homeItems[index].title!, - img: homeBloc.homeItems[index].icon!, - onTap: () => homeBloc.homeItems[index] - .onPress(context), - ); - }, - ), - ), - ), - ], - ), - ], - ), - ), - ); - }, - )); + ), + ); + }, + ), + ); } }