'Recalling duration of timer - Flutter
I am trying to recall and print an exact duration in time from my timer and it is just showing me 00:00:00 and never changing. It is trying to recall the code within my timer page once the sixedbox is pressed and is suppose to record the time and print it below my name of the task Can anyone help? Please let me know if you need any extra code. timer_page.dart
SizedBox(
height: 600,
child: GridView.count(
primary: false,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 3,
children: widget.tasks.map((element) {
final isActive = activeTask != null && activeTask == element;
return GestureDetector(
onTap: () {
// set active task or toggle is active
if (isActive) {
setState(() {
activeTask = null;
StudyViewModel.stopwatch.start();
disable = true;
});
} else {
setState(() {
activeTask = element;
StudyViewModel.stopwatch.stop();
disable = false;
});
}
},
child: Container(
color: isActive ? Colors.amber : Colors.green,
padding: EdgeInsets.all(8),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
element.name,
style: TextStyle(color: Colors.white, fontSize: 25),
textAlign: TextAlign.center,
),
Text(
element.elapsedTime
)
]
),
),
);
}).toList(),
),
),
studyviewmodel.dart
class StudyViewModel {
static List<Study> studies = [];
static List<ValueChanged<ElapsedTime>> timerListeners =
<ValueChanged<ElapsedTime>>[];
static Stopwatch stopwatch = new Stopwatch();
/// load from file...
static Future load() async {
try {
File file = await getFile();
String studiesJson = await file.readAsString();
if (studiesJson.isNotEmpty) {
List studiesParsed = json.decode(studiesJson);
studies = studiesParsed.map((i) => Study.fromJson(i)).toList();
}
} catch (e) {
print(e);
}
}
static Future<File> getFile() async {
final directory = await getApplicationDocumentsDirectory();
final path = directory.path;
return File('$path/studies.json');
}
static Future saveFile() async {
File file = await getFile();
file.writeAsString(json.encode(studies));
}
static bool checkName(String name) {
bool match = false;
for (int i = 0; i < studies.length; i++) {
if (studies[i].name == name) {
match = true;
break;
}
}
return match;
}
static String milliToElapsedString(int milliseconds) {
final int hundreds = (milliseconds / 10).truncate();
final int seconds = (hundreds / 100).truncate();
final int minutes = (seconds / 60).truncate();
String hundredsStr = (hundreds % 100).toString().padLeft(2, '0');
String minutesStr = (minutes % 60).toString().padLeft(2, '0');
String secondsStr = (seconds % 60).toString().padLeft(2, '0');
return minutesStr + ':' + secondsStr + ':' + hundredsStr;
}
}
task.dart (where elapsed time comes from)
class Task {
String name;
String elapsedTime;
Task({required this.name, required this.elapsedTime});
factory Task.fromJson(Map<String, dynamic> json) {
return Task(name: json['name'], elapsedTime: json['elapsedTime']);
}
Map<String, dynamic> toJson() => {'name': name, 'elapsedTime': elapsedTime};
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

