mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Fix memory leaks in MaterialBanner
(#146963)
This commit is contained in:
parent
c83d650de4
commit
fb110b98da
@ -267,11 +267,15 @@ class MaterialBanner extends StatefulWidget {
|
|||||||
|
|
||||||
class _MaterialBannerState extends State<MaterialBanner> {
|
class _MaterialBannerState extends State<MaterialBanner> {
|
||||||
bool _wasVisible = false;
|
bool _wasVisible = false;
|
||||||
|
CurvedAnimation? _heightAnimation;
|
||||||
|
CurvedAnimation? _slideOutCurvedAnimation;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
widget.animation?.addStatusListener(_onAnimationStatusChanged);
|
widget.animation?.addStatusListener(_onAnimationStatusChanged);
|
||||||
|
_setCurvedAnimations();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -280,12 +284,31 @@ class _MaterialBannerState extends State<MaterialBanner> {
|
|||||||
if (widget.animation != oldWidget.animation) {
|
if (widget.animation != oldWidget.animation) {
|
||||||
oldWidget.animation?.removeStatusListener(_onAnimationStatusChanged);
|
oldWidget.animation?.removeStatusListener(_onAnimationStatusChanged);
|
||||||
widget.animation?.addStatusListener(_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
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
widget.animation?.removeStatusListener(_onAnimationStatusChanged);
|
widget.animation?.removeStatusListener(_onAnimationStatusChanged);
|
||||||
|
_heightAnimation?.dispose();
|
||||||
|
_slideOutCurvedAnimation?.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -408,14 +431,10 @@ class _MaterialBannerState extends State<MaterialBanner> {
|
|||||||
child: materialBanner,
|
child: materialBanner,
|
||||||
);
|
);
|
||||||
|
|
||||||
final CurvedAnimation heightAnimation = CurvedAnimation(parent: widget.animation!, curve: _materialBannerHeightCurve);
|
|
||||||
final Animation<Offset> slideOutAnimation = Tween<Offset>(
|
final Animation<Offset> slideOutAnimation = Tween<Offset>(
|
||||||
begin: const Offset(0.0, -1.0),
|
begin: const Offset(0.0, -1.0),
|
||||||
end: Offset.zero,
|
end: Offset.zero,
|
||||||
).animate(CurvedAnimation(
|
).animate(_slideOutCurvedAnimation!);
|
||||||
parent: widget.animation!,
|
|
||||||
curve: const Threshold(0.0),
|
|
||||||
));
|
|
||||||
|
|
||||||
materialBanner = Semantics(
|
materialBanner = Semantics(
|
||||||
container: true,
|
container: true,
|
||||||
@ -436,11 +455,11 @@ class _MaterialBannerState extends State<MaterialBanner> {
|
|||||||
materialBannerTransition = materialBanner;
|
materialBannerTransition = materialBanner;
|
||||||
} else {
|
} else {
|
||||||
materialBannerTransition = AnimatedBuilder(
|
materialBannerTransition = AnimatedBuilder(
|
||||||
animation: heightAnimation,
|
animation: _heightAnimation!,
|
||||||
builder: (BuildContext context, Widget? child) {
|
builder: (BuildContext context, Widget? child) {
|
||||||
return Align(
|
return Align(
|
||||||
alignment: AlignmentDirectional.bottomStart,
|
alignment: AlignmentDirectional.bottomStart,
|
||||||
heightFactor: heightAnimation.value,
|
heightFactor: _heightAnimation!.value,
|
||||||
child: child,
|
child: child,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/rendering.dart';
|
import 'package:flutter/rendering.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test('MaterialBannerThemeData copyWith, ==, hashCode basics', () {
|
test('MaterialBannerThemeData copyWith, ==, hashCode basics', () {
|
||||||
@ -324,7 +325,10 @@ void main() {
|
|||||||
expect(find.byType(Divider), findsNothing);
|
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 Color backgroundColor = Colors.purple;
|
||||||
const double elevation = 6.0;
|
const double elevation = 6.0;
|
||||||
const TextStyle textStyle = TextStyle(color: Colors.green);
|
const TextStyle textStyle = TextStyle(color: Colors.green);
|
||||||
|
Loading…
Reference in New Issue
Block a user