Unverified Commit 14bc5e8c authored by Pedro Massango's avatar Pedro Massango Committed by GitHub

Add closeDrawer and closeEndDrawer in ScaffoldState (#96960)

parent da7d7777
......@@ -1590,7 +1590,8 @@ class Scaffold extends StatefulWidget {
///
/// To open the drawer, use the [ScaffoldState.openDrawer] function.
///
/// To close the drawer, use [Navigator.pop].
/// To close the drawer, use either [ScaffoldState.closeDrawer] or
/// [Navigator.pop].
///
/// {@tool dartpad}
/// To disable the drawer edge swipe, set the
......@@ -1613,7 +1614,8 @@ class Scaffold extends StatefulWidget {
///
/// To open the drawer, use the [ScaffoldState.openEndDrawer] function.
///
/// To close the drawer, use [Navigator.pop].
/// To close the drawer, use either [ScaffoldState.closeEndDrawer] or
/// [Navigator.pop].
///
/// {@tool dartpad}
/// To disable the drawer edge swipe, set the
......@@ -1986,7 +1988,8 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, Resto
/// appropriate [IconButton], and handles the edge-swipe gesture, to show the
/// drawer.
///
/// To close the drawer once it is open, use [Navigator.pop].
/// To close the drawer, use either [ScaffoldState.closeEndDrawer] or
/// [Navigator.pop].
///
/// See [Scaffold.of] for information about how to obtain the [ScaffoldState].
void openDrawer() {
......@@ -2004,7 +2007,8 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, Resto
/// appropriate [IconButton], and handles the edge-swipe gesture, to show the
/// drawer.
///
/// To close the end side drawer once it is open, use [Navigator.pop].
/// To close the drawer, use either [ScaffoldState.closeEndDrawer] or
/// [Navigator.pop].
///
/// See [Scaffold.of] for information about how to obtain the [ScaffoldState].
void openEndDrawer() {
......@@ -2305,6 +2309,24 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, Resto
}
}
/// Closes [Scaffold.drawer] if it is currently opened.
///
/// See [Scaffold.of] for information about how to obtain the [ScaffoldState].
void closeDrawer() {
if (hasDrawer && isDrawerOpen) {
_drawerKey.currentState!.close();
}
}
/// Closes [Scaffold.endDrawer] if it is currently opened.
///
/// See [Scaffold.of] for information about how to obtain the [ScaffoldState].
void closeEndDrawer() {
if (hasEndDrawer && isEndDrawerOpen) {
_endDrawerKey.currentState!.close();
}
}
void _updatePersistentBottomSheet() {
_currentBottomSheetKey.currentState!.setState(() {});
}
......
......@@ -379,4 +379,70 @@ void main() {
expect(find.text('drawer'), findsNothing);
expect(find.text('endDrawer'), findsOneWidget);
});
testWidgets('ScaffoldState close drawer', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
key: scaffoldKey,
drawer: const Text('Drawer'),
body: Container(),
),
),
);
expect(find.text('Drawer'), findsNothing);
scaffoldKey.currentState!.openDrawer();
await tester.pumpAndSettle();
expect(find.text('Drawer'), findsOneWidget);
scaffoldKey.currentState!.closeDrawer();
await tester.pumpAndSettle();
expect(find.text('Drawer'), findsNothing);
});
testWidgets('ScaffoldState close drawer do not crash if drawer is already closed', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
key: scaffoldKey,
drawer: const Text('Drawer'),
body: Container(),
),
),
);
expect(find.text('Drawer'), findsNothing);
scaffoldKey.currentState!.closeDrawer();
await tester.pumpAndSettle();
expect(find.text('Drawer'), findsNothing);
});
testWidgets('ScaffoldState close end drawer', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
key: scaffoldKey,
endDrawer: const Text('endDrawer'),
body: Container(),
),
),
);
expect(find.text('endDrawer'), findsNothing);
scaffoldKey.currentState!.openEndDrawer();
await tester.pumpAndSettle();
expect(find.text('endDrawer'), findsOneWidget);
scaffoldKey.currentState!.closeEndDrawer();
await tester.pumpAndSettle();
expect(find.text('endDrawer'), findsNothing);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment