@@ -43,8 +43,36 @@ extension ListExtension<E> on List<E> {
43
43
}
44
44
}
45
45
46
+ /// Extension on `Color` that provides non-deprecated, stable access to
47
+ /// ARGB channels and utility functions across all Flutter versions.
48
+ ///
49
+ /// ⚠️ NOTE:
50
+ /// - Starting from **Flutter 3.22**, the following `Color` properties are deprecated:
51
+ /// - `.red` , `.green` , `.blue` , `.alpha` , `.opacity`
52
+ /// - This extension uses bitmasking on the `.value` field to extract
53
+ /// ARGB components without relying on deprecated APIs.
46
54
extension ColorExtension on Color {
47
- /// Converts opacity value to color with alpha
48
- /// This avoids using the deprecated overlayOpacity directly
49
- Color reduceOpacity (double opacity) => withAlpha ((opacity * 255 ).round ());
55
+ /// Reduces the opacity of this color by the [opacity] factor (0.0 to 1.0).
56
+ ///
57
+ /// ✅ Avoids using deprecated `.opacity` or `.alpha` properties.
58
+ /// ✅ Uses `.withAlpha()` with calculated alpha from the factor.
59
+ ///
60
+ /// Works in all Flutter versions, including Flutter 3.22+.
61
+ Color reduceOpacity (double opacity) =>
62
+ withAlpha ((opacity.clamp (0.0 , 1.0 ) * 255 ).round ());
63
+
64
+ /// Safe replacement for deprecated `.alpha` (Flutter >= 3.22)
65
+ int get safeAlpha => (value >> 24 ) & 0xFF ;
66
+
67
+ /// Safe replacement for deprecated `.red` (Flutter >= 3.22)
68
+ int get safeRed => (value >> 16 ) & 0xFF ;
69
+
70
+ /// Safe replacement for deprecated `.green` (Flutter >= 3.22)
71
+ int get safeGreen => (value >> 8 ) & 0xFF ;
72
+
73
+ /// Safe replacement for deprecated `.blue` (Flutter >= 3.22)
74
+ int get safeBlue => value & 0xFF ;
75
+
76
+ /// Safe replacement for deprecated `.opacity` (Flutter >= 3.22)
77
+ double get safeOpacity => safeAlpha / 255.0 ;
50
78
}
0 commit comments