| @@ -10,6 +10,7 @@ import 'package:get/get.dart'; | |||
| import 'authentication/bloc/authentication_bloc.dart'; | |||
| import 'data/repository/authentication_repository.dart'; | |||
| import 'presentation/screens/login/login_page.dart'; | |||
| import 'routes/routes.dart'; | |||
| import 'services/firebase_notification_service.dart'; | |||
| final GlobalKey<NavigatorState> globalNavigator = GlobalKey<NavigatorState>(); | |||
| @@ -43,9 +44,9 @@ class AppView extends StatefulWidget { | |||
| } | |||
| class _AppViewState extends State<AppView> { | |||
| final _navigatorKey = GlobalKey<NavigatorState>(); | |||
| // final _navigatorKey = GlobalKey<NavigatorState>(); | |||
| NavigatorState? get _navigator => _navigatorKey.currentState; | |||
| NavigatorState? get _navigator => globalNavigator.currentState; | |||
| @override | |||
| Widget build(BuildContext context) { | |||
| @@ -65,28 +66,32 @@ class _AppViewState extends State<AppView> { | |||
| // Theme.of(context).textTheme, | |||
| // ), | |||
| ), | |||
| navigatorKey: _navigatorKey, | |||
| navigatorKey: globalNavigator, | |||
| builder: (context, child) { | |||
| return BlocListener<AuthenticationBloc, AuthenticationState>( | |||
| listener: (context, state) { | |||
| switch (state.status) { | |||
| case AuthenticationStatus.authenticated: | |||
| FirebaseNotificationService.initService(); | |||
| // _navigator!.pushAndRemoveUntil<void>( | |||
| // TabbarScreen.route(), | |||
| // (route) => false, | |||
| // ); | |||
| Get.offAll(() => TabbarScreen()); | |||
| _navigator!.pushAndRemoveUntil<void>( | |||
| Routes.dashboard(), | |||
| (route) => false, | |||
| ); | |||
| // Get.offAll(() => TabbarScreen()); | |||
| break; | |||
| case AuthenticationStatus.unauthenticated: | |||
| FirebaseNotificationService.initService(); | |||
| // _navigator!.pushAndRemoveUntil<void>( | |||
| // LoginPage.route(), | |||
| // (route) => false, | |||
| // ); | |||
| Get.offAll(() => const LoginPage()); | |||
| _navigator!.pushAndRemoveUntil<void>( | |||
| Routes.login(), | |||
| (route) => false, | |||
| ); | |||
| // Get.offAll(() => const LoginPage()); | |||
| break; | |||
| default: | |||
| _navigator!.pushAndRemoveUntil<void>( | |||
| Routes.login(), | |||
| (route) => false, | |||
| ); | |||
| break; | |||
| } | |||
| }, | |||
| @@ -5,6 +5,7 @@ import 'dart:convert'; | |||
| import 'package:dio/adapter.dart'; | |||
| import 'package:dio/dio.dart'; | |||
| import 'package:dio/native_imp.dart'; | |||
| import 'package:farm_tpf/data/repository/user_repository.dart'; | |||
| import 'package:flutter_bloc/flutter_bloc.dart'; | |||
| import 'package:get/get.dart' as getx; | |||
| @@ -12,8 +13,10 @@ import '../../app.dart'; | |||
| import '../../authentication/bloc/authentication_bloc.dart'; | |||
| import '../../environment/app_config.dart'; | |||
| import '../../presentation/screens/login/login_page.dart'; | |||
| import '../../routes/routes.dart'; | |||
| import '../../utils/const_enum.dart'; | |||
| import '../../utils/local_storage.dart'; | |||
| import '../../utils/pref.dart'; | |||
| class DioProvider extends DioForNative { | |||
| factory DioProvider({ | |||
| @@ -119,14 +122,47 @@ class UnauthorizedInterceptor extends InterceptorsWrapper { | |||
| final checkUnauthorized = | |||
| err.type == DioErrorType.response && err.response?.statusCode == HttpStatus.unauthorized && !err.requestOptions.path.contains('authenticate'); | |||
| if (checkUnauthorized) { | |||
| LocalStorage.clearUserInfo(); | |||
| getx.Get.offAll(() => const LoginPage()); | |||
| BlocProvider.of<AuthenticationBloc>(globalNavigator.currentContext!).add( | |||
| const AuthenticationStatusChanged(AuthenticationStatus.unauthenticated), | |||
| ); | |||
| // LocalStorage.clearUserInfo(); | |||
| // getx.Get.offAll(() => const LoginPage()); | |||
| // BlocProvider.of<AuthenticationBloc>(globalNavigator.currentContext!).add( | |||
| // const AuthenticationStatusChanged(AuthenticationStatus.unauthenticated), | |||
| // ); | |||
| // globalNavigator.currentState?.pushAndRemoveUntil<void>( | |||
| // Routes.login(), | |||
| // (route) => false, | |||
| // ); | |||
| // BlocProvider.of<AuthenticationBloc>(globalNavigator.currentContext!).add(AuthenticationLogoutRequested()); | |||
| _logout(); | |||
| } | |||
| return handler.next(err); | |||
| } | |||
| _logout() async { | |||
| var pref = LocalPref(); | |||
| UserRepository _userRepository = UserRepository(); | |||
| globalNavigator.currentState?.pushAndRemoveUntil<void>( | |||
| Routes.login(), | |||
| (route) => false, | |||
| ); | |||
| if (globalNavigator.currentContext != null) { | |||
| BlocProvider.of<AuthenticationBloc>(globalNavigator.currentContext!).add(AuthenticationLogoutRequested()); | |||
| } | |||
| try { | |||
| String pushKey = await pref.getString(DATA_CONST.PUSH_KEY); | |||
| if (pushKey.isNotEmpty) { | |||
| _userRepository.deleteFcmToken(pushKey).then((value) {}).catchError((err) {}).whenComplete(() { | |||
| pref.saveString(DATA_CONST.TOKEN_KEY, ""); | |||
| pref.saveString(DATA_CONST.PUSH_KEY, ""); | |||
| pref.saveString(DATA_CONST.CURRENT_FULL_NAME, ""); | |||
| }); | |||
| } | |||
| } catch (e) { | |||
| pref.saveString(DATA_CONST.CURRENT_FULL_NAME, ""); | |||
| pref.saveString(DATA_CONST.TOKEN_KEY, ""); | |||
| pref.saveString(DATA_CONST.PUSH_KEY, ""); | |||
| } | |||
| } | |||
| } | |||
| // class DioProvider { | |||
| @@ -1,7 +1,11 @@ | |||
| import 'package:bloc/bloc.dart'; | |||
| import 'package:farm_tpf/main.dart'; | |||
| import 'package:farm_tpf/utils/const_enum.dart'; | |||
| import 'package:flutter/material.dart'; | |||
| import 'package:flutter_bloc/flutter_bloc.dart'; | |||
| import 'package:get/get.dart'; | |||
| import '../../../../authentication/bloc/authentication_bloc.dart'; | |||
| import '../../../../data/repository/repository.dart'; | |||
| import '../../../../utils/local_storage.dart'; | |||
| import '../../../../utils/validators.dart'; | |||
| @@ -51,6 +55,11 @@ class LoginCubit extends Cubit<LoginState> { | |||
| }) async { | |||
| try { | |||
| LocalStorage.saveUserInfo(responseUser, username); | |||
| BlocProvider.of<AuthenticationBloc>(globalNavigator.currentContext!).add( | |||
| const AuthenticationStatusChanged( | |||
| AuthenticationStatus.authenticated, | |||
| ), | |||
| ); | |||
| } catch (_) {} | |||
| emit( | |||
| LoginSuccess( | |||
| @@ -307,10 +307,12 @@ class _TabbarScreenState extends State<TabbarScreen> { | |||
| ))); | |||
| } | |||
| _showAlertCheckCropCode(String code) async { | |||
| _showAlertCheckCropCode(String qrCodeValue) async { | |||
| var repository = Repository(); | |||
| Get.defaultDialog(title: "Kiểm tra thông tin lô ....", middleText: "", content: CircularProgressIndicator()); | |||
| try { | |||
| Uri uri = Uri.parse(qrCodeValue); | |||
| String code = uri.pathSegments.isNotEmpty ? uri.pathSegments.last : ''; | |||
| if (code.startsWith('LO')) { | |||
| await repository.getPlotDetailByCode(code).then((value) { | |||
| print("ok"); | |||
| @@ -0,0 +1,6 @@ | |||
| class RouteName { | |||
| static const String home = '/'; | |||
| static const String welcomePage = '/welcome'; | |||
| static const String loginPage = '/login'; | |||
| static const String dashboardPage = '/dashboard'; | |||
| } | |||
| @@ -0,0 +1,60 @@ | |||
| import 'package:flutter/material.dart'; | |||
| import '../presentation/screens/login/login_page.dart'; | |||
| import '../presentation/screens/tabbar/tabbar.dart'; | |||
| import 'route_name.dart'; | |||
| class Routes { | |||
| static Route buildRoutes(RouteSettings settings) { | |||
| switch (settings.name) { | |||
| case RouteName.loginPage: | |||
| return buildRoute(settings, const LoginPage()); | |||
| case RouteName.dashboardPage: | |||
| return buildRoute(settings, TabbarScreen()); | |||
| default: | |||
| return _errorRoute(); | |||
| } | |||
| } | |||
| static Route login() { | |||
| return PageRouteBuilder( | |||
| pageBuilder: (context, animation, secondaryAnimation) => const LoginPage(), | |||
| transitionsBuilder: (context, animation, secondaryAnimation, child) { | |||
| return child; | |||
| }, | |||
| transitionDuration: const Duration(seconds: 0), | |||
| ); | |||
| } | |||
| static Route dashboard() { | |||
| return PageRouteBuilder( | |||
| pageBuilder: (context, animation, secondaryAnimation) => TabbarScreen(), | |||
| transitionsBuilder: (context, animation, secondaryAnimation, child) { | |||
| return child; | |||
| }, | |||
| transitionDuration: const Duration(seconds: 0), | |||
| ); | |||
| } | |||
| static Route homeRoute(RouteSettings settings) { | |||
| return buildRoutes(settings); | |||
| } | |||
| static Route _errorRoute() { | |||
| return MaterialPageRoute(builder: (_) { | |||
| return const Scaffold( | |||
| body: Center( | |||
| child: Text(''), | |||
| ), | |||
| ); | |||
| }); | |||
| } | |||
| static MaterialPageRoute buildRoute(RouteSettings settings, Widget builder) { | |||
| return MaterialPageRoute( | |||
| settings: settings, | |||
| builder: (BuildContext context) => builder, | |||
| ); | |||
| } | |||
| } | |||