federicodesia / custom_timer Goto Github PK
View Code? Open in Web Editor NEWA Flutter package to create a customizable timer.
Home Page: https://pub.dev/packages/custom_timer
License: MIT License
A Flutter package to create a customizable timer.
Home Page: https://pub.dev/packages/custom_timer
License: MIT License
on version 0.0.3
this worked fine but since I upgraded to 0.0.5
the counter goes down to negative values
CustomTimer(
controller: _controller,
from: Duration(seconds: 0),
to: Duration(hours: 10),
interval: Duration(milliseconds: 500),
onBuildAction: CustomTimerAction.auto_start,
builder: (CustomTimerRemainingTime remaining) {
return Text(
'${remaining.minutes}:${remaining.seconds}',
style: _tema.title2().copyWith(color: _tema.backgroundTertiary()),
);
},
)
CustomTimer( from: Duration(milliseconds: 0), to: Duration(hours: 1), onBuildAction: CustomTimerAction.auto_start, builder: (CustomTimerRemainingTime remaining) { return Text( "${remaining.minutes}:${remaining.seconds}:${remaining.milliseconds} ", style: TextStyle(fontSize: 20.0), ); }, ),
Love this package, nice and simple & thank you for producing it. Now, I think something important could be improved.
I'm no Flutter pro engineer, mega computer genius, crazy Stephen Hawking intellectual, but rebuilding a widget a million times per second has always since I started learning Flutter been a serious main issue, concern & my arch nemesis and looks like Flutter's also. However it is also necessary IF one is showing milliseconds as part of their timer. I can understand this aspect of the programming. This uses enormous system resources in debug mode.
So my proposal is to include some sort of boolean value updateForMilliseconds or something which uses state and does not rebuild so much when not necessary when not using milliseconds value from timer. If using milliseconds then use the good old stateful widget and rebuild it quickly so user can see milliseconds passing.
I suppose all timers would have this issue and this is why I think this could be very valuable when some don't need rapid updates.
Showing my performance issue
class OTPTimer extends StatelessWidget {
final CustomTimerController timeController;
const OTPTimer({
required this.timeController,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return CustomTimer(
controller: timeController,
begin: const Duration(minutes: 10),
end: const Duration(minutes: 0),
builder: (timeRemaining) {
return Text(
"${timeRemaining.hours}:${timeRemaining.minutes}:${timeRemaining.seconds}",
style: timerStyle(timeRemaining),
);
});
}
// // Timer time text
TextStyle timerStyle(CustomTimerRemainingTime timeRemaining) {
print(int.parse(timeRemaining.secondsWithoutFill)); // Prints 40-60 times per second (useless in my case)
// Green indicator text
if (int.parse(timeRemaining.minutesWithoutFill) > 3) {
return ThemeEndpoints.infoHeaderPositive();
}
// Yellow indicator warning text
if (int.parse(timeRemaining.minutesWithoutFill) < 3 &&
int.parse(timeRemaining.minutesWithoutFill) > 1) {
return ThemeEndpoints.infoHeaderWarning();
}
// Red indicator warning text
return ThemeEndpoints.infoHeaderNegative();
}
If set the interval: CustomTimerInterval.seconds, can normal execution code for the first time. However, when you reset begin for the second countdown, it will end directly.
CustomTimerController( vsync: this, begin: Duration(seconds: state.duration.value), end: const Duration(), initialState: CustomTimerState.reset, interval: CustomTimerInterval.seconds)
interval: CustomTimerInterval.milliseconds is ok;
interval: CustomTimerInterval.seconds is failed;
When the counter reaches the end its still in CustomTimerState.counting
. I would like to take action and update certain state values once the clock hits 00:00:00. What would be the best way to do that please?
end default value can be Duration.zero
Currently it does not have a default value and needs to get specify
CustomTimerController(
vsync: this,
begin: Duration(hours: 24),
end: Duration.zero,
);
i was planning to use it to control calls. how can i set it to count up. .... and
return the duration- how long the call lasted. thanks
I wrapped CustomTimer with Visibility and expected the timer not to reset when I hide/show but the timer always reset.
I am trying to set up a countdown timer but it keeps throwing an error. Controller is already disposed here's my code
CustomTimer(
controller: controller,
begin: Duration(seconds: 30),
end: Duration(seconds: 0),
onChangeState: (s) {
if(s == CustomTimerState.finished) {
print("We completed finished");
// Prevent calling setState during a widget build
showTimer.value = false;
controller.reset();
}
},
builder: (remaining) {
return Text("${remaining.minutes}:${remaining.seconds}", style: TextStyle(
fontFamily: "Montserrat",
fontWeight: FontWeight.w600,
color: primaryColor,
fontSize: 14,
));
},
),
how to do something like in a soccer match where you go from 00:00 to 10:95 to 45.00 and so and so ! is this possible?
I was expecting the behaviour of onFinish
to be triggered when it gets to Duration(seconds: 0)
yet it didn't and it continued to negative values, is this a bug? thanks!
Widget _timer() {
return CustomTimer(
controller: _timerController,
from: widget.durationLeft,
to: Duration(seconds: 0),
interval: Duration(seconds: 1),
onFinish: () => widget.onFinish(),
onBuildAction: CustomTimerAction.auto_start,
builder: (CustomTimerRemainingTime remaining) {
return Text('${remaining.hours}:${remaining.minutes}:${remaining.seconds}');
},
);
}
I need a custom duration: eventDate.difference(DateTime.now())
for the from
Yet the onFinished
wasn't being triggered so I tried to add microseconds to the from
property and indeed it doesn't trigger it and continue to countdown below 0
from: Duration(seconds: 10, microseconds: 11111),
CustomTimer(
from: Duration(seconds: 10, microseconds: 11111),
to: Duration(hours: 0),
onBuildAction: CustomTimerAction.auto_start,
onFinish: () => _refreshData(),
builder: (CustomTimerRemainingTime remaining) {
return Text(
'${remaining.hours}:${remaining.minutes}:${remaining.seconds}')
);
},
)
A workaround that works is convert to seconds and convert to Duration
from: Duration(seconds: Duration(seconds: 10, microseconds: 11111).inSeconds),
I want to use the same time accessed between multiple screens, The timer is resetting in every screen currently, even when I use the same controller.
And when I come back to the previous screen, two instances of the timer running.
I want one instance of the timer used across multiple screens, do we have an option for that?
It would be nice to be have the controller be able to keep track of the timer regardless of whether or not the timer is in view or not.
I envision the controller being the source of truth for the timer widget and the widget simply latches on to the current state of the controller it was initialized with whenever it comes into view. This way we can get and set the timer from just the controller in other parts of an app.
The initial discussions for this issue can be found here.
I am trying to set up a countdown timer when state was refreshed, but it throwing an error and i'm not using void dispose();
How to fix it?
Please can you migrate to null safety
Hi, previously I used "onBuildAction: CustomTimerAction.auto_start", but now it's gone, and the timer doesn't auto start. How to auto start on build without using the external controller?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.