Skip to content

Commit a111cd0

Browse files
authored
Support reduceMotion accessibility feature in MediaQuery (flutter#190287)
Exposes the platform's "Reduce Motion" accessibility setting through MediaQueryData, so widgets can react to it with proper dependency tracking instead of reading it ad hoc off the platform dispatcher. The reduceMotion flag tracking the iOS "Reduce Motion" setting already exists at the engine layer as `dart:ui.AccessibilityFeatures.reduceMotion`, but it was never surfaced on MediaQueryData. This mirrors the existing disableAnimations support, which corresponds to Android's "Remove animations" setting. Unlike disableAnimations, reduceMotion is not consulted by framework-level animation APIs such as AnimationController. It is intended for widgets that want to tone down or replace non-essential motion, for example, by substituting a cross-fade for a slide. No engine or SemanticsBinding changes are required: as with disableAnimations, MediaQueryData reads reduceMotion directly from platformDispatcher.accessibilityFeatures, and FakeAccessibilityFeatures already supports the flag. Issue: flutter#4827 Issue: flutter#65874 <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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. - [X] 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]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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
1 parent cde1532 commit a111cd0

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎packages/flutter/lib/src/widgets/media_query.dart‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ enum _MediaQueryAspect {
101101
/// Specifies the aspect corresponding to [MediaQueryData.disableAnimations].
102102
disableAnimations,
103103

104+
/// Specifies the aspect corresponding to [MediaQueryData.reduceMotion].
105+
reduceMotion,
106+
104107
/// Specifies the aspect corresponding to [MediaQueryData.boldText].
105108
boldText,
106109

@@ -228,6 +231,7 @@ class MediaQueryData {
228231
this.highContrast = false,
229232
this.onOffSwitchLabels = false,
230233
this.disableAnimations = false,
234+
this.reduceMotion = false,
231235
this.boldText = false,
232236
this.supportsAnnounce = false,
233237
this.navigationMode = NavigationMode.traditional,
@@ -319,6 +323,8 @@ class MediaQueryData {
319323
disableAnimations =
320324
platformData?.disableAnimations ??
321325
view.platformDispatcher.accessibilityFeatures.disableAnimations,
326+
reduceMotion =
327+
platformData?.reduceMotion ?? view.platformDispatcher.accessibilityFeatures.reduceMotion,
322328
boldText = platformData?.boldText ?? view.platformDispatcher.accessibilityFeatures.boldText,
323329
supportsAnnounce =
324330
platformData?.supportsAnnounce ??
@@ -692,6 +698,29 @@ class MediaQueryData {
692698
/// originates.
693699
final bool disableAnimations;
694700

701+
/// Whether the platform is requesting that animations be reduced or replaced
702+
/// with cross-fades in preference to motion effects.
703+
///
704+
/// This corresponds to the iOS "Reduce Motion" accessibility setting.
705+
///
706+
/// Unlike [disableAnimations], this flag does not automatically alter
707+
/// framework animations such as those controlled via [AnimationController].
708+
/// Instead, it is intended to be read by widgets that want to tone down or
709+
/// replace non-essential motion, for example by substituting a cross-fade
710+
/// for a slide transition.
711+
///
712+
/// When implementing custom animations, you should check this property and
713+
/// adjust behavior accordingly; for example, by preferring a fade over
714+
/// movement when it is true.
715+
///
716+
/// See also:
717+
///
718+
/// * [dart:ui.AccessibilityFeatures.reduceMotion], the underlying primitive
719+
/// flag provided by the platform.
720+
/// * [dart:ui.PlatformDispatcher.accessibilityFeatures], where the setting
721+
/// originates.
722+
final bool reduceMotion;
723+
695724
/// Whether the platform is requesting that text be drawn with a bold font
696725
/// weight.
697726
///
@@ -852,6 +881,7 @@ class MediaQueryData {
852881
bool? highContrast,
853882
bool? onOffSwitchLabels,
854883
bool? disableAnimations,
884+
bool? reduceMotion,
855885
bool? invertColors,
856886
bool? accessibleNavigation,
857887
bool? boldText,
@@ -879,6 +909,7 @@ class MediaQueryData {
879909
highContrast: highContrast ?? this.highContrast,
880910
onOffSwitchLabels: onOffSwitchLabels ?? this.onOffSwitchLabels,
881911
disableAnimations: disableAnimations ?? this.disableAnimations,
912+
reduceMotion: reduceMotion ?? this.reduceMotion,
882913
accessibleNavigation: accessibleNavigation ?? this.accessibleNavigation,
883914
boldText: boldText ?? this.boldText,
884915
supportsAnnounce: supportsAnnounce ?? this.supportsAnnounce,
@@ -927,6 +958,7 @@ class MediaQueryData {
927958
highContrast: highContrast,
928959
onOffSwitchLabels: onOffSwitchLabels,
929960
disableAnimations: disableAnimations,
961+
reduceMotion: reduceMotion,
930962
accessibleNavigation: accessibleNavigation,
931963
boldText: boldText,
932964
supportsAnnounce: supportsAnnounce,
@@ -962,6 +994,7 @@ class MediaQueryData {
962994
highContrast: highContrast,
963995
onOffSwitchLabels: onOffSwitchLabels,
964996
disableAnimations: disableAnimations,
997+
reduceMotion: reduceMotion,
965998
accessibleNavigation: accessibleNavigation,
966999
boldText: boldText,
9671000
supportsAnnounce: supportsAnnounce,
@@ -1165,6 +1198,7 @@ class MediaQueryData {
11651198
other.highContrast == highContrast &&
11661199
other.onOffSwitchLabels == onOffSwitchLabels &&
11671200
other.disableAnimations == disableAnimations &&
1201+
other.reduceMotion == reduceMotion &&
11681202
other.invertColors == invertColors &&
11691203
other.accessibleNavigation == accessibleNavigation &&
11701204
other.boldText == boldText &&
@@ -1193,6 +1227,7 @@ class MediaQueryData {
11931227
highContrast,
11941228
onOffSwitchLabels,
11951229
disableAnimations,
1230+
reduceMotion,
11961231
invertColors,
11971232
accessibleNavigation,
11981233
boldText,
@@ -1225,6 +1260,7 @@ class MediaQueryData {
12251260
'highContrast: $highContrast',
12261261
'onOffSwitchLabels: $onOffSwitchLabels',
12271262
'disableAnimations: $disableAnimations',
1263+
'reduceMotion: $reduceMotion',
12281264
'invertColors: $invertColors',
12291265
'boldText: $boldText',
12301266
'navigationMode: ${navigationMode.name}',
@@ -2026,6 +2062,28 @@ class MediaQuery extends InheritedModel<_MediaQueryAspect> {
20262062
static bool? maybeDisableAnimationsOf(BuildContext context) =>
20272063
_maybeOf(context, _MediaQueryAspect.disableAnimations)?.disableAnimations;
20282064

2065+
/// Returns [MediaQueryData.reduceMotion] for the nearest [MediaQuery]
2066+
/// ancestor or false, if no such ancestor exists.
2067+
///
2068+
/// Use of this method will cause the given [context] to rebuild any time that
2069+
/// the [MediaQueryData.reduceMotion] property of the ancestor
2070+
/// [MediaQuery] changes.
2071+
///
2072+
/// {@macro flutter.widgets.media_query.MediaQuery.dontUseOf}
2073+
static bool reduceMotionOf(BuildContext context) =>
2074+
_of(context, _MediaQueryAspect.reduceMotion).reduceMotion;
2075+
2076+
/// Returns [MediaQueryData.reduceMotion] for the nearest [MediaQuery]
2077+
/// ancestor or null, if no such ancestor exists.
2078+
///
2079+
/// Use of this method will cause the given [context] to rebuild any time that
2080+
/// the [MediaQueryData.reduceMotion] property of the ancestor
2081+
/// [MediaQuery] changes.
2082+
///
2083+
/// {@macro flutter.widgets.media_query.MediaQuery.dontUseMaybeOf}
2084+
static bool? maybeReduceMotionOf(BuildContext context) =>
2085+
_maybeOf(context, _MediaQueryAspect.reduceMotion)?.reduceMotion;
2086+
20292087
/// Returns the [MediaQueryData.boldText] accessibility setting for the
20302088
/// nearest [MediaQuery] ancestor or false, if no such ancestor exists.
20312089
///
@@ -2273,6 +2331,7 @@ class MediaQuery extends InheritedModel<_MediaQueryAspect> {
22732331
data.onOffSwitchLabels != oldWidget.data.onOffSwitchLabels,
22742332
_MediaQueryAspect.disableAnimations =>
22752333
data.disableAnimations != oldWidget.data.disableAnimations,
2334+
_MediaQueryAspect.reduceMotion => data.reduceMotion != oldWidget.data.reduceMotion,
22762335
_MediaQueryAspect.boldText => data.boldText != oldWidget.data.boldText,
22772336
_MediaQueryAspect.supportsAnnounce =>
22782337
data.supportsAnnounce != oldWidget.data.supportsAnnounce,

‎packages/flutter/test/widgets/media_query_test.dart‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ void main() {
155155
expect(data.accessibleNavigation, false);
156156
expect(data.invertColors, false);
157157
expect(data.disableAnimations, false);
158+
expect(data.reduceMotion, false);
158159
expect(data.boldText, false);
159160
expect(data.highContrast, false);
160161
expect(data.onOffSwitchLabels, false);
@@ -172,6 +173,7 @@ void main() {
172173
accessibleNavigation: true,
173174
invertColors: true,
174175
disableAnimations: true,
176+
reduceMotion: true,
175177
boldText: true,
176178
highContrast: true,
177179
onOffSwitchLabels: true,
@@ -206,6 +208,7 @@ void main() {
206208
expect(data.accessibleNavigation, platformData.accessibleNavigation);
207209
expect(data.invertColors, platformData.invertColors);
208210
expect(data.disableAnimations, platformData.disableAnimations);
211+
expect(data.reduceMotion, platformData.reduceMotion);
209212
expect(data.boldText, platformData.boldText);
210213
expect(data.highContrast, platformData.highContrast);
211214
expect(data.onOffSwitchLabels, platformData.onOffSwitchLabels);
@@ -257,6 +260,7 @@ void main() {
257260
data.disableAnimations,
258261
tester.platformDispatcher.accessibilityFeatures.disableAnimations,
259262
);
263+
expect(data.reduceMotion, tester.platformDispatcher.accessibilityFeatures.reduceMotion);
260264
expect(data.boldText, tester.platformDispatcher.accessibilityFeatures.boldText);
261265
expect(data.highContrast, tester.platformDispatcher.accessibilityFeatures.highContrast);
262266
expect(
@@ -283,6 +287,7 @@ void main() {
283287
accessibleNavigation: true,
284288
invertColors: true,
285289
disableAnimations: true,
290+
reduceMotion: true,
286291
boldText: true,
287292
highContrast: true,
288293
onOffSwitchLabels: true,
@@ -331,6 +336,7 @@ void main() {
331336
expect(data.accessibleNavigation, platformData.accessibleNavigation);
332337
expect(data.invertColors, platformData.invertColors);
333338
expect(data.disableAnimations, platformData.disableAnimations);
339+
expect(data.reduceMotion, platformData.reduceMotion);
334340
expect(data.boldText, platformData.boldText);
335341
expect(data.highContrast, platformData.highContrast);
336342
expect(data.onOffSwitchLabels, platformData.onOffSwitchLabels);
@@ -401,6 +407,7 @@ void main() {
401407
data.disableAnimations,
402408
tester.platformDispatcher.accessibilityFeatures.disableAnimations,
403409
);
410+
expect(data.reduceMotion, tester.platformDispatcher.accessibilityFeatures.reduceMotion);
404411
expect(data.boldText, tester.platformDispatcher.accessibilityFeatures.boldText);
405412
expect(data.highContrast, tester.platformDispatcher.accessibilityFeatures.highContrast);
406413
expect(
@@ -592,6 +599,7 @@ void main() {
592599
expect(copied.accessibleNavigation, data.accessibleNavigation);
593600
expect(copied.invertColors, data.invertColors);
594601
expect(copied.disableAnimations, data.disableAnimations);
602+
expect(copied.reduceMotion, data.reduceMotion);
595603
expect(copied.boldText, data.boldText);
596604
expect(copied.highContrast, data.highContrast);
597605
expect(copied.onOffSwitchLabels, data.onOffSwitchLabels);
@@ -634,6 +642,7 @@ void main() {
634642
accessibleNavigation: true,
635643
invertColors: true,
636644
disableAnimations: true,
645+
reduceMotion: true,
637646
boldText: true,
638647
highContrast: true,
639648
onOffSwitchLabels: true,
@@ -654,6 +663,7 @@ void main() {
654663
expect(copied.accessibleNavigation, true);
655664
expect(copied.invertColors, true);
656665
expect(copied.disableAnimations, true);
666+
expect(copied.reduceMotion, true);
657667
expect(copied.boldText, true);
658668
expect(copied.highContrast, true);
659669
expect(copied.onOffSwitchLabels, true);
@@ -703,6 +713,7 @@ void main() {
703713
expect(updatedData.accessibleNavigation, data.accessibleNavigation);
704714
expect(updatedData.invertColors, data.invertColors);
705715
expect(updatedData.disableAnimations, data.disableAnimations);
716+
expect(updatedData.reduceMotion, data.reduceMotion);
706717
expect(updatedData.boldText, data.boldText);
707718
expect(updatedData.highContrast, data.highContrast);
708719
expect(updatedData.onOffSwitchLabels, data.onOffSwitchLabels);
@@ -747,6 +758,7 @@ void main() {
747758
expect(updatedData.accessibleNavigation, data.accessibleNavigation);
748759
expect(updatedData.invertColors, data.invertColors);
749760
expect(updatedData.disableAnimations, data.disableAnimations);
761+
expect(updatedData.reduceMotion, data.reduceMotion);
750762
expect(updatedData.boldText, data.boldText);
751763
expect(updatedData.highContrast, data.highContrast);
752764
expect(updatedData.onOffSwitchLabels, data.onOffSwitchLabels);
@@ -791,6 +803,7 @@ void main() {
791803
accessibleNavigation: true,
792804
invertColors: true,
793805
disableAnimations: true,
806+
reduceMotion: true,
794807
boldText: true,
795808
highContrast: true,
796809
onOffSwitchLabels: true,
@@ -828,6 +841,7 @@ void main() {
828841
expect(unpadded.accessibleNavigation, true);
829842
expect(unpadded.invertColors, true);
830843
expect(unpadded.disableAnimations, true);
844+
expect(unpadded.reduceMotion, true);
831845
expect(unpadded.boldText, true);
832846
expect(unpadded.highContrast, true);
833847
expect(unpadded.onOffSwitchLabels, true);
@@ -867,6 +881,7 @@ void main() {
867881
accessibleNavigation: true,
868882
invertColors: true,
869883
disableAnimations: true,
884+
reduceMotion: true,
870885
boldText: true,
871886
highContrast: true,
872887
onOffSwitchLabels: true,
@@ -901,6 +916,7 @@ void main() {
901916
expect(unpadded.accessibleNavigation, true);
902917
expect(unpadded.invertColors, true);
903918
expect(unpadded.disableAnimations, true);
919+
expect(unpadded.reduceMotion, true);
904920
expect(unpadded.boldText, true);
905921
expect(unpadded.highContrast, true);
906922
expect(unpadded.onOffSwitchLabels, true);
@@ -940,6 +956,7 @@ void main() {
940956
accessibleNavigation: true,
941957
invertColors: true,
942958
disableAnimations: true,
959+
reduceMotion: true,
943960
boldText: true,
944961
highContrast: true,
945962
onOffSwitchLabels: true,
@@ -977,6 +994,7 @@ void main() {
977994
expect(unpadded.accessibleNavigation, true);
978995
expect(unpadded.invertColors, true);
979996
expect(unpadded.disableAnimations, true);
997+
expect(unpadded.reduceMotion, true);
980998
expect(unpadded.boldText, true);
981999
expect(unpadded.highContrast, true);
9821000
expect(unpadded.onOffSwitchLabels, true);
@@ -1016,6 +1034,7 @@ void main() {
10161034
accessibleNavigation: true,
10171035
invertColors: true,
10181036
disableAnimations: true,
1037+
reduceMotion: true,
10191038
boldText: true,
10201039
highContrast: true,
10211040
onOffSwitchLabels: true,
@@ -1050,6 +1069,7 @@ void main() {
10501069
expect(unpadded.accessibleNavigation, true);
10511070
expect(unpadded.invertColors, true);
10521071
expect(unpadded.disableAnimations, true);
1072+
expect(unpadded.reduceMotion, true);
10531073
expect(unpadded.boldText, true);
10541074
expect(unpadded.highContrast, true);
10551075
expect(unpadded.onOffSwitchLabels, true);
@@ -1089,6 +1109,7 @@ void main() {
10891109
accessibleNavigation: true,
10901110
invertColors: true,
10911111
disableAnimations: true,
1112+
reduceMotion: true,
10921113
boldText: true,
10931114
highContrast: true,
10941115
onOffSwitchLabels: true,
@@ -1126,6 +1147,7 @@ void main() {
11261147
expect(unpadded.accessibleNavigation, true);
11271148
expect(unpadded.invertColors, true);
11281149
expect(unpadded.disableAnimations, true);
1150+
expect(unpadded.reduceMotion, true);
11291151
expect(unpadded.boldText, true);
11301152
expect(unpadded.highContrast, true);
11311153
expect(unpadded.onOffSwitchLabels, true);
@@ -1165,6 +1187,7 @@ void main() {
11651187
accessibleNavigation: true,
11661188
invertColors: true,
11671189
disableAnimations: true,
1190+
reduceMotion: true,
11681191
boldText: true,
11691192
highContrast: true,
11701193
onOffSwitchLabels: true,
@@ -1199,6 +1222,7 @@ void main() {
11991222
expect(unpadded.accessibleNavigation, true);
12001223
expect(unpadded.invertColors, true);
12011224
expect(unpadded.disableAnimations, true);
1225+
expect(unpadded.reduceMotion, true);
12021226
expect(unpadded.boldText, true);
12031227
expect(unpadded.highContrast, true);
12041228
expect(unpadded.onOffSwitchLabels, true);
@@ -1531,6 +1555,7 @@ void main() {
15311555
accessibleNavigation: true,
15321556
invertColors: true,
15331557
disableAnimations: true,
1558+
reduceMotion: true,
15341559
boldText: true,
15351560
highContrast: true,
15361561
onOffSwitchLabels: true,
@@ -1563,6 +1588,7 @@ void main() {
15631588
expect(subScreenMediaQuery.accessibleNavigation, true);
15641589
expect(subScreenMediaQuery.invertColors, true);
15651590
expect(subScreenMediaQuery.disableAnimations, true);
1591+
expect(subScreenMediaQuery.reduceMotion, true);
15661592
expect(subScreenMediaQuery.boldText, true);
15671593
expect(subScreenMediaQuery.highContrast, true);
15681594
expect(subScreenMediaQuery.onOffSwitchLabels, true);
@@ -1610,6 +1636,7 @@ void main() {
16101636
accessibleNavigation: true,
16111637
invertColors: true,
16121638
disableAnimations: true,
1639+
reduceMotion: true,
16131640
boldText: true,
16141641
highContrast: true,
16151642
onOffSwitchLabels: true,
@@ -1648,6 +1675,7 @@ void main() {
16481675
expect(subScreenMediaQuery.accessibleNavigation, true);
16491676
expect(subScreenMediaQuery.invertColors, true);
16501677
expect(subScreenMediaQuery.disableAnimations, true);
1678+
expect(subScreenMediaQuery.reduceMotion, true);
16511679
expect(subScreenMediaQuery.boldText, true);
16521680
expect(subScreenMediaQuery.highContrast, true);
16531681
expect(subScreenMediaQuery.onOffSwitchLabels, true);
@@ -1929,6 +1957,11 @@ void main() {
19291957
MediaQuery.maybeDisableAnimationsOf,
19301958
MediaQueryData(disableAnimations: true),
19311959
),
1960+
const _MediaQueryAspectCase(MediaQuery.reduceMotionOf, MediaQueryData(reduceMotion: true)),
1961+
const _MediaQueryAspectCase(
1962+
MediaQuery.maybeReduceMotionOf,
1963+
MediaQueryData(reduceMotion: true),
1964+
),
19321965
const _MediaQueryAspectCase(MediaQuery.boldTextOf, MediaQueryData(boldText: true)),
19331966
const _MediaQueryAspectCase(MediaQuery.maybeBoldTextOf, MediaQueryData(boldText: true)),
19341967
const _MediaQueryAspectCase(

0 commit comments

Comments
 (0)