Skip to content

Commit

Permalink
Merge pull request #84 from KristiforMilchev/on_hover_states
Browse files Browse the repository at this point in the history
On hover states
  • Loading branch information
itsJoKr authored Feb 15, 2023
2 parents 364f222 + 7084d69 commit b526135
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
10 changes: 8 additions & 2 deletions example/lib/charts/scrollable_chart_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,19 @@ class _ScrollableChartScreenState extends State<ScrollableChartScreen> {
},
),
behaviour: ChartBehaviour(
scrollSettings:
_isScrollable ? ScrollSettings() : ScrollSettings.none(),
scrollSettings: _isScrollable ? ScrollSettings() : ScrollSettings.none(),
onItemClicked: (item) {
print('Clciked');
setState(() {
_selected = item.itemIndex;
});
},
onItemHoverEnter: (_) {
print('Hover Enter');
},
onItemHoverExit: (_) {
print('Hover Enter');
},
),
backgroundDecorations: [
HorizontalAxisDecoration(
Expand Down
1 change: 1 addition & 0 deletions lib/chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:ui';
import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

Expand Down
19 changes: 18 additions & 1 deletion lib/chart/model/theme/chart_behaviour.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ class ChartBehaviour {
const ChartBehaviour({
this.scrollSettings = const ScrollSettings.none(),
this.onItemClicked,
this.onItemHoverEnter,
this.onItemHoverExit,
});

ChartBehaviour._lerp(this.scrollSettings, this.onItemClicked);
ChartBehaviour._lerp(
this.scrollSettings,
this.onItemClicked,
this.onItemHoverEnter,
this.onItemHoverExit,
);

final ScrollSettings scrollSettings;

Expand All @@ -24,11 +31,21 @@ class ChartBehaviour {
/// Return true if chart is currently scrollable
bool get isScrollable => scrollSettings._isScrollable > 0.5;

/// Return index of item clicked. Since graph can be multi value, user
/// will have to handle clicked index to show data they want to show
final ValueChanged<ItemBuilderData>? onItemHoverEnter;

/// Return index of item clicked. Since graph can be multi value, user
/// will have to handle clicked index to show data they want to show
final ValueChanged<ItemBuilderData>? onItemHoverExit;

/// Animate Behaviour from one state to other
static ChartBehaviour lerp(ChartBehaviour a, ChartBehaviour b, double t) {
return ChartBehaviour._lerp(
ScrollSettings.lerp(a.scrollSettings, b.scrollSettings, t),
t > 0.5 ? b.onItemClicked : a.onItemClicked,
t > 0.5 ? b.onItemHoverEnter : a.onItemHoverEnter,
t > 0.5 ? b.onItemHoverExit : a.onItemHoverExit,
);
}
}
10 changes: 10 additions & 0 deletions lib/chart/render/geometry/child_item_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ class _RenderChildChartItem<T> extends RenderShiftedBox {
_state.behaviour.onItemClicked
?.call(ItemBuilderData<T>(item, itemIndex, listIndex));
}

if (event is PointerHoverEvent) {
_state.behaviour.onItemHoverEnter
?.call(ItemBuilderData<T>(item, itemIndex, listIndex));
}

if (event is PointerHoverEvent) {
_state.behaviour.onItemHoverExit
?.call(ItemBuilderData<T>(item, itemIndex, listIndex));
}
}

@override
Expand Down
10 changes: 10 additions & 0 deletions lib/chart/render/geometry/leaf_item_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ class _RenderLeafChartItem<T> extends RenderBox {
_state.behaviour.onItemClicked
?.call(ItemBuilderData<T>(item, itemIndex, listIndex));
}

if (event is PointerHoverEvent) {
_state.behaviour.onItemHoverEnter
?.call(ItemBuilderData<T>(item, itemIndex, listIndex));
}

if (event is PointerHoverEvent) {
_state.behaviour.onItemHoverExit
?.call(ItemBuilderData<T>(item, itemIndex, listIndex));
}
}

@override
Expand Down

0 comments on commit b526135

Please sign in to comment.