首页 > 解决方案 > 口音颜色已弃用颤振 2.5

问题描述

我使用accentColor属性,我知道这个属性已被弃用。如何使用新方法创建特定的强调色?

代码是:

 theme: ThemeData(
            accentColor: const Color(accentColor),
),

标签: flutter

解决方案


用这个

 theme: ThemeData(
      colorScheme:  Theme.of(context).colorScheme.copyWith(secondary: Color(accentColor))
),

有关更多信息,请查看他们的文档

https://flutter.dev/docs/release/break-changes/theme-data-accent-properties


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

迁移前的代码:

Color myColor = Theme.of(context).accentColor;

迁移后的代码:

Color myColor = Theme.of(context).colorScheme.secondary;

推荐阅读