csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 97 forks source link

How to implement "refreshListenable" and "redirect" using Flutter Bloc? #148

Closed Rammehar closed 3 years ago

Rammehar commented 3 years ago

I am using @felangel bloc library for state management? I don't know how to implement "refreshListenable" with this. and also how to use "redirects"

void main() async {
  configureApp();
  WidgetsFlutterBinding.ensureInitialized();
  //for injectable package and get_it
  configureInjection('development');
  //bloc observer helps us to debug our bloc and cubit
  Bloc.observer = AppBlocObserver();
  runApp(
    MultiBlocProvider(
      providers: [
        BlocProvider<AuthBloc>(
          create: (_) => getIt<AuthBloc>()..add(const AuthCheckRequested()),
        ),
      ],
      child: SMSApp(),
    ),
  );
}
MaterialApp.router(
        debugShowCheckedModeBanner: false,
        title: 'SMS',
        theme: SMSTheme.lightThemeData,
        darkTheme: SMSTheme.darkThemeData,
        themeMode: ThemeMode.system,
        routeInformationParser: myGoRouter.routeInformationParser,
        routerDelegate: myGoRouter.routerDelegate,
        // routerDelegate: AutoRouterDelegate(
        //   _appRouter,
        //   navigatorObservers: () => [RouteObserver()],
        // ),
        // routeInformationParser: _appRouter.defaultRouteParser(),
      ),

and here is my go router:

final myGoRouter = GoRouter(
  // urlPathStrategy: UrlPathStrategy.path,
  debugLogDiagnostics: true,
  initialLocation: '/login',
  //refreshListenable: ,//how to add authBloc here????
  redirect: (state) {
    final goingToLogin = state.location == '/login';
    print("IAM $goingToLogin");
    // BlocListener<AuthBloc, AuthState>(
    //   listener: (context, state) {
    //     state.maybeWhen(
    //       orElse: () {},
    //       unauthenticated: () {
    //         print("IAM2 $goingToLogin");
    //
    //         // the user is not logged in and not headed to /login, they need to login
    //         if (!goingToLogin) return '/login';
    //       },
    //       authenticated: (User user) {
    //         print("IAM3 $goingToLogin");
    //
    //         // the user is logged in and headed to /login, no need to login again
    //         if (goingToLogin) return '/';
    //       },
    //     );
    //   },
    // );
    return null;
  },
  routes: [
    GoRoute(
      name: 'login',
      path: '/login',
      pageBuilder: (context, state) => NoTransitionPage(
        key: state.pageKey,
        child: const LoginScreen(),
      ),
    ),
    GoRoute(
      name: "dashboard",
      path: '/',
      pageBuilder: (context, state) => NoTransitionPage(
        key: state.pageKey,
        child: const DashboardScreen(),
      ),
    ),
  ],
  errorPageBuilder: (BuildContext context, GoRouterState state) {
    return const MaterialPage<void>(
      child: Scaffold(
        body: Center(
          child: Text('Oops Error Page'),
        ),
      ),
    );
  },
);

class NoTransitionPage<T> extends CustomTransitionPage<T> {
  const NoTransitionPage({required Widget child, LocalKey? key})
      : super(transitionsBuilder: _transitionsBuilder, child: child, key: key);

  static Widget _transitionsBuilder(
          BuildContext context,
          Animation<double> animation,
          Animation<double> secondaryAnimation,
          Widget child) =>
      child;
}