skip to Main Content

I have a problem here.

I am using flutter BloC .

I Have Two block need to be applied to the app (Theme and Language).

I used MultiBlocProvider with Builder to clear the code as possible.

The problem is when I press the buttons in Localization Screen & The Theme Screen The App NOT Re-Build

but when I restart the app, I found the state I pressed is stored in shared preference

Example The Current State is dark Theme, I press The Light Theme Button, the app is still in dark theme, When I re-started the app, It’s is opened in Light Theme.

How can I Solve this problem

I Uploaded the code to GitHub on the following Link

https://github.com/MaxJan2010/flutter_bloc_starting_project

Can any body help please

Thanks in Advance

3

Answers


  1. Wrap theme with ThemeBlocBuilder and wrap locale with LocaleBlocBuilder this will make your screen updates as your state changes

    Login or Signup to reply.
  2. You can make use of the BlocBuilder widgets more effectively.
    Below is the code:

    MultiBlocProvider(
            providers: [
              BlocProvider(
                create: (context) => AppConnectivityBloc(),
              ),
              BlocProvider(
                create: (context) =>
                    AppLocalizationBloc()..add(AppLocalizationInitialEvent()),
              ),
              BlocProvider(
                create: (context) => AppThemeBloc()..add(AppThemeInitialEvent()),
              ),
            ],
            child: BlocBuilder<AppLocalizationBloc, AppLocalizationState>(
              builder: (context, langState) {
                var lang = langState is AppLocalizationChangeState
                    ? langState.languageCode
                    : 'en';
                return BlocBuilder<AppThemeBloc, AppThemeState>(
                  builder: (context, themeState) {
                    var theme = themeState is AppThemeChangeState
                        ? themeState.appThemeType
                        : 'light';
                    return MaterialApp(
                      debugShowCheckedModeBanner: false,
                      theme:
                          theme == 'light' ? ThemeData.light() : ThemeData.dark(),
                      home: const MyHomeScreen(),
                      locale: Locale(lang!),
                      supportedLocales: const [
                        Locale('en'),
                        Locale('ar'),
                      ],
                      localizationsDelegates: [
                        AppLocalizations.delegate,
                        GlobalMaterialLocalizations.delegate, // For Android
                        GlobalWidgetsLocalizations
                            .delegate, // For Widget Directions
                        GlobalCupertinoLocalizations.delegate, // For IOS
                      ],
                      localeResolutionCallback: (deviceLocale, supportedLocales) {
                        for (var locale in supportedLocales) {
                          if (deviceLocale != null) {
                            if (deviceLocale.languageCode == locale.languageCode) {
                              return deviceLocale;
                            }
                          }
                        }
                        return supportedLocales.first;
                      },
                    );
                  },
                );
              },
            ));
    
    Login or Signup to reply.
  3. use BlocBuilder above the MaterialApp Widget

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search