gpt4 book ai didi

flutter - 为单个容器设置两种不同的颜色

转载 作者:IT王子 更新时间:2023-10-29 07:10:57 24 4
gpt4 key购买 nike

我正在尝试通过我的按钮动态实现自定义设计。我从一个带有 InkWell 的容器中设计了这个按钮。但是我没有得到如何根据从我的 API 接收到的值为此按钮设置 2 种不同颜色的正确方法。供引用,这里是按钮: enter image description here

此按钮中灰色部分为容器背景色。然后我在这个容器中添加了一个图像。现在红色应该随着从服务器接收到的高度而动态变化。但是我没有找到正确的方法。

最佳答案

您可以使用容器和线性渐变轻松实现这一点。将颜色作为渐变传递,并以所需的百分比定义适当的停靠点。

例子:

@override
Widget build(BuildContext context) {
final Color background = Colors.grey;
final Color fill = Colors.redAccent;
final List<Color> gradient = [
background,
background,
fill,
fill,
];
final double fillPercent = 56.23; // fills 56.23% for container from bottom
final double fillStop = (100 - fillPercent) / 100;
final List<double> stops = [0.0, fillStop, fillStop, 1.0];

return Container(
child: Icon(Icons.forward),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: gradient,
stops: stops,
end: Alignment.bottomCenter,
begin: Alignment.topCenter,
),
),
);
}

希望这对您有所帮助!

关于flutter - 为单个容器设置两种不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57068825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com