Fix memory leaks in MaterialBanner (#146963)

This commit is contained in:
Valentin Vignal 2024-04-18 22:21:18 +08:00 committed by GitHub
parent c83d650de4
commit fb110b98da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 8 deletions

View File

@ -267,11 +267,15 @@ class MaterialBanner extends StatefulWidget {
class _MaterialBannerState extends State<MaterialBanner> {
bool _wasVisible = false;
CurvedAnimation? _heightAnimation;
CurvedAnimation? _slideOutCurvedAnimation;
@override
void initState() {
super.initState();
widget.animation?.addStatusListener(_onAnimationStatusChanged);
_setCurvedAnimations();
}
@override
@ -280,12 +284,31 @@ class _MaterialBannerState extends State<MaterialBanner> {
if (widget.animation != oldWidget.animation) {
oldWidget.animation?.removeStatusListener(_onAnimationStatusChanged);
widget.animation?.addStatusListener(_onAnimationStatusChanged);
_setCurvedAnimations();
}
}
void _setCurvedAnimations() {
_heightAnimation?.dispose();
_slideOutCurvedAnimation?.dispose();
if (widget.animation != null) {
_heightAnimation = CurvedAnimation(parent: widget.animation!, curve: _materialBannerHeightCurve);
_slideOutCurvedAnimation = CurvedAnimation(
parent: widget.animation!,
curve: const Threshold(0.0),
);
} else {
_heightAnimation = null;
_slideOutCurvedAnimation = null;
}
}
@override
void dispose() {
widget.animation?.removeStatusListener(_onAnimationStatusChanged);
_heightAnimation?.dispose();
_slideOutCurvedAnimation?.dispose();
super.dispose();
}
@ -408,14 +431,10 @@ class _MaterialBannerState extends State<MaterialBanner> {
child: materialBanner,
);
final CurvedAnimation heightAnimation = CurvedAnimation(parent: widget.animation!, curve: _materialBannerHeightCurve);
final Animation<Offset> slideOutAnimation = Tween<Offset>(
begin: const Offset(0.0, -1.0),
end: Offset.zero,
).animate(CurvedAnimation(
parent: widget.animation!,
curve: const Threshold(0.0),
));
).animate(_slideOutCurvedAnimation!);
materialBanner = Semantics(
container: true,
@ -436,11 +455,11 @@ class _MaterialBannerState extends State<MaterialBanner> {
materialBannerTransition = materialBanner;
} else {
materialBannerTransition = AnimatedBuilder(
animation: heightAnimation,
animation: _heightAnimation!,
builder: (BuildContext context, Widget? child) {
return Align(
alignment: AlignmentDirectional.bottomStart,
heightFactor: heightAnimation.value,
heightFactor: _heightAnimation!.value,
child: child,
);
},

View File

@ -5,6 +5,7 @@
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';
void main() {
test('MaterialBannerThemeData copyWith, ==, hashCode basics', () {
@ -324,7 +325,10 @@ void main() {
expect(find.byType(Divider), findsNothing);
});
testWidgets('MaterialBanner widget properties take priority over theme when presented by ScaffoldMessenger', (WidgetTester tester) async {
testWidgets('MaterialBanner widget properties take priority over theme when presented by ScaffoldMessenger',
// 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 {
const Color backgroundColor = Colors.purple;
const double elevation = 6.0;
const TextStyle textStyle = TextStyle(color: Colors.green);