【问题标题】:Flutter : How to set Checkbox and Switch color in the ThemeFlutter:如何在 Theme 中设置 Checkbox 和 Switch 颜色
【发布时间】:2022-06-30 05:58:34
【问题描述】:

在 VSCode 中,Flutter 向我抱怨“'accentColor' is deprecated and shouldn't be used. Use colorScheme.secondary instead. For more information, consult the migration guide at https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. This feature was deprecated after v2.3.0-0.1.pre.. Try replacing the use of the deprecated member with the replacement.

迁移指南建议这样做:

迁移前的代码:

MaterialApp(
  theme: ThemeData(accentColor: myColor),
  // ...
);

迁移后的代码:

final ThemeData theme = ThemeData();
MaterialApp(
  theme: theme.copyWith(
    colorScheme: theme.colorScheme.copyWith(secondary: myColor),
  ),
  //...
)

所以我做了这个改变:

   Widget build(BuildContext context) {
+    final ThemeData theme = ThemeData();
     return MaterialApp(
       title: 'Title',
-      theme: ThemeData(
-        primaryColor: Color.fromRGBO(95, 53, 90, 1),
-        accentColor: Color.fromRGBO(76, 41, 71, 1)
+      theme: theme.copyWith(
+        colorScheme: theme.colorScheme.copyWith(
+          primary: Color.fromRGBO(95, 53, 90, 1),
+          secondary: Color.fromRGBO(76, 41, 71, 1),
         ),
+      ),
       home: MyApp()
     );
   }

但是现在我的复选框和开关已经从我的原色变成了蓝色。

我还能在主题中设置复选框的颜色吗?

【问题讨论】:

标签: flutter checkbox themes


【解决方案1】:

Checkbox 以及其他一些小部件仍然没有迁移到 Material 3 方案。已经有一个未解决的问题#104446。拉取请求 #97972 似乎可以解决这个问题。

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 2020-01-11
    • 2019-04-30
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多