What color system does flutter use and why do we use `const Color` instead of `new Color`

前端 未结 3 1835
鱼传尺愫
鱼传尺愫 2020-12-06 12:11

Today I came over following snippet of code that implements gradient in flutter

return new Container(
  ...
  decoration: new BoxDecoration(
    gradient: ne         


        
3条回答
  •  眼角桃花
    2020-12-06 12:23

    From the Flutter source

    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 instances are canonicalized.

    If you have multiple const Color(0xFF00CCFF) in your code, only one instance will be created.

    const instances are evaluated at compile time. In the Dart VM this is when the code is loaded, but in Flutter production AoT compilation is used and const values therefore provide a small performance benefit.

    See also How does the const constructor actually work?

提交回复
热议问题