Skip to content

Instantly share code, notes, and snippets.

@erluxman
Created May 22, 2020 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erluxman/7b5c1dfec4461b147d9b00a86d080bb5 to your computer and use it in GitHub Desktop.
Save erluxman/7b5c1dfec4461b147d9b00a86d080bb5 to your computer and use it in GitHub Desktop.
Colorfilters in dart
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text(
"Color Filtered".toUpperCase(),
style: TextStyle(fontWeight: FontWeight.w700),
),
),
body: ColorFilteredWidgetTest(),
),
);
}
}
class ColorFilteredWidgetTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("build Method called");
return ListView(
children: [
Center(
child: Wrap(
direction: Axis.horizontal,
children: [
FilterSampleWidget(
null,
"Original Image",
Image.network(
birdUrl,
width: 150,
)),
...getWidgetsForAllBlendMode(getAllBlendModesToUse()),
//End Blend modes
FilterSampleWidget(ColorFilter.linearToSrgbGamma(),
"linearTo SrgbGamma ColorFilter"),
FilterSampleWidget(ColorFilter.srgbToLinearGamma(),
"srgbTo LinearGamma ColorFilter"),
],
),
),
],
);
}
List<Widget> getWidgetsForAllBlendMode(List<BlendMode> blendModes) {
List<Widget> widgets = [];
blendModes.forEach((blendMode) {
widgets.addAll(getWidgetsForBlendMode(blendMode));
});
return widgets;
}
List<Widget> getWidgetsForBlendMode(BlendMode blendMode) {
return [...filterSet(blendMode, 1.0), ...filterSet(blendMode, 0.5)];
}
List<Widget> filterSet([BlendMode blendMode, double opacity = 1.0]) {
return [
FilterSampleWidget(
ColorFilter.mode(Colors.red.withOpacity(opacity), blendMode),
"{Red(${getAlpha(opacity)})-${getBlendMode(blendMode)}} Filter"),
FilterSampleWidget(
ColorFilter.mode(Colors.green.withOpacity(opacity), blendMode),
"{Green(${getAlpha(opacity)})-${getBlendMode(blendMode)}} Filter"),
FilterSampleWidget(
ColorFilter.mode(Colors.blue.withOpacity(opacity), blendMode),
"{Blue(${getAlpha(opacity)})-${getBlendMode(blendMode)}} Filter"),
FilterSampleWidget(
ColorFilter.mode(Colors.pink.withOpacity(opacity), blendMode),
"{Pink(${getAlpha(opacity)})-${getBlendMode(blendMode)}} Filter"),
];
}
int getAlpha(double opacity) {
return (opacity * 255).toInt();
}
String getBlendMode(BlendMode blendMode) {
return blendMode.toString().split(".").last;
}
}
class FilterWidget extends StatelessWidget {
const FilterWidget(this.filter);
final ColorFilter filter;
@override
Widget build(BuildContext context) {
return ColorFiltered(
colorFilter: filter,
child: Image.network(
birdUrl,
width: 150,
),
);
}
}
class FilterSampleWidget extends StatelessWidget {
const FilterSampleWidget([this.filter, this.title, this.widget]);
final ColorFilter filter;
final String title;
final Widget widget;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Stack(
//crossAxisAlignment: CrossAxisAlignment.center,
alignment: Alignment.bottomCenter,
children: [
widget ?? FilterWidget(filter),
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 150),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Color(0X88000000)),
child: Text(
title,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
),
),
)
],
),
);
}
}
List<BlendMode> getAllBlendModesToUse() {
var allBlendModes = [...BlendMode.values].toList();
filtersToRemove.forEach(allBlendModes.remove);
return allBlendModes;
}
List<BlendMode> filtersToRemove = [
BlendMode.clear,
BlendMode.src,
BlendMode.srcOver,
BlendMode.srcOut,
BlendMode.srcIn,
BlendMode.srcATop,
BlendMode.dstOver,
BlendMode.dstOut,
BlendMode.dstATop,
BlendMode.dstIn,
BlendMode.dst,
BlendMode.xor,
BlendMode.luminosity
];
String birdUrl =
"https://pbs.twimg.com/profile_images/1057989852942270464/bt45DHmR.jpg";
extension ColorUtil on Color {
Color withOpacity(double op) {
return Color.fromARGB(
(opacity * 255).toInt(), this.red, this.green, this.blue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment