首页 > 解决方案 > 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来定义颜色时。

标签: flutterdart

解决方案


我认为该类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类包装器,您也必须将变量定义为实例。


推荐阅读