如何从我的渐变容器小部件中获取十六进制颜色?

huangapple go评论48阅读模式
英文:

How can i get the HEX color from my gradient container widget

问题

我有以下渐变颜色

Container(
  decoration: BoxDecoration(
      gradient: LinearGradient(
          colors: [
            Colors.blue[400]!,
            Colors.black38,
          ]
      )
  ),
),

现在我需要获取它的十六进制代码

例如,使用Google颜色选择器,我可以获取黑色的 #0d0900

我如何从我的先前小部件颜色中获取相同的颜色?

英文:

i have the following gradient color

  Container(
    decoration: BoxDecoration(
        gradient: LinearGradient(
            colors: [
              Colors.blue[400]!,
              Colors.black38,
            ]
        )
    ),
  ),

Now i need to get it's HEX code

for example using google color picker i can get #0d0900 for a black color .

How could i get the same from my previous widget color ?

答案1

得分: 1

String colorToHex(Color color) {
  String hex = color.value.toRadixString(16).padLeft(8, '0');
  return "#" + hex.substring(2, 8);
}

-----------------------------
**How this work to get colors from gradient**


>In this example, a `LinearGradient` with `red` and `yellow` as its colors is created and assigned to the `gradient` variable. The colors property of the `LinearGradient` is then assigned to the `gradientColors` list.

>The map function is used to convert each color in the `gradientColors` list to its hexadecimal representation using the `colorToHex` function, and the result is assigned to the `hexColors` list.

LinearGradient gradient = LinearGradient(
    colors: [Colors.red, Colors.yellow],
  );

late List<Color> gradientColors = gradient.colors;
late List<String> hexColors =
    gradientColors.map((color) => colorToHex(color)).toList();

There are Container and Text Widgets in the body of Scaffold

              Container(
                width: 200.0,
                height: 100.0,
                decoration: BoxDecoration(
                  gradient: gradient,
                ),
              ),
              Text("$hexColors");
英文:

Create this function

String colorToHex(Color color) {
  String hex = color.value.toRadixString(16).padLeft(8, &#39;0&#39;);
  return &quot;#&quot; + hex.substring(2, 8);
}

How this work to get colors from gradient

>In this example, a LinearGradient with red and yellow as its colors is created and assigned to the gradient variable. The colors property of the LinearGradient is then assigned to the gradientColors list.

>The map function is used to convert each color in the gradientColors list to its hexadecimal representation using the colorToHex function, and the result is assigned to the hexColors list.

LinearGradient gradient = LinearGradient(
    colors: [Colors.red, Colors.yellow],
  );

  late List&lt;Color&gt; gradientColors = gradient.colors;
  late List&lt;String&gt; hexColors =
      gradientColors.map((color) =&gt; colorToHex(color)).toList();

There are Container and Text Widgets in the body of Scaffold

              Container(
                width: 200.0,
                height: 100.0,
                decoration: BoxDecoration(
                  gradient: gradient,
                ),
              ),
              Text(&quot;$hexColors&quot;),

huangapple
  • 本文由 发表于 2023年2月6日 11:43:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定