Home > OS >  Flutter BLoC I need Theme for init value
Flutter BLoC I need Theme for init value

Time:01-17

Inside my app.dart I have my BlocProviders:

  return MultiBlocProvider(
        providers: [      
           BlocProvider(
            create: (context) => SysUiCubit(context: context, "currentTheme: currentTheme"),
          ), child: MaterialApp(
      theme: AppTheme.lightTheme.copyWith(brightness: Brightness.light),
      darkTheme: AppTheme.darkTheme.copyWith(brightness: Brightness.dark),
      themeMode: themeService.getSysMode ? ThemeMode.system : (themeService.getDarkMode ? ThemeMode.dark : ThemeMode.light),

and after my BlocProviders I have the MaterialApp. The problem is: For the SysUiCubit I need the state of the current theme like I tried to visualize inside the quotation mark. The easiest way would be to have the MaterialApp before my BlocProvider, but I think this isn't possible - isn't it? To understand the use case: Inside my SysUiCubit I have a

  factory SysUiState.initial(BuildContext context){
    return  const SysUiState(systemUiOverlayStyle: SystemUiOverlayStyle.dark);
  }

Where I want to add an Overlaystyle dependent from the current theme. So as you can guess, to realize that inside the init state, the theme mode must already be set. Any advice how I can do this?

CodePudding user response:

I believe you added systemUiOverlayStyle as a required parameter I would suggest trying this solution:

  1. in SysUiCubit constractor make systemUiOverlayStyle an optional parameter (no need to pass "currentTheme: currentTheme")
  2. in SysUiCubit contractor check if systemUiOverlayStyle parameter is null, if it is full then check the shared preferences if the app stored the last theme (dark or light), if the shareprefrance is empty (mainly because the user opened the app for the first time) force a default theme based on the system theme (there are packages or system functions that retrieves the active theme).
  3. whenever you want to change the theme, call bloc controller and update the value (systemUiOverlayStyle).
  4. no need to change the order of the current widgets

let me know if you got my points.

  • Related