Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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