Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set two different colours to single container

I am trying to achieve a custom design dynamically from my button. I have designed this button from a Container with InkWell. But I am not getting a correct way how I can set 2 different colours to this button based on received values from my API. For reference here is the button: enter image description here

In this button grey part is container background color. Then I added a image in this container. Now the red color should dynamic with height received from server. But I am not getting a correct way how I can do it.

like image 253
Code Hunter Avatar asked Dec 13 '22 11:12

Code Hunter


1 Answers

You can easily achieve this with container and linear gradient. Pass in the colors as gradient and define appropriate stops at the required percentages.

Example:

@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,
      ),
    ),
  );
} 

Hope this helps!

like image 107
Hemanth Raj Avatar answered Jan 19 '23 00:01

Hemanth Raj