Reduce motion: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: value (line 17), read (line 62), compare (line 86).
1/// What the player has already told the operating system, read as a default
2/// for a camera's own involuntary movement.
3///
4/// **`flutter3d_game`'s real `Accommodations` is not a dependency of this
5/// app.** It is nine lines wrapping `MediaQuery.maybeDisableAnimationsOf`;
6/// this page reimplements exactly that shape and calls the same real
7/// Flutter API, so the numbers below answer to this device's actual
8/// reduce-motion setting, not a stand-in for it.
9///
10/// Quoted by `accommodations.md` and shown whole in the Source tab.
11library;
13import 'package:flutter/widgets.dart';
14import 'package:flutter3d/flutter3d.dart';
15import 'package:flutter3d_showcase/src/demo/demo.dart';
17/// A default, never an override: a game reads this where the player has not
18/// said otherwise, and a slider they moved themselves still wins.
19final class _Accommodations {
20 const _Accommodations({this.reduceMotion = false});
22 factory _Accommodations.of(BuildContext context) => _Accommodations(
23 reduceMotion: MediaQuery.maybeDisableAnimationsOf(context) ?? false,
24 );
26 final bool reduceMotion;
28 /// Nought when the player has asked for less movement: a camera that
29 /// shakes on every landing is exactly what the system setting is about.
30 double get cameraMotion => reduceMotion ? 0.0 : 1.0;
32 /// The same fallback for a full-screen flash, kept separate because a
33 /// flash is a photosensitivity question and a moving camera is a
34 /// vestibular one.
35 double get screenFlash => reduceMotion ? 0.0 : 1.0;
36}
38/// The `accommodations` page: a ball under a camera whose motion follows the
39/// platform's reduce-motion setting. Its check is that reduced motion zeroes
40/// the camera motion default and nothing else does.
41final class AccommodationsDemo extends ShowcaseDemo {
43 Scene build(DemoContext context) {
44 final material = RenderMaterial(
45 name: 'ball',
46 baseColor: LinearColor.fromSrgb(0.8, 0.6, 0.4, 1.0),
47 );
48 final node = MeshNode(
49 DeviceMesh.upload(context.device, SphereShape(segments: 16).build()),
50 material,
51 );
52 return Scene()
53 ..add(node)
54 ..add(
55 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
56 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
57 );
58 }
61 Widget? customBody(BuildContext buildContext, DemoContext context) {
62 final accommodations = _Accommodations.of(buildContext);
63 return Container(
64 color: const Color(0xFF14161A),
65 padding: const EdgeInsets.all(24),
66 alignment: Alignment.topLeft,
67 child: DefaultTextStyle(
68 style: const TextStyle(color: Color(0xFFE8E8EC), fontSize: 16),
69 child: Text(
70 'this device asks for reduced motion: '
71 '${accommodations.reduceMotion}\n'
72 'a camera rig would default its own motion to '
73 '${accommodations.cameraMotion}\n'
74 'a full-screen flash would default to '
75 '${accommodations.screenFlash}',
76 ),
77 ),
78 );
79 }
82 void verify(Scene scene, FrameResult frame) {
83 if (frame.drawCalls < 1) {
84 throw StateError('the ball was not drawn');
85 }
86 const off = _Accommodations();
87 const on = _Accommodations(reduceMotion: true);
88 if (off.cameraMotion != 1.0 || on.cameraMotion != 0.0) {
89 throw StateError(
90 'reduced motion should zero the camera motion '
91 'default and nothing else should',
92 );
93 }
94 }
95}