Skip to content

Commit

Permalink
fixes DialogRoute memory leak (#147816)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimilkalathiya authored May 16, 2024
1 parent 82fb5db commit 0d22d91
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
35 changes: 28 additions & 7 deletions packages/flutter/lib/src/material/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1308,13 +1308,7 @@ class SimpleDialog extends StatelessWidget {
}

Widget _buildMaterialDialogTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
return FadeTransition(
opacity: CurvedAnimation(
parent: animation,
curve: Curves.easeOut,
),
child: child,
);
return child;
}

/// Displays a Material dialog above the current contents of the app, with
Expand Down Expand Up @@ -1589,6 +1583,33 @@ class DialogRoute<T> extends RawDialogRoute<T> {
transitionDuration: const Duration(milliseconds: 150),
transitionBuilder: _buildMaterialDialogTransitions,
);

CurvedAnimation? _curvedAnimation;

void _setAnimation(Animation<double> animation) {
if (_curvedAnimation?.parent != animation) {
_curvedAnimation?.dispose();
_curvedAnimation = CurvedAnimation(
parent: animation,
curve: Curves.easeOut,
);
}
}

@override
Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
_setAnimation(animation);
return FadeTransition(
opacity: _curvedAnimation!,
child: super.buildTransitions(context, animation, secondaryAnimation, child),
);
}

@override
void dispose() {
_curvedAnimation?.dispose();
super.dispose();
}
}

double _scalePadding(double textScaleFactor) {
Expand Down
6 changes: 5 additions & 1 deletion packages/flutter/test/cupertino/dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

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

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

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

0 comments on commit 0d22d91

Please sign in to comment.