flutter - Flutter 中的颜色与颜色
问题描述
我正在像这样在颤振中编写一个函数
Expanded playButton({Color colorName, int buttonNumber, int soundNumber}) {
return Expanded(
child: FlatButton(
color: colorName,
onPressed: () {
final player = AudioCache();
player.play('note$soundNumber.wav');
},
child: Text(
'> PLAY $buttonNumber',
style: TextStyle(
fontSize: 35.0,
color: Colors.white,
),
),
),
);
}
为什么参数应该是Color colorName而不是Colors colorName。当我们使用color: Colors.teal来定义颜色时。
解决方案
我认为该类Colors
是该类的程序员友好包装器Color
。正如您在Colors
该类的源代码中看到的:
class Colors {
// This class is not meant to be instatiated or extended; this constructor
// prevents instantiation and extension.
// ignore: unused_element
Colors._();
/// Completely invisible.
static const Color transparent = Color(0x00000000);
...
}
这里的“颜色”类定义了一个transparent
我们可以轻松使用的变量,例如
...
Container(
color: Colors.transparent,
)
...
因此,您始终使用Color
实例作为“颜色”,因此Color
即使您使用Colors
类包装器,您也必须将变量定义为实例。
推荐阅读
- python - 如果从 cron 作业执行,python 不写入文件
- scrapy - 我写了一个错误的蜘蛛,但找不到,谁能更正代码?
- dart - 由于颤动,我调用云函数时出错
- javascript - 向 Chart.JS 添加自定义数据
- javascript - Javascript 将除 (/) 检测为加法 (+)
- google-sheets - 有没有办法防止在 Google 表格中堆叠查询的数组中出现重复?
- powershell - powershell 在不同的服务器上运行时给出不同的结果
- r - R 中的 text() 绘图 - 如何在不换行的情况下保持单行?
- ruby-on-rails - 如何在 Prawn PDF 中嵌入 Active Storage 中的图像?
- acumatica - 如何在发布时间活动屏幕上获取约会状态