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
keep state of closed widget
  • Loading branch information
goderbauer committed Aug 31, 2019
commit 7c4d0520e84c52f5fb328eebad0e0149669ae7f7
7 changes: 7 additions & 0 deletions packages/animations/lib/src/open_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class OpenContainer extends StatefulWidget {

class _OpenContainerState extends State<OpenContainer> {
final GlobalKey<_HideableState> _hidableKey = GlobalKey<_HideableState>();
final GlobalKey _closedBuilderKey = GlobalKey();

void openContainer() {
Navigator.of(context).push(_OpenContainerRoute(
Expand All @@ -171,6 +172,7 @@ class _OpenContainerState extends State<OpenContainer> {
openBuilder: widget.openBuilder,
closedBuilder: widget.closedBuilder,
hideableKey: _hidableKey,

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.

hidable => hideable

Here and elsewhere

closedBuilderKey: _closedBuilderKey,
));
}

Expand All @@ -186,6 +188,7 @@ class _OpenContainerState extends State<OpenContainer> {
elevation: widget.closedElevation,
shape: widget.closedShape,
child: Builder(
key: _closedBuilderKey,
builder: (BuildContext context) {
return widget.closedBuilder(context, openContainer);
},
Expand Down Expand Up @@ -238,6 +241,7 @@ class _OpenContainerRoute extends ModalRoute<void> {
@required this.openBuilder,
@required this.closedBuilder,
@required this.hideableKey,
@required this.closedBuilderKey,
}) : assert(closedColor != null),
Comment thread
goderbauer marked this conversation as resolved.
Resolved
assert(openColor != null),
assert(closedElevation != null),
Expand All @@ -246,6 +250,7 @@ class _OpenContainerRoute extends ModalRoute<void> {
assert(openBuilder != null),
assert(closedBuilder != null),
assert(hideableKey != null),
assert(closedBuilderKey != null),
_elevationTween = Tween<double>(
begin: closedElevation,
end: openElevation,
Expand All @@ -270,6 +275,7 @@ class _OpenContainerRoute extends ModalRoute<void> {
final OpenContainerChildBuilder closedBuilder;
final OpenContainerChildBuilder openBuilder;
final GlobalKey<_HideableState> hideableKey;
Comment thread
goderbauer marked this conversation as resolved.
Resolved
final GlobalKey closedBuilderKey;

final Tween<double> _elevationTween;
final ShapeBorderTween _shapeTween;
Expand Down Expand Up @@ -507,6 +513,7 @@ class _OpenContainerRoute extends ModalRoute<void> {
: Opacity(
opacity: closedOpacityTween.evaluate(animation),
child: Builder(
key: closedBuilderKey,
builder: (BuildContext context) {
// Use dummy "open container" callback
// since we are in the process of opening.
Expand Down
48 changes: 48 additions & 0 deletions packages/animations/test/open_container_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,54 @@ void main() {
expect(stateClosing, same(stateOpen));
});

testWidgets('closed widget keeps state', (WidgetTester tester) async {
VoidCallback open;
await tester.pumpWidget(_boilerplate(
child: Center(
child: OpenContainer(
closedBuilder: (BuildContext context, VoidCallback action) {
open = action;
return Switch(
value: true,
onChanged: (bool v) {},
);
},
openBuilder: (BuildContext context, VoidCallback action) {
return const Text('Open');
},
),
),
));

final State stateClosed = tester.state(find.byType(Switch));
expect(stateClosed, isNotNull);

open();
await tester.pump(Duration(milliseconds: 200));
expect(find.text('Open'), findsOneWidget);

final State stateOpening = tester.state(find.byType(Switch));
expect(stateOpening, same(stateClosed));

await tester.pumpAndSettle();
expect(find.byType(Switch), findsNothing);
expect(find.text('Open'), findsOneWidget);
final State stateOpen = tester.state(find.byType(Switch, skipOffstage: false,));
expect(stateOpen, same(stateOpening));

final NavigatorState navigator = tester.state(find.byType(Navigator));
navigator.pop();
await tester.pump(Duration(milliseconds: 200));
expect(find.text('Open'), findsOneWidget);
final State stateClosing = tester.state(find.byType(Switch));
expect(stateClosing, same(stateOpen));

await tester.pumpAndSettle();
expect(find.text('Open'), findsNothing);
final State stateClosedAgain = tester.state(find.byType(Switch));
expect(stateClosedAgain, same(stateClosing));
});

testWidgets('closes to the right location when src position has changed',
(WidgetTester tester) async {
final Widget openContainer = OpenContainer(
Expand Down