@@ -4,6 +4,7 @@ import 'dart:math';
44import 'package:equatable/equatable.dart' ;
55import 'package:flutter/material.dart' ;
66import 'package:flutter_bloc/flutter_bloc.dart' ;
7+ import 'package:interactive_timeline/src/utils.dart' as utils;
78
89// timeline cubit
910class InteractiveTimelineCubit extends Cubit <InteractiveTimelineState > {
@@ -23,11 +24,7 @@ class InteractiveTimelineCubit extends Cubit<InteractiveTimelineState> {
2324 minCursor,
2425 maxCursor,
2526 }) : super (
26- InteractiveTimelineState .initializeAtTime (
27- initialTime ?? DateTime .now (),
28- minCursor,
29- maxCursor,
30- ),
27+ InteractiveTimelineState .initializeAtTime (initialTime ?? DateTime .now ()),
3128 );
3229
3330 double _restrictZoomLevel (double secondsPerScreenWidth) {
@@ -104,8 +101,21 @@ class InteractiveTimelineCubit extends Cubit<InteractiveTimelineState> {
104101 }
105102 }
106103
107- // TODO: Using null won't clear the variables
108- void setMinMax ({DateTime ? min, DateTime ? max}) => emit (state.overwrite (minCursor: min, maxCursor: max));
104+ void setMinMax ({DateTime ? minCursor, DateTime ? maxCursor}) {
105+ var newState = state.setMinMaxCursor (minCursor: minCursor, maxCursor: maxCursor);
106+ newState = newState.overwrite (
107+ middleCursor: newState.cropMinMaxCursor (newState.middleCursor),
108+ );
109+ emit (newState);
110+ }
111+
112+ void setCursor (DateTime cursor) {
113+ emit (
114+ state.overwrite (
115+ middleCursor: state.cropMinMaxCursor (cursor),
116+ ),
117+ );
118+ }
109119
110120 void _tickTimer (Duration duration) {
111121 if (! state.isInteracting) {
@@ -123,12 +133,14 @@ class InteractiveTimelineCubit extends Cubit<InteractiveTimelineState> {
123133 }
124134 }
125135
126- void initialize (double width, double height) {
136+ void initialize (double width, double height, [ DateTime ? minCursor, DateTime ? maxCursor] ) {
127137 emit (state.overwrite (
128138 secondsPerScreenWidth: initialZoomLevel, // default zoom level
129139 middleCursor: initialTime,
130140 width: width,
131141 height: height,
142+ minCursor: minCursor,
143+ maxCursor: maxCursor,
132144 ));
133145 }
134146}
@@ -167,24 +179,36 @@ class InteractiveTimelineState extends Equatable {
167179 @override
168180 List <Object > get props => [width, height, secondsPerPixel, secondsPerScreenWidth, secondsPerScreenWidthBeforeZoom, middleCursor, leftCursor, rightCursor, isPlaying, isInteracting];
169181
170- static InteractiveTimelineState initializeAtTime (DateTime time, DateTime ? minCursor, DateTime ? maxCursor ) {
182+ static InteractiveTimelineState initializeAtTime (DateTime time) {
171183 return InteractiveTimelineState (
172- width: 0 ,
173- height: 0 ,
174- secondsPerPixel: 0 ,
175- secondsPerScreenWidth: 0 ,
176- secondsPerScreenWidthBeforeZoom: 0 ,
184+ width: 1 ,
185+ height: 1 ,
186+ secondsPerPixel: 1 ,
187+ secondsPerScreenWidth: 1 ,
188+ secondsPerScreenWidthBeforeZoom: 1 ,
177189 middleCursor: time,
178190 leftCursor: time,
179191 rightCursor: time,
180- minCursor: minCursor ,
181- maxCursor: maxCursor ,
192+ minCursor: null ,
193+ maxCursor: null ,
182194 playTimer: null ,
183195 isPlaying: false ,
184196 isInteracting: false ,
185197 );
186198 }
187199
200+ bool insideMinMaxCursor (DateTime cursor, [DateTime ? minCursor, DateTime ? maxCursor]) => utils.insideMinMaxCursor (
201+ cursor,
202+ minCursor ?? this .minCursor,
203+ maxCursor ?? this .maxCursor,
204+ );
205+
206+ DateTime cropMinMaxCursor (DateTime cursor, [DateTime ? minCursor, DateTime ? maxCursor]) => utils.cropMinMaxCursor (
207+ cursor,
208+ minCursor ?? this .minCursor,
209+ maxCursor ?? this .maxCursor,
210+ );
211+
188212 DateTime getLeftCursor (double width, double secondsPerPixel) {
189213 return middleCursor.subtract (
190214 Duration (milliseconds: ((width / 2 ) * secondsPerPixel * 1000 ).toInt ()),
@@ -197,10 +221,23 @@ class InteractiveTimelineState extends Equatable {
197221 );
198222 }
199223
200- bool insideMinMaxCursor (DateTime time) {
201- if (minCursor != null ) if (time.isBefore (minCursor! )) return false ;
202- if (maxCursor != null ) if (time.isAfter (maxCursor! )) return false ;
203- return true ;
224+ // minCursor and maxCursor should be changable to null (.overwrite would fall back when passed null)
225+ InteractiveTimelineState setMinMaxCursor ({DateTime ? minCursor, DateTime ? maxCursor}) {
226+ return InteractiveTimelineState (
227+ width: width,
228+ height: height,
229+ secondsPerPixel: secondsPerPixel,
230+ secondsPerScreenWidth: secondsPerScreenWidth,
231+ secondsPerScreenWidthBeforeZoom: secondsPerScreenWidthBeforeZoom,
232+ middleCursor: middleCursor,
233+ leftCursor: leftCursor,
234+ rightCursor: rightCursor,
235+ minCursor: minCursor,
236+ maxCursor: maxCursor,
237+ playTimer: playTimer,
238+ isPlaying: isPlaying,
239+ isInteracting: isInteracting,
240+ );
204241 }
205242
206243 InteractiveTimelineState overwrite ({
0 commit comments