Skip to content

Commit 2a1e448

Browse files
aaazlkmflutter-zl
authored andcommitted
Enhance Hero widget with customizable animation curves (flutter#180100)
<!-- 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 --> Follow-up to flutter#159393 (closed due to inactivity). Implements the same feature with changes based on the previous review feedback. Related issue: flutter#26150 ## Screenshot Using elasticOut because the difference was hard to see with fastOutSlowIn. <table> <tr> <th>curve=elasticOut, reverseCurve=null</th> <th>curve=elasticOut, reverseCurve=linear</th> </tr> <tr> <td> <video src="https://github.com/user-attachments/assets/c8c39fb0-e677-471e-ac2a-2a062277ae48" controls muted loop width="320"></video> </td> <td> <video src="https://github.com/user-attachments/assets/1f39d0a8-ed80-4404-b766-38844cca3909" controls muted loop width="320"></video> </td> </tr> </table> ## 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. - [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]. - [ ] 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]. **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 [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 2837495 commit 2a1e448

2 files changed

Lines changed: 272 additions & 5 deletions

File tree

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ class Hero extends StatefulWidget {
178178
this.flightShuttleBuilder,
179179
this.placeholderBuilder,
180180
this.transitionOnUserGestures = false,
181+
this.curve = Curves.fastOutSlowIn,
182+
this.reverseCurve,
181183
required this.child,
182184
});
183185

@@ -261,6 +263,16 @@ class Hero extends StatefulWidget {
261263
/// Defaults to false.
262264
final bool transitionOnUserGestures;
263265

266+
/// The curve to use in the forward direction.
267+
///
268+
/// Defaults to [Curves.fastOutSlowIn].
269+
final Curve curve;
270+
271+
/// The curve to use in the reverse direction.
272+
///
273+
/// If this property is null, [Hero.curve].flipped is used.
274+
final Curve? reverseCurve;
275+
264276
// Returns a map of all of the heroes in `context` indexed by hero tag that
265277
// should be considered for animation when `navigator` transitions from one
266278
// PageRoute to another.
@@ -459,10 +471,23 @@ class _HeroFlightManifest {
459471
CurvedAnimation? _animation;
460472

461473
Animation<double> get animation {
474+
final Curve curve, reverseCurve;
475+
final Animation<double> parent;
476+
switch (type) {
477+
case HeroFlightDirection.push:
478+
parent = toRoute.animation!;
479+
curve = toHero.widget.curve;
480+
reverseCurve = toHero.widget.reverseCurve ?? curve.flipped;
481+
case HeroFlightDirection.pop:
482+
parent = fromRoute.animation!;
483+
curve = fromHero.widget.curve;
484+
reverseCurve = fromHero.widget.reverseCurve ?? curve.flipped;
485+
}
486+
462487
return _animation ??= CurvedAnimation(
463-
parent: (type == HeroFlightDirection.push) ? toRoute.animation! : fromRoute.animation!,
464-
curve: Curves.fastOutSlowIn,
465-
reverseCurve: isDiverted ? null : Curves.fastOutSlowIn.flipped,
488+
parent: parent,
489+
curve: curve,
490+
reverseCurve: isDiverted ? null : reverseCurve,
466491
);
467492
}
468493

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

Lines changed: 244 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,114 @@ Future<ui.Image> createTestImage() {
2424
return picture.toImage(300, 300);
2525
}
2626

27+
Future<void> verifyPoppedHeroCurve({
28+
required WidgetTester tester,
29+
required Curve curve,
30+
required Curve? reverseCurve,
31+
}) async {
32+
final Key container1 = UniqueKey();
33+
final Key container2 = UniqueKey();
34+
final navigator = GlobalKey<NavigatorState>();
35+
final observer = TransitionDurationObserver();
36+
37+
await tester.pumpWidget(
38+
MaterialApp(
39+
navigatorKey: navigator,
40+
navigatorObservers: <NavigatorObserver>[observer],
41+
home: Scaffold(
42+
body: Center(
43+
child: Hero(
44+
tag: 'test',
45+
createRectTween: (Rect? begin, Rect? end) {
46+
return RectTween(begin: begin, end: end);
47+
},
48+
curve: curve,
49+
reverseCurve: reverseCurve?.flipped,
50+
child: SizedBox(key: container1, height: 100, width: 100),
51+
),
52+
),
53+
),
54+
),
55+
);
56+
final Size originalSize = tester.getSize(find.byKey(container1));
57+
expect(originalSize, const Size(100, 100));
58+
59+
navigator.currentState!.push(
60+
MaterialPageRoute<void>(
61+
builder: (BuildContext context) {
62+
return Scaffold(
63+
body: Center(
64+
child: Hero(
65+
tag: 'test',
66+
createRectTween: (Rect? begin, Rect? end) {
67+
return RectTween(begin: begin, end: end);
68+
},
69+
curve: curve,
70+
reverseCurve: reverseCurve?.flipped,
71+
child: SizedBox(key: container2, height: 200, width: 200),
72+
),
73+
),
74+
);
75+
},
76+
),
77+
);
78+
await tester.pumpAndSettle();
79+
final Size newSize = tester.getSize(find.byKey(container2));
80+
expect(newSize, const Size(200, 200));
81+
82+
navigator.currentState!.pop();
83+
await tester.pump();
84+
85+
final Duration duration = observer.transitionDuration;
86+
final Curve expectedCurve = reverseCurve ?? curve;
87+
final expectedCurveName = expectedCurve.toString();
88+
const epsilon = 0.001;
89+
final Animatable<Size?> tween = SizeTween(
90+
begin: newSize,
91+
end: originalSize,
92+
).chain(CurveTween(curve: expectedCurve));
93+
94+
// Jump 25% into the transition.
95+
await tester.pump(duration ~/ 4);
96+
Size heroSize = tester.getSize(find.byKey(container1));
97+
Size expectedSize = tween.transform(0.25)!;
98+
expect(
99+
heroSize,
100+
within<Size>(distance: epsilon, from: expectedSize),
101+
reason: 'curve=$expectedCurveName, t=0.25, duration=$duration',
102+
);
103+
104+
// Jump to 50% into the transition.
105+
await tester.pump(duration ~/ 4);
106+
heroSize = tester.getSize(find.byKey(container1));
107+
expectedSize = tween.transform(0.50)!;
108+
expect(
109+
heroSize,
110+
within<Size>(distance: epsilon, from: expectedSize),
111+
reason: 'curve=$expectedCurveName, t=0.50, duration=$duration',
112+
);
113+
114+
// Jump to 75% into the transition.
115+
await tester.pump(duration ~/ 4);
116+
heroSize = tester.getSize(find.byKey(container1));
117+
expectedSize = tween.transform(0.75)!;
118+
expect(
119+
heroSize,
120+
within<Size>(distance: epsilon, from: expectedSize),
121+
reason: 'curve=$expectedCurveName, t=0.75, duration=$duration',
122+
);
123+
124+
// Jump to 100% into the transition.
125+
await tester.pump(duration ~/ 4);
126+
heroSize = tester.getSize(find.byKey(container1));
127+
expectedSize = tween.transform(1.0)!;
128+
expect(
129+
heroSize,
130+
within<Size>(distance: epsilon, from: expectedSize),
131+
reason: 'curve=$expectedCurveName, t=1.00, duration=$duration',
132+
);
133+
}
134+
27135
Key firstKey = const Key('first');
28136
Key secondKey = const Key('second');
29137
Key thirdKey = const Key('third');
@@ -620,7 +728,7 @@ Future<void> main() async {
620728
await tester.pump(const Duration(seconds: 1));
621729
});
622730

623-
testWidgets('Heroes animation is fastOutSlowIn', (WidgetTester tester) async {
731+
testWidgets('Default Hero animation is fastOutSlowIn', (WidgetTester tester) async {
624732
final observer = TransitionDurationObserver();
625733
await tester.pumpWidget(
626734
MaterialApp(navigatorObservers: <NavigatorObserver>[observer], routes: routes),
@@ -663,6 +771,112 @@ Future<void> main() async {
663771
);
664772
});
665773

774+
for (final curve in <Curve>[
775+
Curves.linear,
776+
Curves.ease,
777+
Curves.linearToEaseOut,
778+
Curves.easeInOutCubicEmphasized,
779+
Curves.fastOutSlowIn,
780+
]) {
781+
testWidgets('Heroes animation curve is customizable with curve=$curve', (
782+
WidgetTester tester,
783+
) async {
784+
final observer = TransitionDurationObserver();
785+
await tester.pumpWidget(
786+
MaterialApp(
787+
navigatorObservers: <NavigatorObserver>[observer],
788+
routes: <String, WidgetBuilder>{
789+
'/': (BuildContext context) => Material(
790+
child: ListView(
791+
children: <Widget>[
792+
const SizedBox(height: 100.0, width: 100.0),
793+
Card(
794+
child: Hero(
795+
tag: 'a',
796+
transitionOnUserGestures: transitionFromUserGestures,
797+
curve: curve,
798+
child: SizedBox(height: 100.0, width: 100.0, key: firstKey),
799+
),
800+
),
801+
const SizedBox(height: 100.0, width: 100.0),
802+
TextButton(
803+
child: const Text('two'),
804+
onPressed: () {
805+
Navigator.pushNamed(context, '/two');
806+
},
807+
),
808+
],
809+
),
810+
),
811+
'/two': (BuildContext context) => Material(
812+
child: ListView(
813+
key: routeTwoKey,
814+
children: <Widget>[
815+
TextButton(
816+
child: const Text('pop'),
817+
onPressed: () {
818+
Navigator.pop(context);
819+
},
820+
),
821+
const SizedBox(height: 150.0, width: 150.0),
822+
Card(
823+
child: Hero(
824+
tag: 'a',
825+
transitionOnUserGestures: transitionFromUserGestures,
826+
curve: curve,
827+
child: SizedBox(height: 150.0, width: 150.0, key: secondKey),
828+
),
829+
),
830+
],
831+
),
832+
),
833+
},
834+
),
835+
);
836+
837+
await tester.tap(find.text('two'));
838+
await tester.pump(); // begin navigation
839+
840+
// Expect the height of the secondKey Hero to vary from 100 to 150
841+
// over duration and according to curve.
842+
843+
final Duration duration = observer.transitionDuration;
844+
final double initialHeight = tester.getSize(find.byKey(firstKey, skipOffstage: false)).height;
845+
final double finalHeight = tester.getSize(find.byKey(secondKey, skipOffstage: false)).height;
846+
final double deltaHeight = finalHeight - initialHeight;
847+
const epsilon = 0.001;
848+
final curveName = curve.toString();
849+
850+
await tester.pump(duration * 0.25);
851+
expect(
852+
tester.getSize(find.byKey(secondKey)).height,
853+
moreOrLessEquals(curve.transform(0.25) * deltaHeight + initialHeight, epsilon: epsilon),
854+
reason: 'curve=$curveName t=0.25 duration=$duration',
855+
);
856+
857+
await tester.pump(duration * 0.25);
858+
expect(
859+
tester.getSize(find.byKey(secondKey)).height,
860+
moreOrLessEquals(curve.transform(0.50) * deltaHeight + initialHeight, epsilon: epsilon),
861+
reason: 'curve=$curveName t=0.50 duration=$duration',
862+
);
863+
864+
await tester.pump(duration * 0.25);
865+
expect(
866+
tester.getSize(find.byKey(secondKey)).height,
867+
moreOrLessEquals(curve.transform(0.75) * deltaHeight + initialHeight, epsilon: epsilon),
868+
reason: 'curve=$curveName t=0.75 duration=$duration',
869+
);
870+
871+
await tester.pump(duration * 0.25);
872+
expect(
873+
tester.getSize(find.byKey(secondKey)).height,
874+
moreOrLessEquals(curve.transform(1.0) * deltaHeight + initialHeight, epsilon: epsilon),
875+
reason: 'curve=$curveName t=1.00 duration=$duration',
876+
);
877+
});
878+
}
879+
666880
testWidgets('Heroes are not interactive', (WidgetTester tester) async {
667881
final log = <String>[];
668882

@@ -3182,7 +3396,7 @@ Future<void> main() async {
31823396
},
31833397
);
31843398

3185-
testWidgets('popped hero uses fastOutSlowIn curve', (WidgetTester tester) async {
3399+
testWidgets('Default popped hero uses fastOutSlowIn curve', (WidgetTester tester) async {
31863400
final Key container1 = UniqueKey();
31873401
final Key container2 = UniqueKey();
31883402
final navigator = GlobalKey<NavigatorState>();
@@ -3258,6 +3472,34 @@ Future<void> main() async {
32583472
expect(heroSize, tween.transform(1.0));
32593473
});
32603474

3475+
for (final reverseCurve in <Curve>[
3476+
Curves.linear,
3477+
Curves.ease,
3478+
Curves.linearToEaseOut,
3479+
Curves.easeInOutCubicEmphasized,
3480+
Curves.fastOutSlowIn,
3481+
]) {
3482+
testWidgets('popped hero curve is customizable with reverseCurve=$reverseCurve', (
3483+
WidgetTester tester,
3484+
) async {
3485+
await verifyPoppedHeroCurve(tester: tester, curve: Curves.linear, reverseCurve: reverseCurve);
3486+
});
3487+
}
3488+
3489+
for (final curve in <Curve>[
3490+
Curves.linear,
3491+
Curves.ease,
3492+
Curves.linearToEaseOut,
3493+
Curves.easeInOutCubicEmphasized,
3494+
Curves.fastOutSlowIn,
3495+
]) {
3496+
testWidgets('popped hero uses flipped curve=$curve when reverseCurve is not specified', (
3497+
WidgetTester tester,
3498+
) async {
3499+
await verifyPoppedHeroCurve(tester: tester, curve: curve, reverseCurve: null);
3500+
});
3501+
}
3502+
32613503
testWidgets('Heroes in enabled HeroMode do transition', (WidgetTester tester) async {
32623504
await tester.pumpWidget(
32633505
MaterialApp(

0 commit comments

Comments
 (0)