Visitar URL original
Adds animations package with open container transition by goderbauer · Pull Request #30 · 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
review
  • Loading branch information
goderbauer committed Sep 5, 2019
commit bb15abc1194c2fb25edf4239c79953206081f84e
76 changes: 64 additions & 12 deletions packages/animations/lib/src/open_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ class OpenContainer extends StatefulWidget {
this.closedShape = const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(4.0)),
),
this.openShape = const RoundedRectangleBorder(),
@required this.closedBuilder,
@required this.openBuilder,
this.tappable = true,
this.transitionDuration = const Duration(milliseconds: 300),
}) : assert(closedColor != null),
assert(openColor != null),
assert(closedElevation != null),
assert(openElevation != null),
assert(closedShape != null),
assert(openShape != null),
assert(closedBuilder != null),
assert(openBuilder != null),
assert(tappable != null),
Expand Down Expand Up @@ -118,9 +121,9 @@ class OpenContainer extends StatefulWidget {

/// Shape of the container while it is closed.
///
/// When the container is opened it will transition from this shape to a
/// rectangular that fills the surrounding [Navigator]. When the container
/// is closed, it will transition back to this shape.
/// When the container is opened it will transition from this shape to
/// [openShape]. When the container is closed, it will transition back to this
/// shape.
///
/// Defaults to a [RoundedRectangleBorder] with a [Radius.circular] of 4.0.
///
Expand All @@ -129,6 +132,19 @@ class OpenContainer extends StatefulWidget {
/// * [Material.shape], which is used to implement this property.
final ShapeBorder closedShape;

/// Shape of the container while it is open.
///
/// When the container is opened it will transition from [closedShape] to
/// this shape. When the container is closed, it will transition from this
/// shape back to [closedShape].
///
/// Defaults to a rectangular.
///
/// See also:
///
/// * [Material.shape], which is used to implement this property.
final ShapeBorder openShape;

/// Called to obtain the child for the container in the closed state.
///
/// The [Widget] returned by this builder is faded out when the container
Expand Down Expand Up @@ -157,12 +173,27 @@ class OpenContainer extends StatefulWidget {
/// `action` callback that is provided to the [closedBuilder].
final bool tappable;

/// The time it will take to animate the container from its closed to its

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 provide open and closed durations?

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.

The underlying ModalRoute would have to be extended for this to support different durations.

/// open state and vice versa.
///
/// Defaults to 300ms.
final Duration transitionDuration;

@override
_OpenContainerState createState() => _OpenContainerState();
}

class _OpenContainerState extends State<OpenContainer> {
// Key used in [_OpenContainerRoute] to hide the widget returned by
// [OpenContainer.openBuilder] in the source route while the container is
// opening/open. A copy of that widget is included in the
// [_OpenContainerRoute] where it fades out. To avoid issues with double
// shadows and transparency, we hide it in the source route.
final GlobalKey<_HideableState> _hideableKey = GlobalKey<_HideableState>();

// Key used to steal the state of the widget returned by
// [OpenContainer.openBuilder] from the source route and attach it to the
// same widget included in the [_OpenContainerRoute] where it fades out.
final GlobalKey _closedBuilderKey = GlobalKey();

void openContainer() {
Expand All @@ -172,10 +203,12 @@ class _OpenContainerState extends State<OpenContainer> {
closedElevation: widget.closedElevation,
openElevation: widget.openElevation,
closedShape: widget.closedShape,
openBuilder: widget.openBuilder,
openShape: widget.openShape,
closedBuilder: widget.closedBuilder,
openBuilder: widget.openBuilder,
hideableKey: _hideableKey,
closedBuilderKey: _closedBuilderKey,
transitionDuration: widget.transitionDuration,
));
}

Expand Down Expand Up @@ -278,10 +311,12 @@ class _OpenContainerRoute extends ModalRoute<void> {
@required double closedElevation,
@required this.openElevation,
@required ShapeBorder closedShape,
@required this.openBuilder,
@required this.openShape,
@required this.closedBuilder,
@required this.openBuilder,
@required this.hideableKey,
@required this.closedBuilderKey,
@required this.transitionDuration,
}) : assert(closedColor != null),
Comment thread
goderbauer marked this conversation as resolved.
Resolved
assert(openColor != null),
assert(closedElevation != null),
Expand All @@ -297,7 +332,7 @@ class _OpenContainerRoute extends ModalRoute<void> {
),
_shapeTween = ShapeBorderTween(
begin: closedShape,
end: const RoundedRectangleBorder(),
end: openShape,
),
_colorTween = _FlippableTweenSequence<Color>(<TweenSequenceItem<Color>>[
Comment thread
goderbauer marked this conversation as resolved.
Resolved
TweenSequenceItem<Color>(
Expand All @@ -312,11 +347,19 @@ class _OpenContainerRoute extends ModalRoute<void> {

final Color openColor;
final double openElevation;
final ShapeBorder openShape;
final OpenContainerBuilder closedBuilder;
final OpenContainerBuilder openBuilder;

// See [_OpenContainerState._hideableKey].
final GlobalKey<_HideableState> hideableKey;
Comment thread
goderbauer marked this conversation as resolved.
Resolved

// See [_OpenContainerState._closedBuilderKey].
final GlobalKey closedBuilderKey;

@override
final Duration transitionDuration;

final Tween<double> _elevationTween;
final ShapeBorderTween _shapeTween;
final _FlippableTweenSequence<Color> _colorTween;
Expand Down Expand Up @@ -344,12 +387,17 @@ class _OpenContainerRoute extends ModalRoute<void> {
),
]);

// Key used for the widget returned by [OpenContainer.openBuilder] to keep
// its state when the shape of the widget tree is changed at the end of the
// animation to remove all the craft that was necessary to make the animation
// work.
final GlobalKey _openBuilderKey = GlobalKey();
Comment thread
goderbauer marked this conversation as resolved.
Resolved

// Defines the position of the container on screen.
// Defines the position of the (opening) [OpenContainer] within the bounds of
// the enclosing [Navigator].
final RectTween _insetsTween = RectTween(end: Rect.zero);

// Defines the size of the container on screen.
// Defines the size of the [OpenContainer].
final SizeTween _sizeTween = SizeTween();

AnimationStatus _lastAnimationStatus;
Expand Down Expand Up @@ -401,6 +449,9 @@ class _OpenContainerRoute extends ModalRoute<void> {
_sizeTween.end = navSize;

void takeMeasurementsInSourceRoute([Duration _]) {
if (!navigator.attached || hideableKey.currentContext == null) {
return;
}
final Rect srcRect = _getRect(hideableKey, navigator);
_sizeTween.begin = srcRect.size;
_insetsTween.begin = Rect.fromLTRB(
Expand All @@ -425,10 +476,13 @@ class _OpenContainerRoute extends ModalRoute<void> {
return render.size;
}

// Returns the bounds of the [RenderObject] identified by `key` in the
// coordinate system of `ancestor`.
Rect _getRect(GlobalKey key, RenderBox ancestor) {
Comment thread
goderbauer marked this conversation as resolved.
Resolved
assert(key.currentContext != null);
assert(ancestor != null && ancestor.hasSize);
final RenderBox render = key.currentContext.findRenderObject();
assert(render != null && render.hasSize);
assert(ancestor != null && ancestor.hasSize);
return MatrixUtils.transformRect(
render.getTransformTo(ancestor),
Offset.zero & render.size,
Expand Down Expand Up @@ -482,6 +536,7 @@ class _OpenContainerRoute extends ModalRoute<void> {
child: Material(
Comment thread
goderbauer marked this conversation as resolved.
Resolved
color: openColor,
elevation: openElevation,
shape: openShape,
child: Builder(
key: _openBuilderKey,
builder: (BuildContext context) {
Expand Down Expand Up @@ -595,9 +650,6 @@ class _OpenContainerRoute extends ModalRoute<void> {
);
}

@override
Duration get transitionDuration => const Duration(milliseconds: 300);

@override
bool get maintainState => true;

Expand Down
168 changes: 161 additions & 7 deletions packages/animations/test/open_container_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,158 @@ void main() {
));
expect(materialRectOpen, fullNavigator);
});

testWidgets('does not crash when disposed right after pop', (WidgetTester tester) async {
await tester.pumpWidget(Center(
child: SizedBox(
width: 300,
height: 400,
child: _boilerplate(
child: Center(
child: OpenContainer(
closedBuilder: (BuildContext context, VoidCallback action) {
return const Text('Closed');
},
openBuilder: (BuildContext context, VoidCallback action) {
return const Text('Open');
},
),
),
),
),
));
await tester.tap(find.text('Closed'));
await tester.pumpAndSettle();

final NavigatorState navigator = tester.state(find.byType(Navigator));
navigator.pop();

await tester.pumpWidget(const Placeholder());
expect(tester.takeException(), isNull);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
});

testWidgets('can specify a duration', (WidgetTester tester) async {
await tester.pumpWidget(Center(
child: SizedBox(
width: 300,
height: 400,
child: _boilerplate(
child: Center(
child: OpenContainer(
transitionDuration: const Duration(seconds: 2),
closedBuilder: (BuildContext context, VoidCallback action) {
return const Text('Closed');
},
openBuilder: (BuildContext context, VoidCallback action) {
return const Text('Open');
},
),
),
),
),
));

expect(find.text('Open'), findsNothing);
expect(find.text('Closed'), findsOneWidget);

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

// Jump to the end of the transition.
await tester.pump(const Duration(seconds: 2));
expect(find.text('Open'), findsOneWidget); // faded in
expect(find.text('Closed'), findsOneWidget); // faded out
await tester.pump(const Duration(milliseconds: 1));
expect(find.text('Open'), findsOneWidget);
expect(find.text('Closed'), findsNothing);

final NavigatorState navigator = tester.state(find.byType(Navigator));
navigator.pop();
await tester.pump();

// Jump to the end of the transition.
await tester.pump(const Duration(seconds: 2));
expect(find.text('Open'), findsOneWidget); // faded out
expect(find.text('Closed'), findsOneWidget); // faded in
await tester.pump(const Duration(milliseconds: 1));
expect(find.text('Open'), findsNothing);
expect(find.text('Closed'), findsOneWidget);
});

testWidgets('can specify an open shape', (WidgetTester tester) async {
await tester.pumpWidget(Center(
child: SizedBox(
width: 300,
height: 400,
child: _boilerplate(
child: Center(
child: OpenContainer(
closedShape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
openShape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40),
),
closedBuilder: (BuildContext context, VoidCallback action) {
return const Text('Closed');
},
openBuilder: (BuildContext context, VoidCallback action) {
return const Text('Open');
},
),
),
),
),
));

expect(find.text('Open'), findsNothing);
expect(find.text('Closed'), findsOneWidget);
final double closedRadius = _getRadius(tester.firstWidget(find.ancestor(
of: find.text('Closed'),
matching: find.byType(Material),
)));
expect(closedRadius, 10.0);

await tester.tap(find.text('Closed'));
await tester.pump();
expect(find.text('Open'), findsOneWidget);
expect(find.text('Closed'), findsOneWidget);
final double openingRadius = _getRadius(tester.firstWidget(find.ancestor(
of: find.text('Open'),
matching: find.byType(Material),
)));
expect(openingRadius, 10.0);

await tester.pump(const Duration(milliseconds: 150));
expect(find.text('Open'), findsOneWidget);
expect(find.text('Closed'), findsOneWidget);
final double halfwayRadius = _getRadius(tester.firstWidget(find.ancestor(
of: find.text('Open'),
matching: find.byType(Material),
)));
expect(halfwayRadius, greaterThan(10.0));
expect(halfwayRadius, lessThan(40.0));

await tester.pump(const Duration(milliseconds: 150));
expect(find.text('Open'), findsOneWidget);
expect(find.text('Closed'), findsOneWidget);
final double openRadius = _getRadius(tester.firstWidget(find.ancestor(
of: find.text('Open'),
matching: find.byType(Material),
)));
expect(openRadius, 40.0);

await tester.pump(const Duration(milliseconds: 1));
expect(find.text('Closed'), findsNothing);
expect(find.text('Open'), findsOneWidget);
final double finalRadius = _getRadius(tester.firstWidget(find.ancestor(
of: find.text('Open'),
matching: find.byType(Material),
)));
expect(finalRadius, 40.0);
});
}

void _expectMaterialPropertiesHaveAdvanced({
Expand Down Expand Up @@ -760,14 +912,16 @@ class _TrackedData {
final Material material;
final Rect rect;

double get radius {
final RoundedRectangleBorder shape = material.shape;
if (shape == null) {
return 0.0;
}
final BorderRadius radius = shape.borderRadius;
return radius.topRight.x;
double get radius => _getRadius(material);
}

double _getRadius(Material material) {
final RoundedRectangleBorder shape = material.shape;
if (shape == null) {
return 0.0;
}
final BorderRadius radius = shape.borderRadius;
return radius.topRight.x;
}

Widget _boilerplate({@required Widget child}) {
Expand Down