| import 'authentication/bloc/authentication_bloc.dart'; | import 'authentication/bloc/authentication_bloc.dart'; | ||||
| import 'data/repository/authentication_repository.dart'; | import 'data/repository/authentication_repository.dart'; | ||||
| import 'presentation/screens/login/login_page.dart'; | import 'presentation/screens/login/login_page.dart'; | ||||
| import 'routes/routes.dart'; | |||||
| import 'services/firebase_notification_service.dart'; | import 'services/firebase_notification_service.dart'; | ||||
| final GlobalKey<NavigatorState> globalNavigator = GlobalKey<NavigatorState>(); | final GlobalKey<NavigatorState> globalNavigator = GlobalKey<NavigatorState>(); | ||||
| } | } | ||||
| class _AppViewState extends State<AppView> { | class _AppViewState extends State<AppView> { | ||||
| final _navigatorKey = GlobalKey<NavigatorState>(); | |||||
| // final _navigatorKey = GlobalKey<NavigatorState>(); | |||||
| NavigatorState? get _navigator => _navigatorKey.currentState; | |||||
| NavigatorState? get _navigator => globalNavigator.currentState; | |||||
| @override | @override | ||||
| Widget build(BuildContext context) { | Widget build(BuildContext context) { | ||||
| // Theme.of(context).textTheme, | // Theme.of(context).textTheme, | ||||
| // ), | // ), | ||||
| ), | ), | ||||
| navigatorKey: _navigatorKey, | |||||
| navigatorKey: globalNavigator, | |||||
| builder: (context, child) { | builder: (context, child) { | ||||
| return BlocListener<AuthenticationBloc, AuthenticationState>( | return BlocListener<AuthenticationBloc, AuthenticationState>( | ||||
| listener: (context, state) { | listener: (context, state) { | ||||
| switch (state.status) { | switch (state.status) { | ||||
| case AuthenticationStatus.authenticated: | case AuthenticationStatus.authenticated: | ||||
| FirebaseNotificationService.initService(); | FirebaseNotificationService.initService(); | ||||
| // _navigator!.pushAndRemoveUntil<void>( | |||||
| // TabbarScreen.route(), | |||||
| // (route) => false, | |||||
| // ); | |||||
| Get.offAll(() => TabbarScreen()); | |||||
| _navigator!.pushAndRemoveUntil<void>( | |||||
| Routes.dashboard(), | |||||
| (route) => false, | |||||
| ); | |||||
| // Get.offAll(() => TabbarScreen()); | |||||
| break; | break; | ||||
| case AuthenticationStatus.unauthenticated: | case AuthenticationStatus.unauthenticated: | ||||
| FirebaseNotificationService.initService(); | 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; | break; | ||||
| default: | default: | ||||
| _navigator!.pushAndRemoveUntil<void>( | |||||
| Routes.login(), | |||||
| (route) => false, | |||||
| ); | |||||
| break; | break; | ||||
| } | } | ||||
| }, | }, |
| import 'package:dio/adapter.dart'; | import 'package:dio/adapter.dart'; | ||||
| import 'package:dio/dio.dart'; | import 'package:dio/dio.dart'; | ||||
| import 'package:dio/native_imp.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:flutter_bloc/flutter_bloc.dart'; | ||||
| import 'package:get/get.dart' as getx; | import 'package:get/get.dart' as getx; | ||||
| import '../../authentication/bloc/authentication_bloc.dart'; | import '../../authentication/bloc/authentication_bloc.dart'; | ||||
| import '../../environment/app_config.dart'; | import '../../environment/app_config.dart'; | ||||
| import '../../presentation/screens/login/login_page.dart'; | import '../../presentation/screens/login/login_page.dart'; | ||||
| import '../../routes/routes.dart'; | |||||
| import '../../utils/const_enum.dart'; | import '../../utils/const_enum.dart'; | ||||
| import '../../utils/local_storage.dart'; | import '../../utils/local_storage.dart'; | ||||
| import '../../utils/pref.dart'; | |||||
| class DioProvider extends DioForNative { | class DioProvider extends DioForNative { | ||||
| factory DioProvider({ | factory DioProvider({ | ||||
| final checkUnauthorized = | final checkUnauthorized = | ||||
| err.type == DioErrorType.response && err.response?.statusCode == HttpStatus.unauthorized && !err.requestOptions.path.contains('authenticate'); | err.type == DioErrorType.response && err.response?.statusCode == HttpStatus.unauthorized && !err.requestOptions.path.contains('authenticate'); | ||||
| if (checkUnauthorized) { | 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); | 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 { | // class DioProvider { |
| import 'package:bloc/bloc.dart'; | 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/material.dart'; | ||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | |||||
| import 'package:get/get.dart'; | import 'package:get/get.dart'; | ||||
| import '../../../../authentication/bloc/authentication_bloc.dart'; | |||||
| import '../../../../data/repository/repository.dart'; | import '../../../../data/repository/repository.dart'; | ||||
| import '../../../../utils/local_storage.dart'; | import '../../../../utils/local_storage.dart'; | ||||
| import '../../../../utils/validators.dart'; | import '../../../../utils/validators.dart'; | ||||
| }) async { | }) async { | ||||
| try { | try { | ||||
| LocalStorage.saveUserInfo(responseUser, username); | LocalStorage.saveUserInfo(responseUser, username); | ||||
| BlocProvider.of<AuthenticationBloc>(globalNavigator.currentContext!).add( | |||||
| const AuthenticationStatusChanged( | |||||
| AuthenticationStatus.authenticated, | |||||
| ), | |||||
| ); | |||||
| } catch (_) {} | } catch (_) {} | ||||
| emit( | emit( | ||||
| LoginSuccess( | LoginSuccess( |
| ))); | ))); | ||||
| } | } | ||||
| _showAlertCheckCropCode(String code) async { | |||||
| _showAlertCheckCropCode(String qrCodeValue) async { | |||||
| var repository = Repository(); | var repository = Repository(); | ||||
| Get.defaultDialog(title: "Kiểm tra thông tin lô ....", middleText: "", content: CircularProgressIndicator()); | Get.defaultDialog(title: "Kiểm tra thông tin lô ....", middleText: "", content: CircularProgressIndicator()); | ||||
| try { | try { | ||||
| Uri uri = Uri.parse(qrCodeValue); | |||||
| String code = uri.pathSegments.isNotEmpty ? uri.pathSegments.last : ''; | |||||
| if (code.startsWith('LO')) { | if (code.startsWith('LO')) { | ||||
| await repository.getPlotDetailByCode(code).then((value) { | await repository.getPlotDetailByCode(code).then((value) { | ||||
| print("ok"); | print("ok"); |
| class RouteName { | |||||
| static const String home = '/'; | |||||
| static const String welcomePage = '/welcome'; | |||||
| static const String loginPage = '/login'; | |||||
| static const String dashboardPage = '/dashboard'; | |||||
| } |
| 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, | |||||
| ); | |||||
| } | |||||
| } |