1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/foundation.dart';
6import 'package:flutter/rendering.dart';
7
8import 'framework.dart';
9
10/// Applies a [ColorFilter] to its child.
11///
12/// This widget applies a function independently to each pixel of [child]'s
13/// content, according to the [ColorFilter] specified.
14/// Use the [ColorFilter.mode] constructor to apply a [Color] using a [BlendMode].
15/// Use the [BackdropFilter] widget instead, if the [ColorFilter]
16/// needs to be applied onto the content beneath [child].
17///
18/// {@youtube 560 315 https://www.youtube.com/watch?v=F7Cll22Dno8}
19///
20/// {@tool dartpad}
21/// These two images have two [ColorFilter]s applied with different [BlendMode]s,
22/// one with red color and [BlendMode.modulate] another with a grey color and [BlendMode.saturation].
23///
24/// ** See code in examples/api/lib/widgets/color_filter/color_filtered.0.dart **
25///{@end-tool}
26///
27/// See also:
28///
29/// * [BlendMode], describes how to blend a source image with the destination image.
30/// * [ColorFilter], which describes a function that modify a color to a different color.
31
32@immutable
33class ColorFiltered extends SingleChildRenderObjectWidget {
34 /// Creates a widget that applies a [ColorFilter] to its child.
35 const ColorFiltered({required this.colorFilter, super.child, super.key});
36
37 /// The color filter to apply to the child of this widget.
38 final ColorFilter colorFilter;
39
40 @override
41 RenderObject createRenderObject(BuildContext context) => _ColorFilterRenderObject(colorFilter);
42
43 @override
44 void updateRenderObject(BuildContext context, RenderObject renderObject) {
45 (renderObject as _ColorFilterRenderObject).colorFilter = colorFilter;
46 }
47
48 @override
49 void debugFillProperties(DiagnosticPropertiesBuilder properties) {
50 super.debugFillProperties(properties);
51 properties.add(DiagnosticsProperty<ColorFilter>('colorFilter', colorFilter));
52 }
53}
54
55class _ColorFilterRenderObject extends RenderProxyBox {
56 _ColorFilterRenderObject(this._colorFilter);
57
58 ColorFilter get colorFilter => _colorFilter;
59 ColorFilter _colorFilter;
60 set colorFilter(ColorFilter value) {
61 if (value != _colorFilter) {
62 _colorFilter = value;
63 markNeedsPaint();
64 }
65 }
66
67 @override
68 bool get alwaysNeedsCompositing => child != null;
69
70 @override
71 void paint(PaintingContext context, Offset offset) {
72 layer = context.pushColorFilter(offset, colorFilter, super.paint, oldLayer: layer as ColorFilterLayer?);
73 assert(() {
74 layer!.debugCreator = debugCreator;
75 return true;
76 }());
77 }
78}
79