how do you invert a color in flutter?
621
As per http://www.vb-helper.com/howto_invert_color.html:
You can invert a color by subtracting each of its red, green, and blue components from 255. In other words:
new_red = 255 - old_red new_green = 255 - old_green new_blue = 255 - old_blue
Color invert(Color color) {
final r = 255 - color.red;
final g = 255 - color.green;
final b = 255 - color.blue;
return Color.fromARGB((color.opacity * 255).round(), r, g, b);
}
void main() {
final inverted = invert(Colors.deepOrange);
}
Author by
whatever
Updated on December 28, 2022Comments
-
whatever over 1 year
i have a widget that is used to create a gradient effect:
class GradientContainer extends StatelessWidget { final child; GradientContainer({@required this.child}); @override Widget build(BuildContext context) { return Stack(children: [ Positioned( left: 5, top: 17, bottom: 5, right: 9, child: ClipRRect( borderRadius: BorderRadius.circular(4), child: Container( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, stops: [ 0.0, 1.0 ], colors: [ Colors.black.withOpacity(0.0), Theme.of(context).scaffoldBackgroundColor.withOpacity(0.8), ])), ), )), child, ]); } }
this works great intill the user switches to dark theme and cannot sea the gradient very clearly. So, I would like to do something like
Theme.of(context).scaffoldBackgroundColor.invertColor().withOpacity(0.8)
so that I can get the inverted color of the body which allows me to create a good contrast on the gradient no matter the theme. Anyone know how to do this? I have tried looking online but all I could find is how to apply an invert filter onto an image.