Add detection of drawer open and close in Scaffold widget as a callback method. (#67249)
This commit is contained in:
parent
a7f5fd5360
commit
dc432d78db
@ -1443,7 +1443,9 @@ class Scaffold extends StatefulWidget {
|
|||||||
this.floatingActionButtonAnimator,
|
this.floatingActionButtonAnimator,
|
||||||
this.persistentFooterButtons,
|
this.persistentFooterButtons,
|
||||||
this.drawer,
|
this.drawer,
|
||||||
|
this.onDrawerChanged,
|
||||||
this.endDrawer,
|
this.endDrawer,
|
||||||
|
this.onEndDrawerChanged,
|
||||||
this.bottomNavigationBar,
|
this.bottomNavigationBar,
|
||||||
this.bottomSheet,
|
this.bottomSheet,
|
||||||
this.backgroundColor,
|
this.backgroundColor,
|
||||||
@ -1607,6 +1609,9 @@ class Scaffold extends StatefulWidget {
|
|||||||
/// {@end-tool}
|
/// {@end-tool}
|
||||||
final Widget? drawer;
|
final Widget? drawer;
|
||||||
|
|
||||||
|
/// Optional callback that is called when the [Scaffold.drawer] is opened or closed.
|
||||||
|
final DrawerCallback? onDrawerChanged;
|
||||||
|
|
||||||
/// A panel displayed to the side of the [body], often hidden on mobile
|
/// A panel displayed to the side of the [body], often hidden on mobile
|
||||||
/// devices. Swipes in from right-to-left ([TextDirection.ltr]) or
|
/// devices. Swipes in from right-to-left ([TextDirection.ltr]) or
|
||||||
/// left-to-right ([TextDirection.rtl])
|
/// left-to-right ([TextDirection.rtl])
|
||||||
@ -1667,6 +1672,9 @@ class Scaffold extends StatefulWidget {
|
|||||||
/// {@end-tool}
|
/// {@end-tool}
|
||||||
final Widget? endDrawer;
|
final Widget? endDrawer;
|
||||||
|
|
||||||
|
/// Optional callback that is called when the [Scaffold.endDrawer] is opened or closed.
|
||||||
|
final DrawerCallback? onEndDrawerChanged;
|
||||||
|
|
||||||
/// The color to use for the scrim that obscures primary content while a drawer is open.
|
/// The color to use for the scrim that obscures primary content while a drawer is open.
|
||||||
///
|
///
|
||||||
/// By default, the color is [Colors.black54]
|
/// By default, the color is [Colors.black54]
|
||||||
@ -2091,12 +2099,14 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin {
|
|||||||
setState(() {
|
setState(() {
|
||||||
_drawerOpened = isOpened;
|
_drawerOpened = isOpened;
|
||||||
});
|
});
|
||||||
|
widget.onDrawerChanged?.call(isOpened);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _endDrawerOpenedCallback(bool isOpened) {
|
void _endDrawerOpenedCallback(bool isOpened) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_endDrawerOpened = isOpened;
|
_endDrawerOpened = isOpened;
|
||||||
});
|
});
|
||||||
|
widget.onEndDrawerChanged?.call(isOpened);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Opens the [Drawer] (if any).
|
/// Opens the [Drawer] (if any).
|
||||||
|
@ -11,6 +11,44 @@ import 'package:flutter_test/flutter_test.dart';
|
|||||||
import '../widgets/semantics_tester.dart';
|
import '../widgets/semantics_tester.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
testWidgets('Scaffold drawer callback test', (WidgetTester tester) async {
|
||||||
|
bool isDrawerOpen = false;
|
||||||
|
bool isEndDrawerOpen = false;
|
||||||
|
|
||||||
|
await tester.pumpWidget(MaterialApp(
|
||||||
|
home: Scaffold(
|
||||||
|
drawer: Container(
|
||||||
|
color: Colors.blue,
|
||||||
|
),
|
||||||
|
onDrawerChanged: (bool isOpen) {
|
||||||
|
isDrawerOpen = isOpen;
|
||||||
|
},
|
||||||
|
endDrawer: Container(
|
||||||
|
color: Colors.green,
|
||||||
|
),
|
||||||
|
onEndDrawerChanged: (bool isOpen) {
|
||||||
|
isEndDrawerOpen = isOpen;
|
||||||
|
},
|
||||||
|
body: Container()),
|
||||||
|
));
|
||||||
|
|
||||||
|
final ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
|
||||||
|
|
||||||
|
scaffoldState.openDrawer();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(true, isDrawerOpen);
|
||||||
|
scaffoldState.openEndDrawer();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(false, isDrawerOpen);
|
||||||
|
|
||||||
|
scaffoldState.openEndDrawer();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(true, isEndDrawerOpen);
|
||||||
|
scaffoldState.openDrawer();
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(false, isEndDrawerOpen);
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('Scaffold control test', (WidgetTester tester) async {
|
testWidgets('Scaffold control test', (WidgetTester tester) async {
|
||||||
final Key bodyKey = UniqueKey();
|
final Key bodyKey = UniqueKey();
|
||||||
Widget boilerplate(Widget child) {
|
Widget boilerplate(Widget child) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user