152 lines
4.8 KiB
Dart
152 lines
4.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'core/di/injection.dart';
|
|
import 'core/network/api_client.dart';
|
|
import 'core/router/app_router.dart';
|
|
import 'core/theme/app_theme.dart';
|
|
import 'data/repositories/admin_repository_impl.dart';
|
|
import 'data/repositories/wallet_repository_impl.dart';
|
|
import 'domain/repositories/faq_repository.dart';
|
|
import 'domain/repositories/health_repository.dart';
|
|
import 'domain/repositories/prediction_repository.dart';
|
|
import 'domain/repositories/referral_repository.dart';
|
|
import 'domain/repositories/repositories.dart';
|
|
import 'data/repositories/support_repository.dart';
|
|
import 'features/auth/bloc/auth_bloc.dart';
|
|
import 'features/prediction/bloc/prediction_bloc.dart';
|
|
import 'features/support/bloc/support_bloc.dart';
|
|
import 'features/support/presentation/support_widget.dart';
|
|
import 'features/wallet/bloc/wallet_bloc.dart';
|
|
import 'l10n/app_locale.dart';
|
|
import 'l10n/locale_controller.dart';
|
|
|
|
class PrediqoApp extends StatefulWidget {
|
|
const PrediqoApp({super.key});
|
|
|
|
@override
|
|
State<PrediqoApp> createState() => _PrediqoAppState();
|
|
}
|
|
|
|
class _PrediqoAppState extends State<PrediqoApp> {
|
|
late final AuthBloc _authBloc;
|
|
late final SupportBloc _supportBloc;
|
|
late final GoRouter _router;
|
|
late final LocaleController _localeController;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_localeController = sl<LocaleController>();
|
|
_authBloc = sl<AuthBloc>();
|
|
_supportBloc = sl<SupportBloc>();
|
|
sl<ApiClient>().onSessionExpired = () {
|
|
if (!_authBloc.isClosed) {
|
|
_authBloc.add(const AuthSessionExpired());
|
|
}
|
|
};
|
|
_router = createAppRouter(_authBloc);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
sl<ApiClient>().onSessionExpired = null;
|
|
_supportBloc.close();
|
|
_authBloc.close();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MultiRepositoryProvider(
|
|
providers: [
|
|
RepositoryProvider<ApiClient>.value(value: sl()),
|
|
RepositoryProvider<AuthRepository>.value(value: sl<AuthRepository>()),
|
|
RepositoryProvider<WalletRepositoryImpl>.value(value: sl()),
|
|
RepositoryProvider<AdminRepositoryImpl>.value(value: sl()),
|
|
RepositoryProvider<HealthRepository>.value(value: sl()),
|
|
RepositoryProvider<PredictionRepository>.value(value: sl()),
|
|
RepositoryProvider<ReferralRepository>.value(value: sl()),
|
|
RepositoryProvider<FaqRepository>.value(value: sl()),
|
|
RepositoryProvider<SupportRepository>.value(value: sl()),
|
|
],
|
|
child: MultiBlocProvider(
|
|
providers: [
|
|
BlocProvider.value(value: _authBloc),
|
|
BlocProvider.value(value: _supportBloc),
|
|
BlocProvider(create: (_) => sl<WalletBloc>()),
|
|
BlocProvider(create: (_) => sl<PredictionBloc>()),
|
|
],
|
|
child: ListenableBuilder(
|
|
listenable: _localeController,
|
|
builder: (context, _) {
|
|
return MaterialApp.router(
|
|
title: 'Tloto',
|
|
debugShowCheckedModeBanner: false,
|
|
theme: AppTheme.light,
|
|
darkTheme: AppTheme.dark,
|
|
themeMode: ThemeMode.dark,
|
|
locale: _localeController.locale,
|
|
supportedLocales: AppLocale.supported,
|
|
localizationsDelegates: const [
|
|
GlobalMaterialLocalizations.delegate,
|
|
GlobalWidgetsLocalizations.delegate,
|
|
GlobalCupertinoLocalizations.delegate,
|
|
],
|
|
routerConfig: _router,
|
|
builder: (context, child) {
|
|
return _SupportRouteLayer(router: _router, child: child);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Rebuilds the support overlay only when the route path actually changes.
|
|
class _SupportRouteLayer extends StatefulWidget {
|
|
const _SupportRouteLayer({required this.router, this.child});
|
|
|
|
final GoRouter router;
|
|
final Widget? child;
|
|
|
|
@override
|
|
State<_SupportRouteLayer> createState() => _SupportRouteLayerState();
|
|
}
|
|
|
|
class _SupportRouteLayerState extends State<_SupportRouteLayer> {
|
|
late String _path;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_path = widget.router.routerDelegate.currentConfiguration.uri.path;
|
|
widget.router.routerDelegate.addListener(_onRoute);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.router.routerDelegate.removeListener(_onRoute);
|
|
super.dispose();
|
|
}
|
|
|
|
void _onRoute() {
|
|
final path = widget.router.routerDelegate.currentConfiguration.uri.path;
|
|
if (path == _path) return;
|
|
setState(() => _path = path);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Stack(
|
|
children: [
|
|
widget.child ?? const SizedBox.shrink(),
|
|
SupportOverlay(currentPath: _path),
|
|
],
|
|
);
|
|
}
|
|
}
|