Skip to content

Commit 79d77a1

Browse files
ksokolovskyiPiinksdkwingsmt
authored
Add missing deprecations to CupertinoDynamicColor. (flutter#171160)
Closes flutter#171059 ### Description - Adds missing deprecations to `CupertinoDynamicColor` - Adds dart fixes for some of the deprecations ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Kate Lovett <katelovett@google.com> Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com>
1 parent 0c43fe4 commit 79d77a1

File tree

5 files changed

+103
-8
lines changed

5 files changed

+103
-8
lines changed

engine/src/flutter/lib/ui/painting.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class Color {
215215
final ColorSpace colorSpace;
216216

217217
static int _floatToInt8(double x) {
218-
return (x * 255.0).round() & 0xff;
218+
return (x * 255.0).round().clamp(0, 255);
219219
}
220220

221221
/// A 32 bit value representing this color.
@@ -233,7 +233,7 @@ class Color {
233233
///
234234
/// Unlike accessing the floating point equivalent channels individually
235235
/// ([a], [r], [g], [b]), this method is intentionally _lossy_, and scales
236-
/// each channel using `(channel * 255.0).round() & 0xff`.
236+
/// each channel using `(channel * 255.0).round().clamp(0, 255)`.
237237
///
238238
/// While useful for storing a 32-bit integer value, prefer accessing the
239239
/// individual channels (and storing the double equivalent) where higher
@@ -263,7 +263,7 @@ class Color {
263263
///
264264
/// A value of 0 means this color is fully transparent. A value of 255 means
265265
/// this color is fully opaque.
266-
@Deprecated('Use (*.a * 255.0).round() & 0xff')
266+
@Deprecated('Use (*.a * 255.0).round().clamp(0, 255)')
267267
int get alpha => (0xff000000 & value) >> 24;
268268

269269
/// The alpha channel of this color as a double.
@@ -274,15 +274,15 @@ class Color {
274274
double get opacity => alpha / 0xFF;
275275

276276
/// The red channel of this color in an 8 bit value.
277-
@Deprecated('Use (*.r * 255.0).round() & 0xff')
277+
@Deprecated('Use (*.r * 255.0).round().clamp(0, 255)')
278278
int get red => (0x00ff0000 & value) >> 16;
279279

280280
/// The green channel of this color in an 8 bit value.
281-
@Deprecated('Use (*.g * 255.0).round() & 0xff')
281+
@Deprecated('Use (*.g * 255.0).round().clamp(0, 255)')
282282
int get green => (0x0000ff00 & value) >> 8;
283283

284284
/// The blue channel of this color in an 8 bit value.
285-
@Deprecated('Use (*.b * 255.0).round() & 0xff')
285+
@Deprecated('Use (*.b * 255.0).round().clamp(0, 255)')
286286
int get blue => (0x000000ff & value) >> 0;
287287

288288
/// Returns a new color with the provided components updated.

packages/flutter/lib/fix_data/fix_cupertino.yaml

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,53 @@ transforms:
451451
pageBuilder:
452452
kind: fragment
453453
value: 'arguments[pageBuilder]'
454-
# Before adding a new fix: read instructions at the top of this file.
454+
455+
# Changes made in https://github.com/flutter/flutter/pull/171160
456+
- title: "Rename from 'opacity' to 'a'"
457+
date: 2025-06-24
458+
element:
459+
uris: [ 'cupertino.dart' ]
460+
method: 'opacity'
461+
inClass: 'CupertinoDynamicColor'
462+
changes:
463+
- kind: 'rename'
464+
newName: 'a'
465+
466+
# Changes made in https://github.com/flutter/flutter/pull/171160
467+
- title: "Replace 'value' with 'toARGB32()'"
468+
date: 2025-06-24
469+
element:
470+
uris: [ 'cupertino.dart' ]
471+
method: 'value'
472+
inClass: 'CupertinoDynamicColor'
473+
changes:
474+
- kind: 'replacedBy'
475+
newElement:
476+
uris: [ 'cupertino.dart' ]
477+
method: 'toARGB32'
478+
inClass: 'CupertinoDynamicColor'
479+
480+
# Changes made in https://github.com/flutter/flutter/pull/171160
481+
- title: "Rename 'withOpacity'"
482+
date: 2025-06-24
483+
element:
484+
uris: [ 'cupertino.dart' ]
485+
method: 'withOpacity'
486+
inClass: 'CupertinoDynamicColor'
487+
changes:
488+
- kind: 'rename'
489+
newName: 'withValues'
490+
- kind: 'removeParameter'
491+
index: 0
492+
- kind: 'addParameter'
493+
index: 0
494+
name: 'alpha'
495+
style: optional_named
496+
argumentValue:
497+
expression: '{% opacity %}'
498+
requiredIf: "opacity != ''"
499+
variables:
500+
opacity:
501+
kind: 'fragment'
502+
value: 'arguments[0]'
503+
# Before adding a new fix: read instructions at the top of this file.

packages/flutter/lib/src/cupertino/colors.dart

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,27 +1157,51 @@ class CupertinoDynamicColor with Diagnosticable implements Color {
11571157
}
11581158
}
11591159

1160+
@Deprecated(
1161+
'Use component accessors like .r or .g, or toARGB32 for an explicit conversion. '
1162+
'This feature was deprecated after v3.33.0-1.0.pre.',
1163+
)
11601164
@override
11611165
int get value => _effectiveColor.value;
11621166

11631167
@override
1164-
int toARGB32() => value;
1168+
int toARGB32() => _effectiveColor.toARGB32();
11651169

1170+
@Deprecated(
1171+
'Use (*.a * 255.0).round().clamp(0, 255). '
1172+
'This feature was deprecated after v3.33.0-1.0.pre.',
1173+
)
11661174
@override
11671175
int get alpha => _effectiveColor.alpha;
11681176

1177+
@Deprecated(
1178+
'Use (*.b * 255.0).round().clamp(0, 255). '
1179+
'This feature was deprecated after v3.33.0-1.0.pre.',
1180+
)
11691181
@override
11701182
int get blue => _effectiveColor.blue;
11711183

11721184
@override
11731185
double computeLuminance() => _effectiveColor.computeLuminance();
11741186

1187+
@Deprecated(
1188+
'Use (*.g * 255.0).round().clamp(0, 255). '
1189+
'This feature was deprecated after v3.33.0-1.0.pre.',
1190+
)
11751191
@override
11761192
int get green => _effectiveColor.green;
11771193

1194+
@Deprecated(
1195+
'Use .a. '
1196+
'This feature was deprecated after v3.33.0-1.0.pre.',
1197+
)
11781198
@override
11791199
double get opacity => _effectiveColor.opacity;
11801200

1201+
@Deprecated(
1202+
'Use (*.r * 255.0).round().clamp(0, 255). '
1203+
'This feature was deprecated after v3.33.0-1.0.pre.',
1204+
)
11811205
@override
11821206
int get red => _effectiveColor.red;
11831207

@@ -1190,6 +1214,10 @@ class CupertinoDynamicColor with Diagnosticable implements Color {
11901214
@override
11911215
Color withGreen(int g) => _effectiveColor.withGreen(g);
11921216

1217+
@Deprecated(
1218+
'Use .withValues() to avoid precision loss. '
1219+
'This feature was deprecated after v3.33.0-1.0.pre.',
1220+
)
11931221
@override
11941222
Color withOpacity(double opacity) => _effectiveColor.withOpacity(opacity);
11951223

packages/flutter/test_fixes/cupertino/cupertino.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,13 @@ void main() {
295295
context: context,
296296
pageBuilder: (BuildContext context) => Container(),
297297
);
298+
299+
// https://github.com/flutter/flutter/pull/171160
300+
CupertinoDynamicColor dynamicColor = CupertinoDynamicColor.withBrightness(
301+
color: Color(0xFF000000),
302+
darkColor: Color(0xFF000001),
303+
);
304+
dynamicColor.opacity;
305+
dynamicColor.value;
306+
dynamicColor = dynamicColor.withOpacity(0.55);
298307
}

packages/flutter/test_fixes/cupertino/cupertino.dart.expect

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,13 @@ void main() {
313313
context: context,
314314
builder: (BuildContext context) => Container(),
315315
);
316+
317+
// https://github.com/flutter/flutter/pull/171160
318+
CupertinoDynamicColor dynamicColor = CupertinoDynamicColor.withBrightness(
319+
color: Color(0xFF000000),
320+
darkColor: Color(0xFF000001),
321+
);
322+
dynamicColor.a;
323+
dynamicColor.toARGB32();
324+
dynamicColor = dynamicColor.withValues(alpha: 0.55);
316325
}

0 commit comments

Comments
 (0)