Visitar URL original
PageTransitionSwitcher by goderbauer · Pull Request #50 · flutter/packages · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
format
  • Loading branch information
goderbauer committed Dec 2, 2019
commit d62a35b29cd4079a34be43eeae8b9934cbaa12ad
25 changes: 19 additions & 6 deletions packages/animations/lib/src/page_transition_switcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ class PageTransitionSwitcher extends StatefulWidget {
_PageTransitionSwitcherState createState() => _PageTransitionSwitcherState();
}

class _PageTransitionSwitcherState extends State<PageTransitionSwitcher> with TickerProviderStateMixin {
class _PageTransitionSwitcherState extends State<PageTransitionSwitcher>
with TickerProviderStateMixin {
final List<_ChildEntry> _activeEntries = <_ChildEntry>[];
_ChildEntry _currentEntry;
int _childNumber = 0;
Expand All @@ -179,7 +180,9 @@ class _PageTransitionSwitcherState extends State<PageTransitionSwitcher> with Ti

final bool hasNewChild = widget.child != null;
final bool hasOldChild = _currentEntry != null;
if (hasNewChild != hasOldChild || hasNewChild && !Widget.canUpdate(widget.child, _currentEntry.widgetChild)) {
if (hasNewChild != hasOldChild ||
hasNewChild &&
!Widget.canUpdate(widget.child, _currentEntry.widgetChild)) {
// Child has changed, fade current entry out and add new entry.
_childNumber += 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the transition duration was 1ms, then this value will overflow in about 292 million years. Probably a safe bet :-).

_addEntryForNewChild(animate: true);
Expand All @@ -206,8 +209,9 @@ class _PageTransitionSwitcherState extends State<PageTransitionSwitcher> with Ti
}
_currentEntry = null;
}
if (widget.child == null)
if (widget.child == null) {
Comment thread
shihaohong marked this conversation as resolved.
Resolved
return;
}
final AnimationController primaryController = AnimationController(
duration: widget.duration,
vsync: this,
Expand Down Expand Up @@ -251,7 +255,10 @@ class _PageTransitionSwitcherState extends State<PageTransitionSwitcher> with Ti
}) {
final _ChildEntry entry = _ChildEntry(
widgetChild: child,
transition: KeyedSubtree.wrap(builder(child, primaryController, secondaryController), _childNumber),
transition: KeyedSubtree.wrap(

@shihaohong shihaohong Dec 4, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main purpose of KeyedSubtree here is to avoid any complete replacements of the child when PageTransitionSwitcher's transition is updated right? I'm not too familiar with KeyedSubtree, so I just want to make sure I'm getting its intent right.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

builder(child, primaryController, secondaryController),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe assert that the builder's return value is non-null here and provide a nice error message

_childNumber,
),
primaryController: primaryController,
secondaryController: secondaryController,
);
Expand Down Expand Up @@ -281,7 +288,11 @@ class _PageTransitionSwitcherState extends State<PageTransitionSwitcher> with Ti
void _updateTransitionForEntry(_ChildEntry entry) {
entry.transition = KeyedSubtree(
key: entry.transition.key,
child: widget.transitionBuilder(entry.widgetChild, entry.primaryController, entry.secondaryController),
child: widget.transitionBuilder(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe assert that the child is non-null here and provide a nice error message

entry.widgetChild,
entry.primaryController,
entry.secondaryController,
),
);
}

Expand All @@ -296,7 +307,9 @@ class _PageTransitionSwitcherState extends State<PageTransitionSwitcher> with Ti
@override
Widget build(BuildContext context) {
return Stack(
children: _activeEntries.map<Widget>((_ChildEntry entry) => entry.transition).toList(),
children: _activeEntries
.map<Widget>((_ChildEntry entry) => entry.transition)
.toList(),
alignment: Alignment.center,
);
}
Expand Down
120 changes: 80 additions & 40 deletions packages/animations/test/page_transition_switcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ void main() {
transitionBuilder: _transitionBuilder,
),
);

Map<Key, double> _primaryAnimation = _getPrimaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne], tester);

Map<Key, double> _primaryAnimation =
_getPrimaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _secondaryAnimation =
_getSecondaryAnimation(<Key>[containerOne], tester);
expect(_primaryAnimation[containerOne], moreOrLessEquals(1.0));
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.0));

Expand All @@ -33,8 +35,10 @@ void main() {
);
await tester.pump(const Duration(milliseconds: 40));

_primaryAnimation = _getPrimaryAnimation(<Key>[containerOne, containerTwo], tester);
_secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne, containerTwo], tester);
_primaryAnimation =
_getPrimaryAnimation(<Key>[containerOne, containerTwo], tester);
_secondaryAnimation =
_getSecondaryAnimation(<Key>[containerOne, containerTwo], tester);
// Secondary is running for outgoing widget.
expect(_primaryAnimation[containerOne], moreOrLessEquals(1.0));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe moreOrLessEquals isn't needed for cases were we believe the animation's value will have reached 0 or 1

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed where not needed.

expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.4));
Expand All @@ -55,8 +59,10 @@ void main() {
);
await tester.pump(const Duration(milliseconds: 20));

_primaryAnimation = _getPrimaryAnimation(<Key>[containerOne, containerTwo, containerThree], tester);
_secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne, containerTwo, containerThree], tester);
_primaryAnimation = _getPrimaryAnimation(
<Key>[containerOne, containerTwo, containerThree], tester);
_secondaryAnimation = _getSecondaryAnimation(
<Key>[containerOne, containerTwo, containerThree], tester);
expect(_primaryAnimation[containerOne], moreOrLessEquals(1.0));
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.6));
expect(_primaryAnimation[containerTwo], moreOrLessEquals(0.6));
Expand All @@ -66,7 +72,8 @@ void main() {
await tester.pumpAndSettle();
});

testWidgets('transitions in a new child in reverse.', (WidgetTester tester) async {
testWidgets('transitions in a new child in reverse.',
(WidgetTester tester) async {
final UniqueKey containerOne = UniqueKey();
final UniqueKey containerTwo = UniqueKey();
final UniqueKey containerThree = UniqueKey();
Expand All @@ -79,8 +86,10 @@ void main() {
),
);

Map<Key, double> _primaryAnimation = _getPrimaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _primaryAnimation =
_getPrimaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _secondaryAnimation =
_getSecondaryAnimation(<Key>[containerOne], tester);
expect(_primaryAnimation[containerOne], moreOrLessEquals(1.0));
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.0));

Expand All @@ -94,8 +103,10 @@ void main() {
);
await tester.pump(const Duration(milliseconds: 40));

_primaryAnimation = _getPrimaryAnimation(<Key>[containerOne, containerTwo], tester);
_secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne, containerTwo], tester);
_primaryAnimation =
_getPrimaryAnimation(<Key>[containerOne, containerTwo], tester);
_secondaryAnimation =
_getSecondaryAnimation(<Key>[containerOne, containerTwo], tester);
// Primary is running for outgoing widget.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Primary is running for outgoing widget.
// Primary is running forward for outgoing widget.

expect(_primaryAnimation[containerOne], moreOrLessEquals(0.6));
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.0));
Expand All @@ -117,8 +128,10 @@ void main() {
);
await tester.pump(const Duration(milliseconds: 20));

_primaryAnimation = _getPrimaryAnimation(<Key>[containerOne, containerTwo, containerThree], tester);
_secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne, containerTwo, containerThree], tester);
_primaryAnimation = _getPrimaryAnimation(
<Key>[containerOne, containerTwo, containerThree], tester);
_secondaryAnimation = _getSecondaryAnimation(
<Key>[containerOne, containerTwo, containerThree], tester);
expect(_primaryAnimation[containerOne], moreOrLessEquals(0.4));
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.0));
expect(_primaryAnimation[containerTwo], moreOrLessEquals(0.8));
Expand All @@ -140,8 +153,10 @@ void main() {
),
);

Map<Key, double> _primaryAnimation = _getPrimaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _primaryAnimation =
_getPrimaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _secondaryAnimation =
_getSecondaryAnimation(<Key>[containerOne], tester);
expect(_primaryAnimation[containerOne], moreOrLessEquals(1.0));
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.0));

Expand All @@ -154,8 +169,10 @@ void main() {
);
await tester.pump(const Duration(milliseconds: 40));

_primaryAnimation = _getPrimaryAnimation(<Key>[containerOne, containerTwo], tester);
_secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne, containerTwo], tester);
_primaryAnimation =
_getPrimaryAnimation(<Key>[containerOne, containerTwo], tester);
_secondaryAnimation =
_getSecondaryAnimation(<Key>[containerOne, containerTwo], tester);
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.4));
expect(_primaryAnimation[containerOne], moreOrLessEquals(1.0));
expect(_secondaryAnimation[containerTwo], moreOrLessEquals(0.0));
Expand All @@ -171,8 +188,10 @@ void main() {
);
await tester.pump(const Duration(milliseconds: 20));

_primaryAnimation = _getPrimaryAnimation(<Key>[containerOne, containerTwo, containerThree], tester);
_secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne, containerTwo, containerThree], tester);
_primaryAnimation = _getPrimaryAnimation(
<Key>[containerOne, containerTwo, containerThree], tester);
_secondaryAnimation = _getSecondaryAnimation(
<Key>[containerOne, containerTwo, containerThree], tester);
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.6));
expect(_primaryAnimation[containerOne], moreOrLessEquals(1.0));
expect(_secondaryAnimation[containerTwo], moreOrLessEquals(0.0));
Expand All @@ -195,8 +214,10 @@ void main() {
),
);

Map<Key, double> _primaryAnimation = _getPrimaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _primaryAnimation =
_getPrimaryAnimation(<Key>[containerOne], tester);
Map<Key, double> _secondaryAnimation =
_getSecondaryAnimation(<Key>[containerOne], tester);
expect(_primaryAnimation[containerOne], moreOrLessEquals(1.0));
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.0));

Expand All @@ -210,8 +231,10 @@ void main() {
);
await tester.pump(const Duration(milliseconds: 40));

_primaryAnimation = _getPrimaryAnimation(<Key>[containerOne, containerTwo], tester);
_secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne, containerTwo], tester);
_primaryAnimation =
_getPrimaryAnimation(<Key>[containerOne, containerTwo], tester);
_secondaryAnimation =
_getSecondaryAnimation(<Key>[containerOne, containerTwo], tester);
// Primary is running for outgoing widget.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Primary is running for outgoing widget.
// Primary is running in reverse for outgoing widget.

expect(_primaryAnimation[containerOne], moreOrLessEquals(0.6));
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.0));
Expand All @@ -233,8 +256,10 @@ void main() {
);
await tester.pump(const Duration(milliseconds: 20));

_primaryAnimation = _getPrimaryAnimation(<Key>[containerOne, containerTwo, containerThree], tester);
_secondaryAnimation = _getSecondaryAnimation(<Key>[containerOne, containerTwo, containerThree], tester);
_primaryAnimation = _getPrimaryAnimation(
<Key>[containerOne, containerTwo, containerThree], tester);
_secondaryAnimation = _getSecondaryAnimation(
<Key>[containerOne, containerTwo, containerThree], tester);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe comment the expectation for this particular case since it's pretty unique. Like:

    // Container one is expected to continue running its primary animation in reverse since it is exiting.
    // Container two's secondary animation switches from running its secondary animation in reverse to
    // running forwards since it should now be exiting underneath container three. Container three's primary 
    // animation should be running forwards since it is entering above container two.

expect(_primaryAnimation[containerOne], moreOrLessEquals(0.4));
expect(_secondaryAnimation[containerOne], moreOrLessEquals(0.0));
expect(_primaryAnimation[containerTwo], moreOrLessEquals(1.0));
Expand All @@ -244,7 +269,8 @@ void main() {
await tester.pumpAndSettle();
});

testWidgets("doesn't transition in a new child of the same type.", (WidgetTester tester) async {
testWidgets("doesn't transition in a new child of the same type.",
(WidgetTester tester) async {
await tester.pumpWidget(
PageTransitionSwitcher(
duration: const Duration(milliseconds: 100),
Expand Down Expand Up @@ -325,7 +351,8 @@ void main() {
await tester.pumpAndSettle();
});

testWidgets("doesn't start any animations after dispose.", (WidgetTester tester) async {
testWidgets("doesn't start any animations after dispose.",
(WidgetTester tester) async {
await tester.pumpWidget(
PageTransitionSwitcher(
duration: const Duration(milliseconds: 100),
Expand All @@ -346,16 +373,19 @@ void main() {
expect(find.byType(FadeTransition), findsNWidgets(2));
expect(find.byType(ScaleTransition), findsNWidgets(2));
final FadeTransition fade = tester.firstWidget(find.byType(FadeTransition));
final ScaleTransition scale = tester.firstWidget(find.byType(ScaleTransition));
final ScaleTransition scale =
tester.firstWidget(find.byType(ScaleTransition));
expect(fade.opacity.value, moreOrLessEquals(0.5));
expect(scale.scale.value, moreOrLessEquals(1.0));

// Change the widget tree in the middle of the animation.
await tester.pumpWidget(Container(color: const Color(0xffff0000)));
expect(await tester.pumpAndSettle(const Duration(milliseconds: 100)), equals(1));
expect(await tester.pumpAndSettle(const Duration(milliseconds: 100)),
equals(1));
});

testWidgets("doesn't reset state of the children in transitions.", (WidgetTester tester) async {
testWidgets("doesn't reset state of the children in transitions.",
(WidgetTester tester) async {
final UniqueKey statefulOne = UniqueKey();
final UniqueKey statefulTwo = UniqueKey();
final UniqueKey statefulThree = UniqueKey();
Expand All @@ -370,8 +400,10 @@ void main() {
),
);

Map<Key, double> _primaryAnimation = _getPrimaryAnimation(<Key>[statefulOne], tester);
Map<Key, double> _secondaryAnimation = _getSecondaryAnimation(<Key>[statefulOne], tester);
Map<Key, double> _primaryAnimation =
_getPrimaryAnimation(<Key>[statefulOne], tester);
Map<Key, double> _secondaryAnimation =
_getSecondaryAnimation(<Key>[statefulOne], tester);
expect(_primaryAnimation[statefulOne], equals(1.0));
expect(_secondaryAnimation[statefulOne], equals(0.0));
expect(StatefulTestState.generation, equals(1));
Expand All @@ -386,8 +418,10 @@ void main() {

await tester.pump(const Duration(milliseconds: 50));
expect(find.byType(FadeTransition), findsNWidgets(2));
_primaryAnimation = _getPrimaryAnimation(<Key>[statefulOne, statefulTwo], tester);
_secondaryAnimation = _getSecondaryAnimation(<Key>[statefulOne, statefulTwo], tester);
_primaryAnimation =
_getPrimaryAnimation(<Key>[statefulOne, statefulTwo], tester);
_secondaryAnimation =
_getSecondaryAnimation(<Key>[statefulOne, statefulTwo], tester);
expect(_primaryAnimation[statefulTwo], equals(0.5));
expect(_secondaryAnimation[statefulTwo], equals(0.0));
expect(StatefulTestState.generation, equals(2));
Expand All @@ -406,7 +440,8 @@ void main() {
expect(StatefulTestState.generation, equals(3));
});

testWidgets('updates widgets without animating if they are isomorphic.', (WidgetTester tester) async {
testWidgets('updates widgets without animating if they are isomorphic.',
(WidgetTester tester) async {
Future<void> pumpChild(Widget child) async {
return tester.pumpWidget(
Directionality(
Expand Down Expand Up @@ -438,7 +473,9 @@ void main() {
expect(find.text('2'), findsOneWidget);
});

testWidgets('updates previous child transitions if the transitionBuilder changes.', (WidgetTester tester) async {
testWidgets(
'updates previous child transitions if the transitionBuilder changes.',
(WidgetTester tester) async {
final UniqueKey containerOne = UniqueKey();
final UniqueKey containerTwo = UniqueKey();
final UniqueKey containerThree = UniqueKey();
Expand Down Expand Up @@ -488,9 +525,11 @@ void main() {
expect(find.byType(SlideTransition), findsNothing);
expect(find.byType(SizeTransition), findsNothing);

Widget newTransitionBuilder(Widget child, Animation<double> primary, Animation<double> secondary) {
Widget newTransitionBuilder(
Widget child, Animation<double> primary, Animation<double> secondary) {
return SlideTransition(
position: Tween<Offset>(begin: Offset.zero, end: const Offset(20, 30)).animate(primary),
position: Tween<Offset>(begin: Offset.zero, end: const Offset(20, 30))
.animate(primary),
child: SizeTransition(
sizeFactor: Tween<double>(begin: 10, end: 0.0).animate(secondary),
child: child,
Expand Down Expand Up @@ -541,7 +580,8 @@ class StatefulTestState extends State<StatefulTest> {
Widget build(BuildContext context) => Container();
}

Widget _transitionBuilder(Widget child, Animation<double> primary, Animation<double> secondary) {
Widget _transitionBuilder(
Widget child, Animation<double> primary, Animation<double> secondary) {
return ScaleTransition(
scale: Tween<double>(begin: 0.0, end: 1.0).animate(primary),
child: FadeTransition(
Expand Down