You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.1KB

  1. import 'package:farm_tpf/presentation/screens/home/view/home_page.dart';
  2. import 'package:farm_tpf/presentation/screens/login/view/login_page.dart';
  3. import 'package:farm_tpf/presentation/screens/splash/view/splash_page.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_bloc/flutter_bloc.dart';
  6. import 'authentication/bloc/authentication_bloc.dart';
  7. import 'data/repository/authentication_repository.dart';
  8. class App extends StatelessWidget {
  9. const App({
  10. Key key,
  11. @required this.authenticationRepository,
  12. }) : assert(authenticationRepository != null),
  13. super(key: key);
  14. final AuthenticationRepository authenticationRepository;
  15. @override
  16. Widget build(BuildContext context) {
  17. return RepositoryProvider.value(
  18. value: authenticationRepository,
  19. child: BlocProvider(
  20. create: (_) => AuthenticationBloc(
  21. authenticationRepository: authenticationRepository,
  22. ),
  23. child: AppView(),
  24. ),
  25. );
  26. }
  27. }
  28. class AppView extends StatefulWidget {
  29. @override
  30. _AppViewState createState() => _AppViewState();
  31. }
  32. class _AppViewState extends State<AppView> {
  33. final _navigatorKey = GlobalKey<NavigatorState>();
  34. NavigatorState get _navigator => _navigatorKey.currentState;
  35. @override
  36. Widget build(BuildContext context) {
  37. return MaterialApp(
  38. navigatorKey: _navigatorKey,
  39. builder: (context, child) {
  40. return BlocListener<AuthenticationBloc, AuthenticationState>(
  41. listener: (context, state) {
  42. switch (state.status) {
  43. case AuthenticationStatus.authenticated:
  44. _navigator.pushAndRemoveUntil<void>(
  45. HomePage.route(),
  46. (route) => false,
  47. );
  48. break;
  49. case AuthenticationStatus.unauthenticated:
  50. _navigator.pushAndRemoveUntil<void>(
  51. LoginPage.route(),
  52. (route) => false,
  53. );
  54. break;
  55. default:
  56. break;
  57. }
  58. },
  59. child: child,
  60. );
  61. },
  62. onGenerateRoute: (_) => SplashPage.route(),
  63. );
  64. }
  65. }