今天,我看完以下代码片段,这些代码片段在Flutter中实现了渐变
return new Container( ... decoration: new BoxDecoration( gradient: new LinearGradient( colors: [ const Color(0xFF3366FF), const Color(0xFF00CCFF), ] begin: const FractionalOffset(0.0, 0.0), end: const FractionalOffset(1.0, 0.0), stops: [0.0, 1.0], tileMode: TileMode.clamp ), ), ),
它提出了两个问题:
1)这是什么颜色系统0xFF3366FF?它看起来有点类似于十六进制,但事实并非如此。
0xFF3366FF
2)为什么我们使用constfor const Color()相反,new Color()我理解两者之间的不同,但是这里的const对我来说并不直观,我希望它能创建一个new Color()类实例,类似于我们的用法new Text("Some text")。如果需要使用const,为什么还不TileMode.clamp使用const?
const
const Color()
new Color()
new Text("Some text")
TileMode.clamp
从Flutter来源
class Color { /// Construct a color from the lower 32 bits of an [int]. /// /// The bits are interpreted as follows: /// /// * Bits 24-31 are the alpha value. /// * Bits 16-23 are the red value. /// * Bits 8-15 are the green value. /// * Bits 0-7 are the blue value. /// /// In other words, if AA is the alpha value in hex, RR the red value in hex, /// GG the green value in hex, and BB the blue value in hex, a color can be /// expressed as `const Color(0xAARRGGBB)`. /// /// For example, to get a fully opaque orange, you would use `const /// Color(0xFFFF9000)` (`FF` for the alpha, `FF` for the red, `90` for the /// green, and `00` for the blue). const Color(int value) : value = value & 0xFFFFFFFF;
const 实例被规范化。
如果您const Color(0xFF00CCFF)的代码中有多个实例,则只会创建一个实例。
const Color(0xFF00CCFF)
const实例在编译时评估。在Dart VM中,这是加载代码的时间,但是在Flutter生产中,将使用AoT编译,因此const值会带来很小的性能优势。