Skip to content

Commit

Permalink
Fix equality check for FlSpot.null() values, #1607
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed May 1, 2024
1 parent a269575 commit ab38584
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lib/src/chart/base/axis_chart/axis_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ class FlTitlesData with EquatableMixin {
}

/// Represents a conceptual position in cartesian (axis based) space.
class FlSpot with EquatableMixin {
@immutable
class FlSpot {
/// [x] determines cartesian (axis based) horizontally position
/// 0 means most left point of the chart
///
Expand Down Expand Up @@ -477,13 +478,6 @@ class FlSpot with EquatableMixin {
/// Determines if [x] and [y] is not null.
bool isNotNull() => !isNull();

/// Used for equality check, see [EquatableMixin].
@override
List<Object?> get props => [
x,
y,
];

/// Lerps a [FlSpot] based on [t] value, check [Tween.lerp].
static FlSpot lerp(FlSpot a, FlSpot b, double t) {
if (a == FlSpot.nullSpot) {
Expand All @@ -499,6 +493,25 @@ class FlSpot with EquatableMixin {
lerpDouble(a.y, b.y, t)!,
);
}

/// Two [FlSpot] are equal if their [x] and [y] are equal.
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! FlSpot) {
return false;
}

if (x.isNaN && y.isNaN && other.x.isNaN && other.y.isNaN) {
return true;
}

return other.x == x && other.y == y;
}

/// Override hashCode
@override
int get hashCode => x.hashCode ^ y.hashCode;
}

/// Responsible to hold grid data,
Expand Down
6 changes: 6 additions & 0 deletions test/chart/base/axis_chart/axis_chart_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ void main() {
expect(flSpot1 == flSpot2, false);

expect(flSpot2 == flSpot2Clone, true);

expect(nullSpot1 == nullSpot2, true);

expect(nullSpot2 == nullSpot3, true);

expect(nullSpot1 == nullSpot3, true);
});

test('FlGridData equality test', () {
Expand Down
4 changes: 4 additions & 0 deletions test/chart/data_pool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@ final FlSpot flSpot1Clone = flSpot1.copyWith();
const FlSpot flSpot2 = FlSpot(4, 2);
final FlSpot flSpot2Clone = flSpot2.copyWith();

const nullSpot1 = FlSpot.nullSpot;
final nullSpot2 = nullSpot1.copyWith();
const nullSpot3 = FlSpot.nullSpot;

Widget getTitles(double value, TitleMeta meta) => const Text('sallam');

TextStyle getTextStyles(BuildContext context, double value) =>
Expand Down

0 comments on commit ab38584

Please sign in to comment.