英文:
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, '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"),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论