Skip to content

Commit 1d967ad

Browse files
committed
fix CurvedAnimation & overlayEntry memory leak
1 parent 66ceef3 commit 1d967ad

File tree

8 files changed

+117
-23
lines changed

8 files changed

+117
-23
lines changed

lib/src/custom/custom_dialog.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ class CustomDialog extends BaseDialog {
384384
closeType: closeType,
385385
);
386386
customDialog.overlayEntry.remove();
387+
customDialog.overlayEntry.dispose();
387388
}
388389

389390
static DialogInfo? _getDialog({

lib/src/custom/custom_notify.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ class CustomNotify extends BaseDialog {
211211
closeType: closeType,
212212
);
213213
customDialog.overlayEntry.remove();
214+
customDialog.overlayEntry.dispose();
214215
}
215216

216217
static NotifyInfo? _getDialog({String? tag}) {

lib/src/custom/toast/custom_toast.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class CustomToast extends BaseDialog {
145145
for (var item in toastQueue) {
146146
await ViewUtils.awaitPostFrame(onPostFrame: () {
147147
item.mainDialog.overlayEntry.remove();
148+
item.mainDialog.overlayEntry.dispose();
148149
});
149150
}
150151
toastQueue.clear();
@@ -224,6 +225,7 @@ class CustomToast extends BaseDialog {
224225
Timer(time, () async {
225226
await mainDialog.dismiss();
226227
mainDialog.overlayEntry.remove();
228+
mainDialog.overlayEntry.dispose();
227229
});
228230
}
229231

lib/src/custom/toast/toast_tool.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ToastTool {
3838
await curToast.mainDialog.dismiss();
3939
if (curToast.mainDialog.overlayEntry.mounted) {
4040
curToast.mainDialog.overlayEntry.remove();
41+
curToast.mainDialog.overlayEntry.dispose();
4142
}
4243
await Future.delayed(SmartDialog.config.toast.intervalTime);
4344
}
@@ -51,6 +52,7 @@ class ToastTool {
5152
for (var element in toastQueue) {
5253
if (element.mainDialog.overlayEntry.mounted) {
5354
element.mainDialog.overlayEntry.remove();
55+
element.mainDialog.overlayEntry.dispose();
5456
}
5557
}
5658
toastQueue.clear();
Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
22

3-
class FadeAnimation extends StatelessWidget {
3+
class FadeAnimation extends StatefulWidget {
44
const FadeAnimation({
55
Key? key,
66
required this.controller,
@@ -11,11 +11,33 @@ class FadeAnimation extends StatelessWidget {
1111

1212
final Widget child;
1313

14+
@override
15+
State<FadeAnimation> createState() => _FadeAnimationState();
16+
}
17+
18+
class _FadeAnimationState extends State<FadeAnimation> {
19+
late CurvedAnimation _curvedAnimation;
20+
21+
@override
22+
void initState() {
23+
super.initState();
24+
_curvedAnimation = CurvedAnimation(
25+
parent: widget.controller,
26+
curve: Curves.linear,
27+
);
28+
}
29+
30+
@override
31+
void dispose() {
32+
_curvedAnimation.dispose();
33+
super.dispose();
34+
}
35+
1436
@override
1537
Widget build(BuildContext context) {
1638
return FadeTransition(
17-
opacity: CurvedAnimation(parent: controller, curve: Curves.linear),
18-
child: child,
39+
opacity: _curvedAnimation,
40+
child: widget.child,
1941
);
2042
}
2143
}

lib/src/widget/animation/highlight_mask_animation.dart

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
33

44
import '../attach_dialog_widget.dart';
55

6-
class HighlightMaskAnimation extends StatelessWidget {
6+
class HighlightMaskAnimation extends StatefulWidget {
77
const HighlightMaskAnimation({
88
Key? key,
99
required this.controller,
@@ -31,21 +31,43 @@ class HighlightMaskAnimation extends StatelessWidget {
3131

3232
final List<SmartNonAnimationType> nonAnimationTypes;
3333

34+
@override
35+
State<HighlightMaskAnimation> createState() => _HighlightMaskAnimationState();
36+
}
37+
38+
class _HighlightMaskAnimationState extends State<HighlightMaskAnimation> {
39+
late CurvedAnimation _curvedAnimation;
40+
41+
@override
42+
void initState() {
43+
super.initState();
44+
_curvedAnimation = CurvedAnimation(
45+
parent: widget.controller,
46+
curve: Curves.linear,
47+
);
48+
}
49+
50+
@override
51+
void dispose() {
52+
_curvedAnimation.dispose();
53+
super.dispose();
54+
}
55+
3456
@override
3557
Widget build(BuildContext context) {
3658
//handle mask
3759
late Widget mask;
38-
if (usePenetrate) {
60+
if (widget.usePenetrate) {
3961
mask = Container();
40-
} else if (maskWidget != null) {
41-
mask = maskWidget!;
42-
} else if (highlightBuilder == null) {
43-
mask = Container(color: maskColor);
62+
} else if (widget.maskWidget != null) {
63+
mask = widget.maskWidget!;
64+
} else if (widget.highlightBuilder == null) {
65+
mask = Container(color: widget.maskColor);
4466
} else {
4567
mask = ColorFiltered(
4668
colorFilter: ColorFilter.mode(
4769
// mask color
48-
maskColor,
70+
widget.maskColor,
4971
BlendMode.srcOut,
5072
),
5173
child: Stack(children: [
@@ -58,17 +80,17 @@ class HighlightMaskAnimation extends StatelessWidget {
5880
),
5981

6082
//dissolve mask, highlight location
61-
highlightBuilder!.call(targetOffset, targetSize)
83+
widget.highlightBuilder!.call(widget.targetOffset, widget.targetSize)
6284
]),
6385
);
6486
}
6587

6688
Widget maskAnimation = FadeTransition(
67-
opacity: CurvedAnimation(parent: controller, curve: Curves.linear),
89+
opacity: _curvedAnimation,
6890
child: mask,
6991
);
70-
if (highlightBuilder != null) {
71-
for (var element in nonAnimationTypes) {
92+
if (widget.highlightBuilder != null) {
93+
for (var element in widget.nonAnimationTypes) {
7294
if (element == SmartNonAnimationType.highlightMask_nonAnimation) {
7395
maskAnimation = mask;
7496
}

lib/src/widget/animation/mask_animation.dart

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
22

3-
class MaskAnimation extends StatelessWidget {
3+
class MaskAnimation extends StatefulWidget {
44
const MaskAnimation({
55
Key? key,
66
required this.controller,
@@ -17,13 +17,35 @@ class MaskAnimation extends StatelessWidget {
1717

1818
final bool usePenetrate;
1919

20+
@override
21+
State<MaskAnimation> createState() => _MaskAnimationState();
22+
}
23+
24+
class _MaskAnimationState extends State<MaskAnimation> {
25+
late CurvedAnimation _curvedAnimation;
26+
27+
@override
28+
void initState() {
29+
super.initState();
30+
_curvedAnimation = CurvedAnimation(
31+
parent: widget.controller,
32+
curve: Curves.linear,
33+
);
34+
}
35+
36+
@override
37+
void dispose() {
38+
_curvedAnimation.dispose();
39+
super.dispose();
40+
}
41+
2042
@override
2143
Widget build(BuildContext context) {
2244
return FadeTransition(
23-
opacity: CurvedAnimation(parent: controller, curve: Curves.linear),
24-
child: (maskWidget != null && !usePenetrate)
25-
? maskWidget
26-
: Container(color: usePenetrate ? null : maskColor),
45+
opacity: _curvedAnimation,
46+
child: (widget.maskWidget != null && !widget.usePenetrate)
47+
? widget.maskWidget
48+
: Container(color: widget.usePenetrate ? null : widget.maskColor),
2749
);
2850
}
2951
}

lib/src/widget/animation/scale_animation.dart

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
22

3-
class ScaleAnimation extends StatelessWidget {
3+
class ScaleAnimation extends StatefulWidget {
44
const ScaleAnimation({
55
Key? key,
66
required this.controller,
@@ -14,12 +14,34 @@ class ScaleAnimation extends StatelessWidget {
1414

1515
final Alignment? alignment;
1616

17+
@override
18+
State<ScaleAnimation> createState() => _ScaleAnimationState();
19+
}
20+
21+
class _ScaleAnimationState extends State<ScaleAnimation> {
22+
late CurvedAnimation _curvedAnimation;
23+
24+
@override
25+
void initState() {
26+
super.initState();
27+
_curvedAnimation = CurvedAnimation(
28+
parent: widget.controller,
29+
curve: Curves.linear,
30+
);
31+
}
32+
33+
@override
34+
void dispose() {
35+
_curvedAnimation.dispose();
36+
super.dispose();
37+
}
38+
1739
@override
1840
Widget build(BuildContext context) {
1941
return ScaleTransition(
20-
alignment: alignment ?? const Alignment(0, 0),
21-
scale: CurvedAnimation(parent: controller, curve: Curves.linear),
22-
child: child,
42+
alignment: widget.alignment ?? const Alignment(0, 0),
43+
scale: _curvedAnimation,
44+
child: widget.child,
2345
);
2446
}
2547
}

0 commit comments

Comments
 (0)