Add Timer to Popup

main
Kai Mannweiler 2023-03-03 18:26:44 +01:00
parent 3ae9699137
commit 4279e7961c
3 changed files with 47 additions and 11 deletions

View File

@ -8,6 +8,7 @@ class TimerProvider extends ChangeNotifier {
int get elapsedSeconds => _timer != null ? _timer!.tick : 0;
void startTimer(Duration duration) {
print('started');
started = true;
_timer = Timer.periodic(const Duration(seconds: 1), ((timer) {
if (timer.tick >= duration.inSeconds) {
@ -26,7 +27,10 @@ class TimerProvider extends ChangeNotifier {
@override
void dispose() {
print('disposed');
started = false;
_timer?.cancel();
_timer = null;
super.dispose();
}
}

View File

@ -0,0 +1,41 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smoke_cess_app/providers/timer_provider.dart';
import 'package:smoke_cess_app/widgets/timer_widget.dart';
import '../services/date_service.dart';
import '../services/pages_service.dart';
void showTaskDonePopup(BuildContext context, Pages page) async {
Duration duration = await getTimeTill(page);
await showDialog(
context: context,
builder: (BuildContext context) {
return ChangeNotifierProvider(
create: (context) => TimerProvider(),
child: TaskDonePopup(
duration: duration,
),
);
},
);
}
class TaskDonePopup extends StatelessWidget {
final Duration duration;
const TaskDonePopup({super.key, required this.duration});
@override
Widget build(BuildContext context) {
TimerProvider timerProvider = context.read<TimerProvider>();
timerProvider.startTimer(duration);
return AlertDialog(
title: const Text('Schon gemacht'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Nächstes mal wieder:'),
TimerWidget(duration: duration)
]));
}
}

View File

@ -1,10 +1,10 @@
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smoke_cess_app/services/pages_service.dart';
import '../providers/input_provider.dart';
import '../providers/page_provider.dart';
import '../providers/tasks_provider.dart';
import 'popup_for_task_done.dart';
class ViewFormPage extends StatelessWidget {
final Widget form;
@ -13,15 +13,6 @@ class ViewFormPage extends StatelessWidget {
const ViewFormPage(
{super.key, required this.form, required this.view, required this.page});
void showPopup(BuildContext context) async {
AwesomeDialog(
context: context,
dialogType: DialogType.info,
title: 'Bald erst wieder',
desc: 'hier kommt ein timer hin',
).show();
}
@override
Widget build(BuildContext context) {
PageProvider pageProvider = context.watch<PageProvider>();
@ -35,7 +26,7 @@ class ViewFormPage extends StatelessWidget {
: const Icon(Icons.add_outlined, color: Colors.black),
onPressed: tasksProvider.tasks[page] ?? true
? pageProvider.swap
: () => showPopup(context),
: () => showTaskDonePopup(context, page),
),
),
pageProvider.showForm