Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Memory leaks for Animations #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/src/chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class _ChartState extends State<Chart> with TickerProviderStateMixin {
@override
void dispose() {
_controller.dispose();
widget.layers.forEach((e) => e.dispose());
super.dispose();
}

Expand All @@ -86,7 +87,7 @@ class _ChartState extends State<Chart> with TickerProviderStateMixin {
Expanded(
child: AnimatedBuilder(
animation: _controller,
builder: (_, __) => ChartTouchDetector<dynamic>(
builder: (_, __) => ChartTouchDetector<ChartDataItem>(
onShapes: () => _touchableShapes,
onTap: ((touchPosition, data) => setState(() {
_touchedData = data == null
Expand Down
18 changes: 10 additions & 8 deletions lib/src/models/animation/chart_color_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ part of 'chart_animation.dart';
/// Provides color animation values.
class ChartColorAnimation implements ChartAnimation {
Animation<Color?>? _animation;
CurvedAnimation? _listener;

Color _lastColor = Colors.transparent;

ChartColorAnimation();
Expand All @@ -21,6 +23,7 @@ class ChartColorAnimation implements ChartAnimation {
@override
void dispose() {
_animation = null;
_listener?.dispose();
}

/// Initialize animation.
Expand All @@ -31,15 +34,14 @@ class ChartColorAnimation implements ChartAnimation {
Color? initialColor,
ChartColorAnimation? oldAnimation,
}) {
final Animation<Color?> animation = ColorTween(
_listener?.dispose();
_listener = CurvedAnimation(
parent: controller,
curve: curve,
);
_animation = ColorTween(
begin: oldAnimation?._lastColor ?? initialColor ?? Colors.transparent,
end: color,
).animate(
CurvedAnimation(
parent: controller,
curve: curve,
),
);
_animation = animation;
).animate(_listener!);
}
}
17 changes: 9 additions & 8 deletions lib/src/models/animation/chart_position_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ part of 'chart_animation.dart';
/// Provides position animation values.
class ChartPositionAnimation implements ChartAnimation {
Animation<Offset>? _animation;
CurvedAnimation? _listener;
Offset _lastPosition = Offset.zero;

ChartPositionAnimation();
Expand All @@ -21,6 +22,7 @@ class ChartPositionAnimation implements ChartAnimation {
@override
void dispose() {
_animation = null;
_listener?.dispose();
}

/// Initialize animation.
Expand All @@ -31,15 +33,14 @@ class ChartPositionAnimation implements ChartAnimation {
Offset? initialPosition,
ChartPositionAnimation? oldAnimation,
}) {
final Animation<Offset> animation = Tween<Offset>(
_listener?.dispose();
_listener = CurvedAnimation(
parent: controller,
curve: curve,
);
_animation = Tween<Offset>(
begin: oldAnimation?._lastPosition ?? initialPosition ?? position,
end: position,
).animate(
CurvedAnimation(
parent: controller,
curve: curve,
),
);
_animation = animation;
).animate(_listener!);
}
}
17 changes: 9 additions & 8 deletions lib/src/models/animation/chart_size_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ part of 'chart_animation.dart';
/// Provides size animation values.
class ChartSizeAnimation implements ChartAnimation {
Animation<Size>? _animation;
CurvedAnimation? _listener;
Size _lastSize = Size.zero;

ChartSizeAnimation();
Expand All @@ -21,6 +22,7 @@ class ChartSizeAnimation implements ChartAnimation {
@override
void dispose() {
_animation = null;
_listener?.dispose();
}

/// Initialize animation.
Expand All @@ -31,15 +33,14 @@ class ChartSizeAnimation implements ChartAnimation {
Size? initialSize,
ChartSizeAnimation? oldAnimation,
}) {
final Animation<Size> animation = Tween<Size>(
_listener?.dispose();
_listener = CurvedAnimation(
parent: controller,
curve: curve,
);
_animation = Tween<Size>(
begin: oldAnimation?._lastSize ?? initialSize ?? size,
end: size,
).animate(
CurvedAnimation(
parent: controller,
curve: curve,
),
);
_animation = animation;
).animate(_listener!);
}
}
18 changes: 10 additions & 8 deletions lib/src/models/animation/chart_text_style_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ part of 'chart_animation.dart';
/// Provides text style animation values.
class ChartTextStyleAnimation implements ChartAnimation {
Animation<TextStyle>? _animation;
CurvedAnimation? _listener;

TextStyle _lastTextStyle = const TextStyle(
color: Colors.transparent,
);
Expand All @@ -26,6 +28,7 @@ class ChartTextStyleAnimation implements ChartAnimation {
@override
void dispose() {
_animation = null;
_listener?.dispose();
}

/// Initialize animation.
Expand All @@ -36,19 +39,18 @@ class ChartTextStyleAnimation implements ChartAnimation {
TextStyle? initialTextSyle,
ChartTextStyleAnimation? oldAnimation,
}) {
final Animation<TextStyle> animation = TextStyleTween(
_listener?.dispose();
_listener = CurvedAnimation(
parent: controller,
curve: curve,
);
_animation = TextStyleTween(
begin: oldAnimation?._lastTextStyle ??
initialTextSyle ??
const TextStyle(
color: Colors.transparent,
),
end: textStyle,
).animate(
CurvedAnimation(
parent: controller,
curve: curve,
),
);
_animation = animation;
).animate(_listener!);
}
}
1 change: 1 addition & 0 deletions lib/src/models/line/chart_line_data_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ChartLineDataItem extends ChartDataItem {
@override
void dispose() {
_value.dispose();
_touch.dispose();
}

/// Initialize touch area animations.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mrx_charts
description: Simple, high-performance Flutter charts with beautiful animations.
version: 0.1.3
version: 0.1.4
repository: /~https://github.com/merixstudio/mrx-flutter-charts
issue_tracker: /~https://github.com/merixstudio/mrx-flutter-charts/issues
homepage:
Expand Down