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

Bugfix/flspot null equality #1646

Merged
merged 1 commit into from
May 1, 2024
Merged
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
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