β
Custom Timer A Flutter package to create a customizable timer.
π Features
- Timer controller.
- Auto count up / down timer.
- Custom builders.
π Simple Usage
final CustomTimerController _controller = CustomTimerController();
CustomTimer(
controller: _controller,
begin: Duration(days: 1),
end: Duration(),
builder: (time) {
return Text(
"${time.hours}:${time.minutes}:${time.seconds}.${time.milliseconds}",
style: TextStyle(fontSize: 24.0)
);
}
)
Now you can use the controller methods start()
, pause()
and reset()
. You can also add listeners to state changes or just use the state
property when you need it.
π Using StateBuilder and AnimationBuilder
CustomTimer(
controller: _controller,
begin: Duration(days: 1),
end: Duration(),
builder: (time) {
return Text(
"${time.hours}:${time.minutes}:${time.seconds}.${time.milliseconds}",
style: TextStyle(fontSize: 24.0)
);
},
stateBuilder: (time, state) {
// This builder is shown when the state is different from "couting".
if(state == CustomTimerState.paused) return Text(
"The timer is paused",
style: TextStyle(fontSize: 24.0)
);
// If null is returned, "builder" is displayed.
return null;
},
animationBuilder: (child) {
// You can define your own state change animations.
// Remember to return the child widget of the builder.
return AnimatedSwitcher(
duration: Duration(milliseconds: 250),
child: child,
);
},
onChangeState: (state){
// This callback function runs when the timer state changes.
print("Current state: $state");
}
)
π§ Installation
Add this to your package's pubspec.yaml file:
dependencies:
custom_timer: ^0.1.1
Install it:
$ flutter pub get
Import the package in your project:
import 'package:custom_timer/custom_timer.dart';
π Author
Hi there