Skip to content

Commit de1f758

Browse files
authored
[material_ui] Fix LocalHistoryEntry leak when double tapping Drawer scrim (flutter#12552)
This PR improves the DrawerControllerState lifecycle by making sure the LocalHistoryEntry is properly removed and cleared. Fixes flutter/flutter#133740 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent f1cd894 commit de1f758

3 files changed

Lines changed: 160 additions & 4 deletions

File tree

‎packages/material_ui/lib/src/drawer.dart‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,13 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
513513
..addStatusListener(_animationStatusChanged);
514514
}
515515

516+
bool _disposed = false;
517+
516518
@protected
517519
@override
518520
void dispose() {
519-
_historyEntry?.remove();
521+
_disposed = true;
522+
_removeHistoryEntry();
520523
_controller.dispose();
521524
_focusScopeNode.dispose();
522525
super.dispose();
@@ -558,22 +561,31 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
558561
}
559562
}
560563

564+
void _removeHistoryEntry() {
565+
_historyEntry?.remove();
566+
_historyEntry = null;
567+
}
568+
561569
void _animationStatusChanged(AnimationStatus status) {
562570
switch (status) {
563571
case AnimationStatus.forward:
564572
_ensureHistoryEntry();
565573
case AnimationStatus.reverse:
566-
_historyEntry?.remove();
567-
_historyEntry = null;
568574
case AnimationStatus.dismissed:
575+
_removeHistoryEntry();
569576
case AnimationStatus.completed:
570577
break;
571578
}
572579
}
573580

574581
void _handleHistoryEntryRemoved() {
575582
_historyEntry = null;
576-
close();
583+
if (_disposed || !mounted) {
584+
return;
585+
}
586+
if (!_controller.isDismissed && _controller.status != AnimationStatus.reverse) {
587+
close();
588+
}
577589
}
578590

579591
late AnimationController _controller;
@@ -632,6 +644,13 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
632644
final double visualVelocity = xVelocity / _width * _directionFactor;
633645

634646
_controller.fling(velocity: visualVelocity);
647+
if (visualVelocity < 0.0) {
648+
// We explicitly remove the history entry here because the drawer may already be
649+
// animating closed (status == AnimationStatus.reverse) when the drag begins.
650+
// In that case, flinging it closed again does not change the animation status,
651+
// so the status listener will not fire.
652+
_removeHistoryEntry();
653+
}
635654
widget.drawerCallback?.call(visualVelocity > 0.0);
636655
} else if (_controller.value < 0.5) {
637656
close();
@@ -651,6 +670,8 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
651670
/// Starts an animation to close the drawer.
652671
void close() {
653672
_controller.fling(velocity: -1.0);
673+
// Explicitly removed here for the same reason as in _settle.
674+
_removeHistoryEntry();
654675
widget.drawerCallback?.call(false);
655676
}
656677

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes LocalHistoryEntry leak when double tapping Drawer scrim
3+
version: patch

‎packages/material_ui/test/drawer_test.dart‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:async';
6+
57
import 'package:flutter/foundation.dart';
68
import 'package:flutter/gestures.dart';
79
import 'package:flutter/rendering.dart';
@@ -1513,4 +1515,134 @@ void main() {
15131515
expect(tester.getSize(find.byType(Drawer)), Size.zero);
15141516
expect(tester.getSize(find.byType(DrawerHeader)), Size.zero);
15151517
});
1518+
1519+
testWidgets('Drawer double tap to close does not leak LocalHistoryEntry', (
1520+
WidgetTester tester,
1521+
) async {
1522+
await tester.pumpWidget(
1523+
MaterialApp(
1524+
theme: ThemeData(platform: TargetPlatform.iOS),
1525+
home: const Scaffold(body: Center(child: Text('Home'))),
1526+
),
1527+
);
1528+
1529+
unawaited(
1530+
tester
1531+
.state<NavigatorState>(find.byType(Navigator))
1532+
.push(
1533+
MaterialPageRoute<void>(
1534+
builder: (BuildContext context) {
1535+
return Scaffold(
1536+
appBar: AppBar(title: const Text('Second')),
1537+
endDrawer: const Drawer(child: Text('Drawer Item')),
1538+
body: const Center(child: Text('Second Body')),
1539+
);
1540+
},
1541+
),
1542+
),
1543+
);
1544+
await tester.pumpAndSettle();
1545+
1546+
// Open the end drawer.
1547+
Scaffold.of(tester.element(find.text('Second Body'))).openEndDrawer();
1548+
await tester.pumpAndSettle();
1549+
1550+
// Rapid double tap on the scrim.
1551+
// Use manual pointer events to simulate tapping the scrim twice rapidly.
1552+
final TestGesture gesture1 = await tester.startGesture(const Offset(10.0, 200.0));
1553+
await gesture1.up();
1554+
1555+
// Pump once to start the closing animation ticker.
1556+
await tester.pump();
1557+
// Advance time by 150ms so the drawer is more than half closed.
1558+
await tester.pump(const Duration(milliseconds: 150));
1559+
1560+
final TestGesture gesture2 = await tester.startGesture(const Offset(10.0, 200.0));
1561+
await gesture2.up();
1562+
1563+
await tester.pumpAndSettle();
1564+
1565+
final BuildContext routeContext = tester.element(find.text('Second Body'));
1566+
final ModalRoute<dynamic> route = ModalRoute.of(routeContext)!;
1567+
1568+
// The drawer should be closed.
1569+
expect(find.text('Drawer Item'), findsNothing);
1570+
1571+
// The history entry should have been removed, meaning it won't handle pop internally.
1572+
expect(route.willHandlePopInternally, false);
1573+
1574+
// Attempt swipe to go back.
1575+
await tester.dragFrom(const Offset(5.0, 200.0), const Offset(500.0, 0.0));
1576+
await tester.pump();
1577+
await tester.pumpAndSettle();
1578+
1579+
// Verify it went back.
1580+
expect(find.text('Home'), findsOneWidget);
1581+
expect(find.text('Second Body'), findsNothing);
1582+
});
1583+
1584+
testWidgets('Drawer drag while closing does not leak LocalHistoryEntry', (
1585+
WidgetTester tester,
1586+
) async {
1587+
await tester.pumpWidget(
1588+
MaterialApp(
1589+
theme: ThemeData(platform: TargetPlatform.iOS),
1590+
home: const Scaffold(body: Center(child: Text('Home'))),
1591+
),
1592+
);
1593+
1594+
unawaited(
1595+
tester
1596+
.state<NavigatorState>(find.byType(Navigator))
1597+
.push(
1598+
MaterialPageRoute<void>(
1599+
builder: (BuildContext context) {
1600+
return Scaffold(
1601+
appBar: AppBar(title: const Text('Second')),
1602+
drawer: const Drawer(child: Text('Drawer Item')),
1603+
body: const Center(child: Text('Second Body')),
1604+
);
1605+
},
1606+
),
1607+
),
1608+
);
1609+
await tester.pumpAndSettle();
1610+
1611+
// Open the drawer.
1612+
Scaffold.of(tester.element(find.text('Second Body'))).openDrawer();
1613+
await tester.pumpAndSettle();
1614+
1615+
final BuildContext routeContext = tester.element(find.text('Second Body'));
1616+
final ModalRoute<dynamic> route = ModalRoute.of(routeContext)!;
1617+
expect(route.willHandlePopInternally, true);
1618+
1619+
// Start closing the drawer programmatically to begin the closing animation.
1620+
Scaffold.of(tester.element(find.text('Second Body'))).closeDrawer();
1621+
1622+
// Pump once to start the closing animation ticker.
1623+
await tester.pump();
1624+
// Advance time so the drawer is in the middle of closing.
1625+
await tester.pump(const Duration(milliseconds: 100));
1626+
1627+
// While it is closing (status == AnimationStatus.reverse), grab the drawer.
1628+
// This triggers _handleDragDown, which adds the history entry back.
1629+
final TestGesture gesture = await tester.startGesture(const Offset(50.0, 200.0));
1630+
await tester.pump();
1631+
1632+
// The history entry should have been re-added.
1633+
expect(route.willHandlePopInternally, true);
1634+
1635+
// Drag it closed again by dragging left and releasing.
1636+
await gesture.moveBy(const Offset(-20.0, 0.0));
1637+
await tester.pump();
1638+
await gesture.up();
1639+
1640+
await tester.pump();
1641+
1642+
expect(route.willHandlePopInternally, false);
1643+
1644+
await tester.pumpAndSettle();
1645+
1646+
expect(find.text('Drawer Item'), findsNothing);
1647+
});
15161648
}

0 commit comments

Comments
 (0)