【问题标题】:Does not change the icon after switching the theme切换主题后不改变图标
【发布时间】:2022-01-10 20:41:01
【问题描述】:

有一个 animated_theme_switcher 打包并分配了一个代码来改变切换后的图标,但它没有改变..需要什么代码? 请帮助..我将不胜感激 我的代码:

                                  Container(
                                    margin: const EdgeInsets.only(
                                    ),
                                  child: ThemeSwitcher(
                                    clipper: const ThemeSwitcherCircleClipper(),
                                    builder: (context)
                                    {
                                      return IconButton(
                                        onPressed: () async {
                                          var themeName =
                                          ThemeModelInheritedNotifier
                                              .of(context)
                                              .theme
                                              .brightness ==
                                              Brightness.light
                                              ? 'dark'
                                              : 'light';
                                          var service = await ThemeService.instance
                                            ..save(themeName);
                                          var theme = service.getByName(themeName);
                                          ThemeSwitcher.of(context).changeTheme(theme: theme);
                                        },
                                        icon: Icon(ThemeModelInheritedNotifier
                                            .of(context)
                                            .theme == Brightness.light ? Icons.light_mode_outlined : Icons.dark_mode_outlined),
                                      );
                                    },
                                  ),
                                ),

【问题讨论】:

    标签: flutter animation button icons themes


    【解决方案1】:

    使用 ThemeSwitchingArea 小部件包裹您想要让它们切换的屏幕,如下例所示:

    ThemeSwitchingArea(
            child: Builder(builder: (context)
        {
          return IconButton(
            onPressed: () async {
              var themeName =
              ThemeModelInheritedNotifier
                  .of(context)
                  .theme
                  .brightness ==
                  Brightness.light
                  ? 'dark'
                  : 'light';
              var service = await ThemeService.instance
                ..save(themeName);
              var theme = service.getByName(themeName);
              ThemeSwitcher.of(context).changeTheme(theme: theme);
            },
            icon: Icon(ThemeModelInheritedNotifier
                .of(context)
                .theme == Brightness.light ? Icons.light_mode_outlined : Icons.dark_mode_outlined),
          );
        },);
    

    【讨论】:

    • 我不太明白 where( ... 上面,我从它开始的地方添加了代码
    • 想通了 .of(context) .theme .brightness == Brightness.light ? ...
    • 请完整学习 animated_theme_switcher 文档
    猜你喜欢
    • 2021-04-26
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 2021-09-10
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多