StopWatchTimer _stopWatchTimer = StopWatchTimer(
mode: StopWatchMode.countUp,
onChangeRawSecond: (value) => print('presetMillisecond: $value'),
onChangeRawMinute: (value) => print('onChangeRawMinute $value'),
);
StreamBuilder<int>(
stream: _stopWatchTimer.secondTime,
initialData: _stopWatchTimer.secondTime.value,
builder: (context, snap) {
var value = snap.data;
return StreamBuilder<int>(
stream: _stopWatchTimer.secondTime,
initialData: _stopWatchTimer.secondTime.value,
builder: (context, snap) {
var value = snap.data;
return Card(
child: Padding(
padding: EdgeInsets.all(10),
child: InkWell(
onTap: () {
selectedButton = key;
if (!_stopWatchTimer.isRunning) {
_stopWatchTimer.onExecute.add(StopWatchExecute.reset);
_stopWatchTimer.onExecute.add(StopWatchExecute.start);
} else {
selectedButton = '';
_stopWatchTimer.onExecute.add(StopWatchExecute.reset);
_stopWatchTimer.onExecute.add(StopWatchExecute.stop);
}
}
},
child:
Text( '${value.toString()}',
style: GoogleFonts.roboto(
textStyle: TextStyle(
color: Color.fromRGBO(6, 8, 15, 1),
fontWeight: FontWeight.w700,
fontSize: 14.sp,
)),
)
),
));
},
);,
class TimerService extends ChangeNotifier {
Stopwatch _watch = Stopwatch();
var _timer;
Duration get currentDuration => _currentDuration;
Duration _currentDuration = Duration.zero;
bool get isRunning => _timer != null;
TimerService() {
_watch = Stopwatch();
}
String get getTime => getDisplayTime(_currentDuration.inMilliseconds);
static String getDisplayTime(){
//перевод человеко читаемый вид(если кому надо пишите скину код)
}
void _onTick(Timer timer) {
_currentDuration = _watch.elapsed;
print(_currentDuration);
// notify all listening widgets
notifyListeners();
}
void start() {
if (_timer != null) return;
_timer = null;
_timer = Timer.periodic(Duration(seconds: 1), _onTick);
_watch.start();
notifyListeners();
}
void stop() {
_timer?.cancel();
_watch.stop();
_currentDuration = _watch.elapsed;
notifyListeners();
}
void reset() {
stop();
_watch.reset();
_currentDuration = Duration.zero;
notifyListeners();
}
}
class TimerServiceProvider extends InheritedWidget {
const TimerServiceProvider({Key? key, required this.service, required Widget
child})
: super(key: key, child: child);
final TimerService service;
@override
bool updateShouldNotify(TimerServiceProvider old) => service != old.service;
}
AnimatedBuilder(
animation: globalsVars.timerServ,
builder: (context, child) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 5 , left: 12),
decoration: (selectedButton == key)
? BoxDecoration(
border: Border.all(
color: Colors.white10,
width: 2,
),
borderRadius: BorderRadius.circular(5),
)
: null,
child: Text(
'${globalsVars.timerServ.getTime}',
style: GoogleFonts.roboto(
textStyle: TextStyle(
color: Colors.red,
fontWeight: FontWeight.w700,
fontSize: 16.sp,
)),
))
],
);
},
),
final startTime = DateTime(1967, 10, 12); //Тут будет значение из хранилища
final now = DateTime.now();
final difference = startTime.difference(birthday).inSeconds;