Skip to content

Commit

Permalink
fix: Do not dispose external transformation controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Peetee06 committed Dec 18, 2024
1 parent 4b08d0b commit 555f0f8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/src/chart/base/axis_chart/axis_chart_scaffold_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ class _AxisChartScaffoldWidgetState extends State<AxisChartScaffoldWidget> {

@override
void dispose() {
_transformationController.dispose();
_transformationController.removeListener(_updateChartRect);
if (widget.transformationController == null) {
_transformationController.dispose();
}
super.dispose();
}

Expand All @@ -124,13 +127,13 @@ class _AxisChartScaffoldWidgetState extends State<AxisChartScaffoldWidget> {
_transformationController = widget.transformationController!;
_transformationController.addListener(_updateChartRect);
case (TransformationController(), null):
_transformationController.dispose();
_transformationController.removeListener(_updateChartRect);
_transformationController = TransformationController();
_transformationController.addListener(_updateChartRect);
case (TransformationController(), TransformationController()):
if (oldWidget.transformationController !=
widget.transformationController) {
_transformationController.dispose();
_transformationController.removeListener(_updateChartRect);
_transformationController = widget.transformationController!;
_transformationController.addListener(_updateChartRect);
}
Expand Down
36 changes: 34 additions & 2 deletions test/chart/base/axis_chart/axis_chart_scaffold_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,10 @@ void main() {
expect(chartRects, actualChartRects..add(isNotScaled));

expect(transformationController2, isNot(transformationController));
expect(
() => transformationController.addListener(() {}),
throwsA(isA<FlutterError>()),
);
transformationController2.value = Matrix4.identity()..scale(2.0);
await tester.pump();
expect(chartRects, actualChartRects..add(isScaled));
Expand All @@ -1257,7 +1261,8 @@ void main() {

testWidgets(
'oldWidget.controller is not null and widget.controller is null: '
'disposes old controller and sets up new controller with listeners',
'removes listeners from old controller and sets up new controller '
'with listeners',
(WidgetTester tester) async {
final actualChartRects = <Object?>[isNotScaled, isNotScaled];
final transformationController = TransformationController();
Expand All @@ -1278,6 +1283,9 @@ void main() {

final transformationController2 = getTransformationController(tester);
expect(transformationController2, isNot(transformationController));
// ignore: invalid_use_of_protected_member
expect(transformationController.hasListeners, false);
transformationController.addListener(() {}); // throws if disposed
transformationController2!.value = Matrix4.identity()..scale(2.0);
await tester.pump();
expect(chartRects, actualChartRects..add(isScaled));
Expand All @@ -1287,7 +1295,8 @@ void main() {
testWidgets(
'oldWidget.controller is not null and widget.controller is not null, '
'controllers are different: '
'disposes old controller and sets up widget.controller with listeners',
'removes listeners from old controller and sets up '
'widget.controller with listeners',
(WidgetTester tester) async {
final actualChartRects = <Object?>[isNotScaled, isNotScaled];
final transformationController = TransformationController();
Expand All @@ -1311,6 +1320,9 @@ void main() {
expect(chartRects, actualChartRects..add(isNotScaled));

expect(transformationController2, isNot(transformationController));
// ignore: invalid_use_of_protected_member
expect(transformationController.hasListeners, false);
transformationController.addListener(() {}); // throws if disposed
transformationController2.value = Matrix4.identity()..scale(2.0);
await tester.pump();
expect(chartRects, actualChartRects..add(isScaled));
Expand Down Expand Up @@ -1383,5 +1395,25 @@ void main() {
expect(chartRects, actualChartRects..add(isNotScaled));
},
);

testWidgets('does not dispose external controller',
(WidgetTester tester) async {
final controller = TransformationController();
await tester.pumpWidget(
MaterialApp(
home: AxisChartScaffoldWidget(
data: lineChartDataWithNoTitles,
transformationController: controller,
chartBuilder: (context, rect) {
return dummyChart;
},
),
),
);
await tester.pumpWidget(Container());
// ignore: invalid_use_of_protected_member
expect(controller.hasListeners, false);
controller.addListener(() {}); // throws if disposed
});
});
}

0 comments on commit 555f0f8

Please sign in to comment.