Skip to content

Commit f6951fd

Browse files
committed
Blend Modes #3
1 parent 9a75d67 commit f6951fd

File tree

3 files changed

+51
-50
lines changed

3 files changed

+51
-50
lines changed

lib/src/transformers/node_transformers/passive_rectangle_transformer.dart

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,19 @@ typedef ImageFillBuilder = Widget Function(
556556
bool obscureImages,
557557
);
558558

559+
String getColorHex2(
560+
Color color, {
561+
bool withAlpha = false,
562+
bool withHashtag = false,
563+
}) {
564+
String colorHex =
565+
(color.value - (color.alpha << 24)).toRadixString(16).padLeft(6, '0');
566+
String alphaHex = '';
567+
if (withAlpha) alphaHex = (color.alpha).toRadixString(16).padLeft(2, 'F');
568+
String hashtag = withHashtag ? '#' : '';
569+
return (hashtag + alphaHex + colorHex).toUpperCase();
570+
}
571+
559572
List<Widget> buildFills(
560573
BaseNode node, {
561574
Map<int, TypedBytes> imageBytes = const {},
@@ -576,19 +589,20 @@ List<Widget> buildFills(
576589
// layers above it, and then consume/apply the blendmodes by wrapping itself
577590
// with ShaderMasks until a non-blendmode fill is found. This is done to
578591
// ensure that the blendmodes are applied in the correct order.
579-
Map<int, PaintModel> lastBlendTree = {};
580-
for (final (i, model) in node.fills.reversed.indexed) {
592+
List<PaintModel> lastBlendTree = [];
593+
for (final model in node.fills.reversed) {
594+
final index = node.fills.indexOf(model);
581595
if (!model.visible) continue;
582596
if (model.blendMode != BlendModeC.srcOver) {
583-
lastBlendTree[i] = model;
597+
lastBlendTree.add(model);
584598
continue;
585599
}
586600

587601
// At this point, we have reached a non-blendmode fill. We need to apply the
588602
// blendmodes in the correct order to this fill.
589603
Widget fill = buildFill(
590604
node,
591-
index: i,
605+
index: index,
592606
imageBytes: imageBytes,
593607
imageOpacity: imageOpacity,
594608
imageRotation: imageRotation,
@@ -599,38 +613,34 @@ List<Widget> buildFills(
599613
scopedValues: scopedValues,
600614
);
601615

602-
// fill = BlendMask(
603-
// model: paint,
604-
// image: null,
605-
// child: fill,
606-
// );
607-
fill = Stack(
608-
fit: StackFit.expand,
609-
children: [
610-
fill,
611-
for (final MapEntry(key: i, value: paint)
612-
in lastBlendTree.entries.toList().reversed)
613-
if (paint.visible)
614-
Positioned.fill(
615-
child: PaintBlendMask(
616-
model: paint,
617-
image: null,
618-
child: buildFill(
619-
node,
620-
index: i,
621-
imageBytes: imageBytes,
622-
imageOpacity: imageOpacity,
623-
imageRotation: imageRotation,
624-
imageFillBuilder: imageFillBuilder,
625-
useInk: useInk,
626-
obscureImages: obscureImages,
627-
settings: settings,
628-
scopedValues: scopedValues,
616+
if (lastBlendTree.isNotEmpty) {
617+
fill = Stack(
618+
fit: StackFit.expand,
619+
children: [
620+
fill,
621+
for (final paint in lastBlendTree.reversed)
622+
if (paint.visible)
623+
Positioned.fill(
624+
child: PaintBlendMask(
625+
model: paint,
626+
image: null,
627+
child: buildFill(
628+
node,
629+
index: node.fills.indexOf(paint),
630+
imageBytes: imageBytes,
631+
imageOpacity: imageOpacity,
632+
imageRotation: imageRotation,
633+
imageFillBuilder: imageFillBuilder,
634+
useInk: useInk,
635+
obscureImages: obscureImages,
636+
settings: settings,
637+
scopedValues: scopedValues,
638+
),
629639
),
630640
),
631-
),
632-
],
633-
);
641+
],
642+
);
643+
}
634644

635645
lastBlendTree.clear();
636646
raster.add(wrapFillWithPositioned(
@@ -831,7 +841,6 @@ class RelativeTransform {
831841
);
832842
}
833843

834-
// Applies a BlendMode to its child.
835844
class BlendMask extends SingleChildRenderObjectWidget {
836845
final List<BlendMode> blendModes;
837846
final double opacity;
@@ -863,7 +872,7 @@ class RenderBlendMask extends RenderProxyBox {
863872
@override
864873
void paint(context, offset) {
865874
// Complex blend modes can be raster cached incorrectly on the Skia backend.
866-
context.setWillChangeHint();
875+
// context.setWillChangeHint();
867876
for (var blend in blendModes) {
868877
context.canvas.saveLayer(
869878
offset & size,
@@ -912,8 +921,6 @@ class RenderPaintBlendMask extends RenderProxyBox {
912921

913922
Paint makePaint(Rect bounds) {
914923
Paint paint = Paint()..blendMode = _model.blendMode.flutterBlendMode;
915-
// paint.color = Colors.white;
916-
// return paint;
917924

918925
switch (_model.type) {
919926
case PaintType.solid:
@@ -945,10 +952,13 @@ class RenderPaintBlendMask extends RenderProxyBox {
945952
@override
946953
void paint(context, offset) {
947954
// Complex blend modes can be raster cached incorrectly on the Skia backend.
948-
context.setWillChangeHint();
955+
// context.setWillChangeHint();
949956
context.canvas.saveLayer(
950957
offset & size,
951-
makePaint(offset & size),
958+
// makePaint(offset & size),
959+
Paint()
960+
..blendMode = _model.blendMode.flutterBlendMode
961+
..color = Colors.white,
952962
);
953963

954964
super.paint(context, offset);

lib/src/transformers/passive_transformer_manager.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ class PassiveNodeTransformerManager extends WidgetNodeTransformerManager {
8282
if (settings.withOpacity) {
8383
widget = applyWidgetOpacity(node, widget);
8484
}
85-
8685
if (settings.withReactions) {
8786
widget = wrapWithReaction(context, node, widget);
8887
}
89-
9088
if (settings.withRotation) {
9189
widget = applyWidgetRotation(context, node, widget);
9290
}

lib/src/transformers/widget_node_transformer_manager.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,10 @@ abstract class WidgetNodeTransformerManager extends NodeTransformerManager<
7171
BaseNode node,
7272
Widget widget,
7373
) {
74-
// if (node is BlendMixin && node.blendMode != BlendModeC.srcOver) {
7574
return BlendMask(
76-
// blendMode: node.blendMode.flutterBlendMode,
77-
blendModes: [
78-
node is BlendMixin ? node.blendMode.flutterBlendMode : BlendMode.srcOver
79-
],
75+
blendModes: [BlendMode.srcOver],
8076
child: widget,
8177
);
82-
// }
83-
84-
return widget;
8578
}
8679

8780
/// Convenience method to handle widget rotation.

0 commit comments

Comments
 (0)