Skip to content

Commit

Permalink
Fix Scaffold setState during locked framework due to open drawer …
Browse files Browse the repository at this point in the history
…(#107173)
  • Loading branch information
markusaksli-nc authored Jul 18, 2022
1 parent 2f0a252 commit da3b2a2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/flutter/lib/src/material/scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,7 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, Resto
bool get isEndDrawerOpen => _endDrawerOpened.value;

void _drawerOpenedCallback(bool isOpened) {
if (_drawerOpened.value != isOpened) {
if (_drawerOpened.value != isOpened && _drawerKey.currentState != null) {
setState(() {
_drawerOpened.value = isOpened;
});
Expand All @@ -2038,7 +2038,7 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, Resto
}

void _endDrawerOpenedCallback(bool isOpened) {
if (_endDrawerOpened.value != isOpened) {
if (_endDrawerOpened.value != isOpened && _endDrawerKey.currentState != null) {
setState(() {
_endDrawerOpened.value = isOpened;
});
Expand Down
81 changes: 81 additions & 0 deletions packages/flutter/test/material/drawer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,87 @@ void main() {
expect(find.text('Drawer'), findsNothing);
});

testWidgets('Disposing drawer does not crash if drawer is open and framework is locked', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/34978
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
tester.binding.window.physicalSizeTestValue = const Size(1800.0, 2400.0);

await tester.pumpWidget(
MaterialApp(
home: OrientationBuilder(
builder: (BuildContext context, Orientation orientation) {
switch (orientation) {
case Orientation.portrait:
return Scaffold(
drawer: const Text('drawer'),
body: Container(),
);
case Orientation.landscape:
return Scaffold(
appBar: AppBar(),
body: Container(),
);
}
},
),
),
);

expect(find.text('drawer'), findsNothing);

// Using a global key is a workaround for this issue.
final ScaffoldState portraitScaffoldState = tester.firstState(find.byType(Scaffold));
portraitScaffoldState.openDrawer();
await tester.pumpAndSettle();
expect(find.text('drawer'), findsOneWidget);

// Change the orientation and cause the drawer controller to be disposed
// while the framework is locked.
tester.binding.window.physicalSizeTestValue = const Size(2400.0, 1800.0);
await tester.pumpAndSettle();
expect(find.byType(BackButton), findsNothing);
});

testWidgets('Disposing endDrawer does not crash if endDrawer is open and framework is locked', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/34978
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
tester.binding.window.physicalSizeTestValue = const Size(1800.0, 2400.0);

await tester.pumpWidget(
MaterialApp(
home: OrientationBuilder(
builder: (BuildContext context, Orientation orientation) {
switch (orientation) {
case Orientation.portrait:
return Scaffold(
endDrawer: const Text('endDrawer'),
body: Container(),
);
case Orientation.landscape:
return Scaffold(
appBar: AppBar(),
body: Container(),
);
}
},
),
),
);

expect(find.text('endDrawer'), findsNothing);

// Using a global key is a workaround for this issue.
final ScaffoldState portraitScaffoldState = tester.firstState(find.byType(Scaffold));
portraitScaffoldState.openEndDrawer();
await tester.pumpAndSettle();
expect(find.text('endDrawer'), findsOneWidget);

// Change the orientation and cause the drawer controller to be disposed
// while the framework is locked.
tester.binding.window.physicalSizeTestValue = const Size(2400.0, 1800.0);
await tester.pumpAndSettle();
expect(find.byType(BackButton), findsNothing);
});

testWidgets('ScaffoldState close end drawer', (WidgetTester tester) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
Expand Down

0 comments on commit da3b2a2

Please sign in to comment.