Skip to content

Commit

Permalink
Add onDateTime property so the last display candle will match the des…
Browse files Browse the repository at this point in the history
…ired year, month and day
  • Loading branch information
christenbc committed Oct 27, 2022
1 parent cc07bfb commit 1933e11
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/src/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Candlesticks extends StatefulWidget {

final CandleSticksStyle? style;

final DateTime? onDateTime;

const Candlesticks({
Key? key,
required this.candles,
Expand All @@ -57,8 +59,8 @@ class Candlesticks extends StatefulWidget {
this.indicators,
this.onRemoveIndicator,
this.style,
}) : assert(candles.length == 0 || candles.length > 1,
"Please provide at least 2 candles"),
this.onDateTime,
}) : assert(candles.length == 0 || candles.length > 1, "Please provide at least 2 candles"),
super(key: key);

@override
Expand All @@ -68,7 +70,7 @@ class Candlesticks extends StatefulWidget {
class _CandlesticksState extends State<Candlesticks> {
/// index of the newest candle to be displayed
/// changes when user scrolls along the chart
int index = -10;
late int index;
double lastX = 0;
int lastIndex = -10;

Expand All @@ -84,8 +86,16 @@ class _CandlesticksState extends State<Candlesticks> {
@override
void initState() {
super.initState();
if (widget.candles.length == 0) {
final candlesLength = widget.candles.length;
if (candlesLength == 0) {
return;
} else if (widget.onDateTime != null) {
// TODO: only works for the available list of candles. Make it work for date times older than the oldest available candlestick
final indexOfDesiredDateTime = widget.candles
.indexWhere((e) => e.date.month == widget.onDateTime?.month && e.date.day == widget.onDateTime?.day);
index = indexOfDesiredDateTime;
} else {
index = -10;
}
if (mainWindowDataContainer == null) {
mainWindowDataContainer =
Expand Down

0 comments on commit 1933e11

Please sign in to comment.