【问题标题】:Flutter: ColorScheme.secondary never applied to set accent colorFlutter:ColorScheme.secondary 从未应用于设置强调色
【发布时间】:2022-06-23 14:25:07
【问题描述】:

我的代码 - 使用已弃用的 accentColor 属性设置强调色,它可以工作,应用红色:

return VersionBanner(
      text: "DEV",
      visible: globals.isDev,
      location: BannerLocation.bottomEnd,
      child: MaterialApp(
        title: 'MyApp',
        theme: ThemeData(
          accentColor: Colors.red,
          appBarTheme: AppBarTheme(
            elevation: 0,
            backgroundColor: Colors.white,
            foregroundColor: Colors.black,
            systemOverlayStyle: overlayStyle,
          ),
          scaffoldBackgroundColor: Colors.white,
          primaryColor: Colors.white,
          textTheme: AppTheme.textTheme,
          pageTransitionsTheme: PageTransitionsTheme(builders: {
            TargetPlatform.android: ZoomPageTransitionsBuilder(),
            TargetPlatform.iOS: CupertinoPageTransitionsBuilder()
          }),
        ),
        home: globals.isLogged ? HomePage() : LoginPage(),
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
          DefaultCupertinoLocalizations.delegate
        ],
        supportedLocales: [
          const Locale('pl', 'PL'),
        ],
        routes: routes,
      ),
    );

我迁移到ColorScheme这样调用:

return VersionBanner(
      text: "DEV",
      visible: globals.isDev,
      location: BannerLocation.bottomEnd,
      child: MaterialApp(
        title: 'MyApp',
        theme: ThemeData(
          appBarTheme: AppBarTheme(
            elevation: 0,
            backgroundColor: Colors.white,
            foregroundColor: Colors.black,
            systemOverlayStyle: overlayStyle,
          ),
          scaffoldBackgroundColor: Colors.white,
          primaryColor: Colors.white,
          textTheme: AppTheme.textTheme,
          pageTransitionsTheme: PageTransitionsTheme(builders: {
            TargetPlatform.android: ZoomPageTransitionsBuilder(),
            TargetPlatform.iOS: CupertinoPageTransitionsBuilder()
          }), colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.red),
        ),
        home: globals.isLogged ? HomePage() : LoginPage(),
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
          DefaultCupertinoLocalizations.delegate
        ],
        supportedLocales: [
          const Locale('pl', 'PL'),
        ],
        routes: routes,
      ),
    );

但是在这种情况下从不应用强调色,无论设置什么辅助色,强调色始终是蓝色。所以 - 不推荐使用的方法工作得很好,新方法不行。

有什么想法吗?

【问题讨论】:

    标签: flutter themes deprecation-warning accent-color


    【解决方案1】:

    如果您要寻找 RadioCheckbox 的颜色变化,那么它会从 toggleableActiveColor 主题属性中选择强调色。

    您需要像这样在主题中使用该属性。

        theme: ThemeData(
         //...
          toggleableActiveColor : Colors.red
        //...
        home: ...,
        localizationsDelegates: [
          //...
        ],
        supportedLocales: [
          const Locale('pl', 'PL'),
        ],
        routes: routes,
      ),
    

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多