尝试将背景颜色的默认值传递给构造函数,但这些是弹出的错误消息。 我试过使用 Colors.red[200]!和 Colors.red.shade200,但没有一个成功。
import "package:flutter/material.dart";
class Category {
final String id;
final Color bgColor;
final String title;
Category(
{required this.id, required this.title, this.bgColor = Colors.red[200]});
}
答案 0 :(得分:1)
改成这个。
this.bgColor = const Color(0xFFEF9A9A)
错误是因为,就像错误所说 - 可选参数的默认值必须是常量。 Colors.red[200]
不是常量,而 const Color(0xFFEF9A9A)
是。
答案 1 :(得分:1)
您应该尝试以下答案:
class Category {
Category({
@required this.id,
@required this.title,
this.bgColor: const Color(0xFFEF9A9A),
});
final String id;
final Color bgColor;
final String title;
}