用集团切换主题。我在做对吗?

发布于 2025-01-25 20:16:03 字数 1357 浏览 1 评论 0原文

您好,我试图用Bloc更改主题。但不确定这是否是最佳实践。

这是我的switch_theme_cubit.dart

class ThemeCubit extends Cubit<ThemeData> {
  ThemeCubit() : super(AppTheme.lightTheme);

  void switchTheme() {
    state == AppTheme.lightTheme
        ? emit(AppTheme.darkTheme)
        : emit(AppTheme.lightTheme);
  }
}

and 材料 main.dart的一部分,

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(create: (context) => InternetConnectivityCubit()),
        BlocProvider(create: (context) => CounterCubit()),
        BlocProvider(create: (context) => ThemeCubit())
      ],
      child: BlocBuilder<ThemeCubit, ThemeData>(
        builder: (context, state) {
          return MaterialApp(
            title: Strings.appTitle,
            theme: state,
            debugShowCheckedModeBanner: false,
            initialRoute: AppRouter.homeScreen,
            onGenerateRoute: AppRouter.onGenerateRoute,
          );
        },
      ),
    );
  }
}

看起来像 this (readme中GIF的第一部分)。我在做对吗?

Hello I am trying to change theme with BloC. But not sure if it is best practice.

Here is my switch_theme_cubit.dart

class ThemeCubit extends Cubit<ThemeData> {
  ThemeCubit() : super(AppTheme.lightTheme);

  void switchTheme() {
    state == AppTheme.lightTheme
        ? emit(AppTheme.darkTheme)
        : emit(AppTheme.lightTheme);
  }
}

And MaterialApp part of main.dart

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(create: (context) => InternetConnectivityCubit()),
        BlocProvider(create: (context) => CounterCubit()),
        BlocProvider(create: (context) => ThemeCubit())
      ],
      child: BlocBuilder<ThemeCubit, ThemeData>(
        builder: (context, state) {
          return MaterialApp(
            title: Strings.appTitle,
            theme: state,
            debugShowCheckedModeBanner: false,
            initialRoute: AppRouter.homeScreen,
            onGenerateRoute: AppRouter.onGenerateRoute,
          );
        },
      ),
    );
  }
}

It looks like this (the first part of the gif in the readme). Am I doing right ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2025-02-01 20:16:03

是的,从技术上讲,这是绝对正确的。

我建议您将您的模式变成三州。轻,黑暗和系统默认。

当我将设备设置为Dark Mode时,我希望所有应用程序默认情况下使用DAMM模式。我不想打开所有37个应用程序并找到设置并单独切换。默认值应该是,如果我没有明确设置它,它将从我在系统设置中设置的值中获取值。

Yes, technically, this is absolutely correct.

I would suggest you make your mode a three-state. Light, Dark and System Default.

When I set my device to dark mode, I want all my apps to go to dark mode by default. I don't want to open all 37 apps and find the setting and switch them individually. The default should be that if I have not set it explicitely, it will take the value from what I have set in my system settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文