Skip to content

Commit ca3f0a8

Browse files
authored
[animations] added onClosed (#122)
1 parent 8909df9 commit ca3f0a8

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

packages/animations/lib/src/open_container.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class OpenContainer extends StatefulWidget {
6666
borderRadius: BorderRadius.all(Radius.circular(4.0)),
6767
),
6868
this.openShape = const RoundedRectangleBorder(),
69+
this.onClosed,
6970
@required this.closedBuilder,
7071
@required this.openBuilder,
7172
this.tappable = true,
@@ -163,6 +164,9 @@ class OpenContainer extends StatefulWidget {
163164
/// * [Material.shape], which is used to implement this property.
164165
final ShapeBorder openShape;
165166

167+
/// Called when the container was popped and has returned to the closed state.
168+
final VoidCallback onClosed;
169+
166170
/// Called to obtain the child for the container in the closed state.
167171
///
168172
/// The [Widget] returned by this builder is faded out when the container
@@ -220,8 +224,8 @@ class _OpenContainerState extends State<OpenContainer> {
220224
// same widget included in the [_OpenContainerRoute] where it fades out.
221225
final GlobalKey _closedBuilderKey = GlobalKey();
222226

223-
void openContainer() {
224-
Navigator.of(context).push(_OpenContainerRoute(
227+
Future<void> openContainer<T>() async {
228+
await Navigator.of(context).push(_OpenContainerRoute(
225229
closedColor: widget.closedColor,
226230
openColor: widget.openColor,
227231
closedElevation: widget.closedElevation,
@@ -235,6 +239,9 @@ class _OpenContainerState extends State<OpenContainer> {
235239
transitionDuration: widget.transitionDuration,
236240
transitionType: widget.transitionType,
237241
));
242+
if (widget.onClosed != null) {
243+
widget.onClosed();
244+
}
238245
}
239246

240247
@override

packages/animations/test/open_container_test.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,48 @@ void main() {
14481448
expect(find.text('Closed 2'), findsNothing);
14491449
expect(find.text('Open 2'), findsOneWidget);
14501450
});
1451+
testWidgets('onClosed callback is called when container has closed',
1452+
(WidgetTester tester) async {
1453+
bool hasClosed = false;
1454+
final Widget openContainer = OpenContainer(
1455+
onClosed: () {
1456+
hasClosed = true;
1457+
},
1458+
closedBuilder: (BuildContext context, VoidCallback action) {
1459+
return GestureDetector(
1460+
onTap: action,
1461+
child: const Text('Closed'),
1462+
);
1463+
},
1464+
openBuilder: (BuildContext context, VoidCallback action) {
1465+
return GestureDetector(
1466+
onTap: action,
1467+
child: const Text('Open'),
1468+
);
1469+
},
1470+
);
1471+
1472+
await tester.pumpWidget(
1473+
_boilerplate(child: openContainer),
1474+
);
1475+
1476+
expect(find.text('Open'), findsNothing);
1477+
expect(find.text('Closed'), findsOneWidget);
1478+
expect(hasClosed, isFalse);
1479+
1480+
await tester.tap(find.text('Closed'));
1481+
await tester.pumpAndSettle();
1482+
1483+
expect(find.text('Open'), findsOneWidget);
1484+
expect(find.text('Closed'), findsNothing);
1485+
1486+
await tester.tap(find.text('Open'));
1487+
await tester.pumpAndSettle();
1488+
1489+
expect(find.text('Open'), findsNothing);
1490+
expect(find.text('Closed'), findsOneWidget);
1491+
expect(hasClosed, isTrue);
1492+
});
14511493
}
14521494

14531495
Color _getScrimColor(WidgetTester tester) {

0 commit comments

Comments
 (0)