如何在 flutter 中将颜色阴影应用于自定义小部件的 Color 属性?

问题描述 投票:0回答:1

我想将 Colors.red.shade900 的默认颜色设置为我的自定义小部件,但它显示错误

这是我的自定义课程:

class ChartHeader extends StatelessWidget {
  final String title;
  final Color backgroundColor;
  const ChartHeader({
    Key? key,
    required this.title,
    this.backgroundColor = Colors.red.shade900,
  }) : super(key: key);

我应该为 Colors.red.shade900 使用什么数据类型?

flutter
1个回答
0
投票

这不是错误的数据类型,但

Colors.red.shade900
的值不是恒定的。

您可以使用等效的常量值:

Color(0xFFB71C1C)
,如下所示:

class ChartHeader extends StatelessWidget {
  final String title;
  final Color backgroundColor;
  const ChartHeader({
    Key? key,
    required this.title,
    this.backgroundColor = Color(0xFFB71C1C),
  }) : super(key: key);

您可以通过按

ctrl + click-on-theClass

检查 Dart 中任何颜色的值
© www.soinside.com 2019 - 2024. All rights reserved.