我正在尝试通过我的按钮动态实现自定义设计。我从一个带有 InkWell 的容器中设计了这个按钮。但是我没有得到如何根据从我的 API 接收到的值为此按钮设置 2 种不同颜色的正确方法。 供引用,这里是按钮:
此按钮中灰色部分为容器背景色。然后我在这个容器中添加了一个图像。现在红色应该随着从服务器接收到的高度而动态变化。 但是我没有找到正确的方法。
最佳答案
您可以使用容器和线性渐变轻松实现这一点。将颜色作为渐变传递,并以所需的百分比定义适当的停靠点。
例子:
@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/