I need to get value of my widget countdown to each second for control if the time it's not finish. I don't find how I can do... How i can get the value of the widget? How i can get the value to each second? For the integration in other widget, I think i can do it.
My countdown widget:
class _CountdownState extends State<Countdown> {
Countdown c = new Countdown();
Timer _timer;
int seconds = 0;
@override
Widget build(BuildContext context) {
return Center(
child: Text(constructTime(seconds), style: TextStyle(fontSize: 18, color: Colors.white, fontStyle: FontStyle.italic),),
);
}
String constructTime(int seconds) {
if(seconds == 0){
return "";
}
int hour = seconds ~/ 3600;
int minute = seconds % 3600 ~/ 60;
int second = seconds % 60;
return formatTime(hour) + ":" + formatTime(minute) + ":" + formatTime(second);
}
String formatTime(int timeNum) {
return timeNum < 10 ? "0" + timeNum.toString() : timeNum.toString();
}
@override
initState(){
super.initState();
getDate();
startTimer();
}
void getDate() async{
var now = DateTime.now();
/*String creationDate = await Poll().getCreationDateToSF();
DateTime creationDateTime = DateTime.parse(creationDate);*/
var creationDateTime = DateTime.now().add(new Duration(seconds: 15));
seconds = creationDateTime.difference(now).inSeconds;
}
void startTimer() {
const period = const Duration(seconds: 1);
_timer = Timer.periodic(period, (timer) {
setState(() {
seconds--;
});
if (seconds == 0) {
cancelTimer();
}
});
}
void cancelTimer() {
if (_timer != null) {
_timer.cancel();
_timer = null;
c.onCountDownComplete();
}
}
@override
void dispose() {
super.dispose();
cancelTimer();
}
}
I added the method for print the message in other page and i call the Countdown(onCountDownComplete: onCountDown) in my Poll State Class
I want to control in other page, when the result of my countdown is finish, i stop my app.
EDITED:
E/flutter ( 5306): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'call' was called on null.
E/flutter ( 5306): Receiver: null
E/flutter ( 5306): Tried calling: call()
E/flutter ( 5306): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter ( 5306): #1 _CountdownState.cancelTimer
package:blackbox/…/widgets/countDown.dart:75
E/flutter ( 5306): #2 _CountdownState.startTimer.<anonymous closure>
package:blackbox/…/widgets/countDown.dart:66
E/flutter ( 5306): #3 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 5306): #4 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 5306): #5 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter ( 5306): #6 _CustomZone.bindUnaryCallbackGuarded.<anonymous closure> (dart:async/zone.dart:968:26)
E/flutter ( 5306): #7 _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter ( 5306): #8 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 5306): #9 _CustomZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:952:26)
E/flutter ( 5306): #10 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
E/flutter ( 5306): #11 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
E/flutter ( 5306): #12 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
try this code:
Change your Countdown class like this:
class Countdown extends StatefulWidget {
final Function onCountDownComplete;
Countdown({this.onCountDownComplete});
@override
_CountdownState createState() => _CountdownState();
}
class _CountdownState extends State<Countdown> {
Timer _timer;
int seconds = 0;
@override
Widget build(BuildContext context) {
return Center(
child: Text(
constructTime(seconds),
style: TextStyle(
fontSize: 18, color: Colors.white, fontStyle: FontStyle.italic),
),
);
}
String constructTime(int seconds) {
int hour = seconds ~/ 3600;
int minute = seconds % 3600 ~/ 60;
int second = seconds % 60;
return formatTime(hour) +
":" +
formatTime(minute) +
":" +
formatTime(second);
}
String formatTime(int timeNum) {
return timeNum < 10 ? "0" + timeNum.toString() : timeNum.toString();
}
@override
initState() {
getDate();
//startTimer();
super.initState();
}
void getDate() async {
var now = DateTime.now();
String creationDate = '12:20:11';
DateTime creationDateTime = DateTime.parse(creationDate);
seconds = creationDateTime.difference(now).inSeconds;
startTimer();
}
void startTimer() {
const period = const Duration(seconds: 1);
_timer = Timer.periodic(period, (timer) {
setState(() {
seconds--;
});
if (seconds == 0) {
cancelTimer();
}
});
}
void cancelTimer() {
if (_timer != null) {
_timer.cancel();
_timer = null;
widget.onCountDownComplete();
}
}
@override
void dispose() {
super.dispose();
cancelTimer();
}
}
For use Countdown:
void onCountDown() {
print('Complete Count down');
}
Countdown(onCountDownComplete: onCountDown)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.