I'm making timer application. I want to show the time in notification so I'm using flutter_foreground_task (https://pub.dev/packages/flutter_foreground_task)
I want to stop or reset the time when I push the button on the notification. How can I do that?
this is my whole code.
import 'dart:async';
import 'dart:isolate';
import 'dart:js';
import 'package:flutter/material.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
void _initForegroundTask() {
FlutterForegroundTask.init(
androidNotificationOptions: AndroidNotificationOptions(
channelId: 'foreground_service',
channelName: 'Foreground Service Notification',
channelDescription:
'This notification appears when the foreground service is running.',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
iconData: const NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: 'launcher',
),
buttons: [
const NotificationButton(id: 'stop', text: 'STOP'),
const NotificationButton(id: 'reset', text: 'RESET'),
],
),
iosNotificationOptions: const IOSNotificationOptions(
showNotification: true,
playSound: false,
),
foregroundTaskOptions: const ForegroundTaskOptions(
interval: 5000,
isOnceEvent: false,
autoRunOnBoot: true,
allowWakeLock: true,
allowWifiLock: true,
),
);
noticeStart(format(totalSeconds));
}
@override
void initState() {
super.initState();
_initForegroundTask();
}
static const tenMinutes = 600;
int totalSeconds = tenMinutes;
bool isRunning = false;
late Timer timer;
void onTick(Timer timer) {
if (totalSeconds == 0) {
setState(() {
isRunning = false;
totalSeconds = totalSeconds;
});
timer.cancel();
} else {
setState(() {
totalSeconds = totalSeconds - 1;
});
FlutterForegroundTask.updateService(
notificationText: format(totalSeconds));
}
}
void onStartPressed() {
noticeStart(format(totalSeconds));
timer = Timer.periodic(
const Duration(seconds: 1),
onTick,
);
setState(() {
isRunning = true;
});
}
void noticeStart(String initialText) async {
await FlutterForegroundTask.startService(
notificationTitle: 'Red Timer',
notificationText: initialText,
callback: startCallback,
);
setState(() {});
}
void onPausePreesed() {
timer.cancel();
setState(() {
isRunning = false;
});
}
void onReset() {
noticeStop();
setState(() {
isRunning = false;
totalSeconds = tenMinutes;
});
timer.cancel();
}
void noticeStop() async {
await FlutterForegroundTask.clearAllData();
await FlutterForegroundTask.stopService();
}
String format(int seconds) {
var duration = Duration(seconds: seconds);
return duration.toString().split(".").first.substring(2, 7);
}
@override
Widget build(BuildContext context) {
return WithForegroundTask(
child: Scaffold(
backgroundColor: Theme.of(context).colorScheme.background,
body: Column(
children: [
Flexible(
flex: 3,
child: Container(
width: double.infinity,
height: double.infinity,
alignment: Alignment.bottomCenter,
decoration: const BoxDecoration(),
child: Text(
format(totalSeconds),
style: TextStyle(
color: Theme.of(context).textTheme.displayLarge!.color,
fontSize: 68,
fontWeight: FontWeight.w600,
),
),
),
),
const SizedBox(
height: 15,
),
Flexible(
flex: 3,
child: Container(
decoration: const BoxDecoration(),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: isRunning ? onPausePreesed : onStartPressed,
icon: Icon(
isRunning ? Icons.pause : Icons.play_arrow,
),
iconSize: 60,
color: Theme.of(context).textTheme.displayLarge!.color,
),
IconButton(
onPressed: onReset,
icon: const Icon(
Icons.square,
),
iconSize: 39,
color: Theme.of(context).textTheme.displayLarge!.color,
),
],
),
),
),
],
),
),
);
}
}
@pragma('vm:entry-point')
void startCallback() {
// The setTaskHandler function must be called to handle the task in the background.
FlutterForegroundTask.setTaskHandler(FirstTaskHandler());
}
class FirstTaskHandler extends TaskHandler {
SendPort? _sendPort;
@override
void onStart(DateTime timestamp, SendPort? sendPort) async {
_sendPort = sendPort;
final customData =
await FlutterForegroundTask.getData<String>(key: 'customData');
print('customData: $customData');
}
@override
void onRepeatEvent(DateTime timestamp, SendPort? sendPort) async {
sendPort?.send(timestamp);
}
@override
void onDestroy(DateTime timestamp, SendPort? sendPort) async {}
@override
void onNotificationButtonPressed(String id) {
print('onNotificationButtonPressed >> $id');
if (id == 'stop') {
onPausePreesed();
} else if (id == 'reset') {
onReset();
}
}
@override
void onNotificationPressed() {
// FlutterForegroundTask.launchApp("/resume-route");
_sendPort?.send('onNotificationPressed');
}
}
I wrote code like this:
@override
void onNotificationButtonPressed(String id) {
print('onNotificationButtonPressed >> $id');
if (id == 'stop') {
onPausePreesed();
} else if (id == 'reset') {
onReset();
}
}
But I got the error like this:
The method 'onPausePreesed' isn't defined for the type 'FirstTaskHandler'. Try correcting the name to the name of an existing method, or defining a method named 'onPausePreesed'.