Skip to content

Commit

Permalink
[animations] Fixed useRootNavigator to use correct Navigator size (fl…
Browse files Browse the repository at this point in the history
…utter#159)

* Fixed useRootNavigator to use correct size

Co-authored-by: Shi-Hao Hong <[email protected]>
  • Loading branch information
frmatthew and Shi-Hao Hong authored May 27, 2020
1 parent 18b52ae commit 1df8f1e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/animations/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
and tappable when it is supposed to be hidden.
* Add custom fillColor property to `SharedAxisTransition` and `SharedAxisPageTransitionsBuilder`.
* Fix prefer_const_constructors lint in test and example.
* Add option `useRootNavigator` to `OpenContainer`.


## [1.0.0+5] - February 21, 2020
Expand Down
17 changes: 13 additions & 4 deletions packages/animations/lib/src/open_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ class _OpenContainerState extends State<OpenContainer> {
final GlobalKey _closedBuilderKey = GlobalKey();

Future<void> openContainer() async {
await Navigator.of(context, rootNavigator: widget.useRootNavigator)
.push(_OpenContainerRoute(
await Navigator.of(
context,
rootNavigator: widget.useRootNavigator,
).push(_OpenContainerRoute(
closedColor: widget.closedColor,
openColor: widget.openColor,
closedElevation: widget.closedElevation,
Expand All @@ -249,6 +251,7 @@ class _OpenContainerState extends State<OpenContainer> {
closedBuilderKey: _closedBuilderKey,
transitionDuration: widget.transitionDuration,
transitionType: widget.transitionType,
useRootNavigator: widget.useRootNavigator,
));
if (widget.onClosed != null) {
widget.onClosed();
Expand Down Expand Up @@ -361,6 +364,7 @@ class _OpenContainerRoute extends ModalRoute<void> {
@required this.closedBuilderKey,
@required this.transitionDuration,
@required this.transitionType,
@required this.useRootNavigator,
}) : assert(closedColor != null),
assert(openColor != null),
assert(closedElevation != null),
Expand All @@ -371,6 +375,7 @@ class _OpenContainerRoute extends ModalRoute<void> {
assert(hideableKey != null),
assert(closedBuilderKey != null),
assert(transitionType != null),
assert(useRootNavigator != null),
_elevationTween = Tween<double>(
begin: closedElevation,
end: openElevation,
Expand Down Expand Up @@ -514,6 +519,8 @@ class _OpenContainerRoute extends ModalRoute<void> {
final Duration transitionDuration;
final ContainerTransitionType transitionType;

final bool useRootNavigator;

final Tween<double> _elevationTween;
final ShapeBorderTween _shapeTween;
final _FlippableTweenSequence<double> _closedOpacityTween;
Expand Down Expand Up @@ -592,8 +599,10 @@ class _OpenContainerRoute extends ModalRoute<void> {
BuildContext navigatorContext,
bool delayForSourceRoute = false,
}) {
final RenderBox navigator =
Navigator.of(navigatorContext).context.findRenderObject();
final RenderBox navigator = Navigator.of(
navigatorContext,
rootNavigator: useRootNavigator,
).context.findRenderObject();
final Size navSize = _getSize(navigator);
_rectTween.end = Offset.zero & navSize;

Expand Down
89 changes: 69 additions & 20 deletions packages/animations/test/open_container_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1530,26 +1530,41 @@ void main() {
@required Key nestedNavigatorKey,
@required bool useRootNavigator,
}) {
return MaterialApp(
key: appKey,
// a nested navigator
home: Navigator(
key: nestedNavigatorKey,
onGenerateRoute: (RouteSettings route) {
return MaterialPageRoute<dynamic>(
settings: route,
builder: (BuildContext context) {
return Container(
child: OpenContainer(
useRootNavigator: useRootNavigator,
closedBuilder: (BuildContext context, _) {
return const Text('Closed');
},
openBuilder: (BuildContext context, _) {
return const Text('Opened');
}));
});
}));
return Center(
child: SizedBox(
width: 100,
height: 100,
child: MaterialApp(
key: appKey,
// a nested navigator
home: Center(
child: SizedBox(
width: 50,
height: 50,
child: Navigator(
key: nestedNavigatorKey,
onGenerateRoute: (RouteSettings route) {
return MaterialPageRoute<dynamic>(
settings: route,
builder: (BuildContext context) {
return OpenContainer(
useRootNavigator: useRootNavigator,
closedBuilder: (BuildContext context, _) {
return const Text('Closed');
},
openBuilder: (BuildContext context, _) {
return const Text('Opened');
},
);
},
);
},
),
),
),
),
),
);
}

testWidgets(
Expand Down Expand Up @@ -1598,6 +1613,40 @@ void main() {
of: find.byKey(nestedNavigatorKey), matching: find.text('Opened')),
findsNothing);
});

testWidgets('Verify correct opened size when "useRootNavigator: false"',
(WidgetTester tester) async {
const Key appKey = Key('App');
const Key nestedNavigatorKey = Key('Nested Navigator');

await tester.pumpWidget(_createRootNavigatorTest(
appKey: appKey,
nestedNavigatorKey: nestedNavigatorKey,
useRootNavigator: false));

await tester.tap(find.text('Closed'));
await tester.pumpAndSettle();

expect(tester.getSize(find.text('Opened')),
equals(tester.getSize(find.byKey(nestedNavigatorKey))));
});

testWidgets('Verify correct opened size when "useRootNavigator: true"',
(WidgetTester tester) async {
const Key appKey = Key('App');
const Key nestedNavigatorKey = Key('Nested Navigator');

await tester.pumpWidget(_createRootNavigatorTest(
appKey: appKey,
nestedNavigatorKey: nestedNavigatorKey,
useRootNavigator: true));

await tester.tap(find.text('Closed'));
await tester.pumpAndSettle();

expect(tester.getSize(find.text('Opened')),
equals(tester.getSize(find.byKey(appKey))));
});
}

Color _getScrimColor(WidgetTester tester) {
Expand Down

0 comments on commit 1df8f1e

Please sign in to comment.