Skip to content

Commit 0d22d91

Browse files
fixes DialogRoute memory leak (#147816)
1 parent 82fb5db commit 0d22d91

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

packages/flutter/lib/src/material/dialog.dart

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,13 +1308,7 @@ class SimpleDialog extends StatelessWidget {
13081308
}
13091309

13101310
Widget _buildMaterialDialogTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
1311-
return FadeTransition(
1312-
opacity: CurvedAnimation(
1313-
parent: animation,
1314-
curve: Curves.easeOut,
1315-
),
1316-
child: child,
1317-
);
1311+
return child;
13181312
}
13191313

13201314
/// Displays a Material dialog above the current contents of the app, with
@@ -1589,6 +1583,33 @@ class DialogRoute<T> extends RawDialogRoute<T> {
15891583
transitionDuration: const Duration(milliseconds: 150),
15901584
transitionBuilder: _buildMaterialDialogTransitions,
15911585
);
1586+
1587+
CurvedAnimation? _curvedAnimation;
1588+
1589+
void _setAnimation(Animation<double> animation) {
1590+
if (_curvedAnimation?.parent != animation) {
1591+
_curvedAnimation?.dispose();
1592+
_curvedAnimation = CurvedAnimation(
1593+
parent: animation,
1594+
curve: Curves.easeOut,
1595+
);
1596+
}
1597+
}
1598+
1599+
@override
1600+
Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
1601+
_setAnimation(animation);
1602+
return FadeTransition(
1603+
opacity: _curvedAnimation!,
1604+
child: super.buildTransitions(context, animation, secondaryAnimation, child),
1605+
);
1606+
}
1607+
1608+
@override
1609+
void dispose() {
1610+
_curvedAnimation?.dispose();
1611+
super.dispose();
1612+
}
15921613
}
15931614

15941615
double _scalePadding(double textScaleFactor) {

packages/flutter/test/cupertino/dialog_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:flutter/foundation.dart';
1515
import 'package:flutter/material.dart';
1616
import 'package:flutter/rendering.dart';
1717
import 'package:flutter_test/flutter_test.dart';
18+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1819

1920
import '../widgets/semantics_tester.dart';
2021

@@ -355,7 +356,10 @@ void main() {
355356
expect(tester.getSize(find.widgetWithText(CupertinoDialogAction, 'OK')), equals(const Size(310.0, 98.0)));
356357
});
357358

358-
testWidgets('Dialog respects small constraints.', (WidgetTester tester) async {
359+
testWidgets('Dialog respects small constraints.',
360+
// TODO(polina-c): remove when fixed https://github.com/flutter/flutter/issues/145600 [leak-tracking-opt-in]
361+
experimentalLeakTesting: LeakTesting.settings.withTracked(classes: const <String>['CurvedAnimation']),
362+
(WidgetTester tester) async {
359363
final ScrollController scrollController = ScrollController();
360364
addTearDown(scrollController.dispose);
361365
await tester.pumpWidget(

0 commit comments

Comments
 (0)