Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions example/lib/countdown_timer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class _CountdownTimerPageState extends State<CountdownTimerPage> {
@override
void initState() {
super.initState();
controller =
CountdownTimerController(endTime: endTime, onEnd: onEnd);
controller = CountdownTimerController(
endTime: endTime, onEnd: onEnd, startTime: null);
}

void onEnd() {
Expand Down Expand Up @@ -92,7 +92,8 @@ class _CountdownTimerPageState extends State<CountdownTimerPage> {
AnimatedBuilder(
animation: time.milliseconds!,
builder: (context, child) {
return Text("${(time.milliseconds!.value * 1000).toInt()}");
return Text(
"${(time.milliseconds!.value * 1000).toInt()}");
},
)
],
Expand Down
65 changes: 36 additions & 29 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,56 @@ packages:
dependency: transitive
description:
name: async
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
flutter:
Expand All @@ -68,7 +68,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.1"
version: "4.1.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -78,21 +78,28 @@ packages:
dependency: transitive
description:
name: matcher
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.1.3"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
sky_engine:
Expand All @@ -104,57 +111,57 @@ packages:
dependency: transitive
description:
name: source_span
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.4.8"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.flutter-io.cn"
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
23 changes: 18 additions & 5 deletions lib/countdown_timer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import 'package:flutter_countdown_timer/index.dart';
///Countdown timer controller.
class CountdownTimerController extends ChangeNotifier {
CountdownTimerController(
{required int endTime, this.onEnd, TickerProvider? vsync})
: this._endTime = endTime {
{required int? endTime,
required int? startTime,
this.onEnd,
TickerProvider? vsync})
: this._endTime = endTime,
this._startTime = startTime {
if (vsync != null) {
this._animationController =
AnimationController(vsync: vsync, duration: Duration(seconds: 1));
Expand All @@ -18,7 +22,10 @@ class CountdownTimerController extends ChangeNotifier {
final VoidCallback? onEnd;

///The end time of the countdown.
int _endTime;
int? _endTime;

///The end time of the countdown.
int? _startTime;

///Is the countdown running.
bool _isRunning = false;
Expand Down Expand Up @@ -74,8 +81,14 @@ class CountdownTimerController extends ChangeNotifier {

///Calculate current remaining time.
CurrentRemainingTime? _calculateCurrentRemainingTime() {
int remainingTimeStamp =
(_endTime - DateTime.now().millisecondsSinceEpoch) ~/ 1000;
int remainingTimeStamp = 0;
if (_endTime != null)
remainingTimeStamp =
(_endTime! - DateTime.now().millisecondsSinceEpoch) ~/ 1000;
else
remainingTimeStamp =
(DateTime.now().millisecondsSinceEpoch - _startTime!) ~/ 1000;

if (remainingTimeStamp <= 0) {
return null;
}
Expand Down
34 changes: 21 additions & 13 deletions lib/flutter_countdown_timer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ class CountdownTimer extends StatefulWidget {
///The end time of the countdown.
final int? endTime;

CountdownTimer({
Key? key,
this.endWidget = const Center(
child: Text('The current time has expired'),
),
this.widgetBuilder,
this.controller,
this.textStyle,
this.endTime,
this.onEnd,
}) : assert(endTime != null || controller != null),
///The end time of the countdown.
final int? startTime;

CountdownTimer(
{Key? key,
this.endWidget = const Center(
child: Text('The current time has expired'),
),
this.widgetBuilder,
this.controller,
this.textStyle,
this.endTime,
this.onEnd,
this.startTime})
: assert(endTime != null || controller != null || startTime != null),
super(key: key);

@override
Expand Down Expand Up @@ -64,7 +68,10 @@ class _CountDownState extends State<CountdownTimer> {
///Generate countdown controller.
initController() {
controller = widget.controller ??
CountdownTimerController(endTime: widget.endTime!, onEnd: widget.onEnd);
CountdownTimerController(
endTime: widget.endTime,
onEnd: widget.onEnd,
startTime: widget.startTime);
if (controller.isRunning == false) {
controller.start();
}
Expand All @@ -79,7 +86,8 @@ class _CountDownState extends State<CountdownTimer> {
void didUpdateWidget(CountdownTimer oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.endTime != widget.endTime ||
widget.controller != oldWidget.controller) {
widget.controller != oldWidget.controller ||
widget.startTime == oldWidget.startTime) {
controller.dispose();
initController();
}
Expand Down
23 changes: 15 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,14 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -66,14 +66,21 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -127,7 +134,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.8"
typed_data:
dependency: transitive
description:
Expand All @@ -141,6 +148,6 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"