我有一個名為 CircleIcon 的小部件,我試圖通過命名引數添加 bgcolor。
class CircleIcon extends StatelessWidget {
final IconData icon;
final double iconSize;
final Color bgColor;
final VoidCallback onPressed;
const CircleIcon({
Key? key,
required this.icon,
this.iconSize = 25.0,
this.bgColor = Colors.grey[200],
required this.onPressed,
}) : super(key: key);
作為默認的 bg color ,我試圖保留 Colors.grey[200],但是A value of type 'Color?' can't be assigned to a variable of type 'Color'.
如果我將其更改為 Colors.grey,則會出現錯誤,那么它就可以作業了。我將為 Colors.grey[200] 使用什么型別?
uj5u.com熱心網友回復:
試試下面的代碼,希望對你有幫助。
如果你使用簡單的Colors.grey[200]不是constatntColors.grey就是接受,所以我使用了 const 值,比如 constColor(0xFFEEEEEE)
class CircleIcon extends StatelessWidget {
final IconData icon;
final double iconSize;
final Color bgColor;
final VoidCallback onPressed;
const CircleIcon({
Key? key,
required this.icon,
this.iconSize = 25.0,
this.bgColor = const Color(0xFFEEEEEE),
required this.onPressed,
}) : super(key: key);
}
也參考我的回答here。
參考顫振顏色here
uj5u.com熱心網友回復:
問題在于空值。您需要使用可為空的型別定義 bgColor
final Color? bgColor;
uj5u.com熱心網友回復:
您可以使用 (!) 運算子:
this.bgColor = Colors.grey[200]!
或者
shade在顏色上使用:
this.bgColor = Colors.grey.shade200
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/479916.html
標籤:扑
