【问题标题】:How I can change theme with a switch in flutter using bloc?我如何使用 bloc 在颤动中切换主题?
【发布时间】:2021-06-25 17:26:18
【问题描述】:

我是 Flutter 和 bloc 的新手。我正在制作一个带有 bloc 状态管理的应用程序,它可以随着系统主题的变化而改变主题。现在它工作正常,但我需要可以覆盖主题的开关。我该如何实施?我正在通过观看 youtube 教程制作这个应用程序。 无论如何要创建可以更改主题的开关。

主题肘

class ThemeCubit extends Cubit<ThemeState> {
  ThemeCubit() : super(ThemeState(themeMode: ThemeMode.light)) {
    updateAppTheme();
  }

  void updateAppTheme() {
    final Brightness currentBrightness = AppTheme.currentSystemBrightness;
    currentBrightness == Brightness.light
        ? _setTheme(ThemeMode.light)
        : _setTheme(ThemeMode.dark);
  }

  void _setTheme(ThemeMode themeMode) {
    AppTheme.setStatusBarAndNavigationBarColor(themeMode);
    emit(ThemeState(themeMode: themeMode));
  }
}

主题状态

class ThemeState {
  final ThemeMode themeMode;

  ThemeState({@required this.themeMode});
}

这是main.dart的代码

void main() {
  Bloc.observer = AppBlocObserver();
  runApp(DevicePreview(
    builder: (context) => App(),
    enabled: false,
    plugins: [
      const ScreenshotPlugin(),
    ],
  ));
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<ThemeCubit>(
          create: (context) => ThemeCubit(),
        ),
      ],
      child: MchatsApp(),
    );
  }
}

class MchatsApp extends StatefulWidget {
  const MchatsApp({
    Key key,
  }) : super(key: key);

  @override
  _MchatsAppState createState() => _MchatsAppState();
}

class _MchatsAppState extends State<MchatsApp> with WidgetsBindingObserver {
  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangePlatformBrightness() {
    context.read<ThemeCubit>().updateAppTheme();
    super.didChangePlatformBrightness();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        return OrientationBuilder(
          builder: (context, orientation) {
            SizerUtil().init(constraints, orientation);

            return MaterialApp(
              locale: DevicePreview.locale(context),
              builder: DevicePreview.appBuilder,
              title: Strings.appTitle,
              theme: AppTheme.lightTheme,
              darkTheme: AppTheme.darkTheme,
              themeMode: context.select(
                  (ThemeCubit themeCubit) => themeCubit.state.themeMode),
              debugShowCheckedModeBanner: false,
              initialRoute: AppRouter.root,
              onGenerateRoute: AppRouter.onGenerateRoute,
            );
          },
        );
      },
    );
  }
}

【问题讨论】:

    标签: flutter dart bloc flutter-bloc cubit


    【解决方案1】:

    是的,你可以

    在 Switch Widget 的 onChanged 函数中调用你的 cubit 中的 updateAppTheme() 函数

    context.read<ThemeCubit>().updateAppTheme();
    
    Builder(
    builder:(context){
    bool isDark= context.select(
                      (ThemeCubit themeCubit) => themeCubit.state.themeMode)==ThemeMode.dark?true:false;
    return Switch(value: isDark, onChanged: (value) {
    context.read<ThemeCubit>().updateAppTheme();}
    );
    })
    

    【讨论】:

    • 它不工作。 Switch(activeColor: Colors.black, value: false, onChanged: (value) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text('Theme Change'), duration: Duration(milliseconds: 700), )) ; setState(() { context.read().updateAppTheme(); _switchValue = value; }); }, ),
    • onChanged 函数的值始终为 false,因为它没有状态
    • 等我为你的问题写具体的代码
    • 它显示了这一点,但主题没有改变。 I/flutter (5260): onChange -- ThemeCubit, Change { currentState: 'ThemeState' 实例, nextState: 'ThemeState' 实例}
    • @islamakhrarov 我试过你的 sn-p 但切换器正在切换,但它就像被禁用一样。能不能把Counter UI的整个代码提供一个开关,会更清楚明白
    猜你喜欢
    • 2020-07-08
    • 2021-06-08
    • 2021-04-08
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2019-08-17
    • 1970-01-01
    相关资源
    最近更新 更多