flutter_alarm Examples
flutter_alarm Examples.
Flutter Alarm - How To Examples
Introduction
Flutter Alarm is a package that allows you to implement alarm functionality in your Flutter applications. It provides various features such as setting alarms, scheduling repeated alarms, and handling alarm callbacks.
In this tutorial, we will cover the installation process and provide 10 detailed examples of how to use the Flutter Alarm package in your Flutter applications.
Installation
To install the Flutter Alarm package, add the following dependency to your pubspec.yaml
file:
dependencies:
flutter_alarm: ^1.0.0
Then, run the following command to fetch the package:
flutter pub get
Now, you are ready to use the Flutter Alarm package in your Flutter application.
Example 1: Setting a One-Time Alarm
To set a one-time alarm, you can use the setAlarm
method provided by the Flutter Alarm package. Here's an example of how to set a one-time alarm:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() {
FlutterAlarm.setAlarm(
DateTime.now().add(Duration(seconds: 10)),
"Wake up!",
"It's time to start your day.",
);
}
In this example, we are setting an alarm that will trigger after 10 seconds from the current time. The alarm will display a notification with the title "Wake up!" and the message "It's time to start your day."
Example 2: Setting a Repeated Alarm
To set a repeated alarm, you can use the setRepeatingAlarm
method provided by the Flutter Alarm package. Here's an example of how to set a repeated alarm:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() {
FlutterAlarm.setRepeatingAlarm(
DateTime.now().add(Duration(seconds: 10)),
Duration(minutes: 30),
"Take a break!",
"It's time to relax for a while.",
);
}
In this example, we are setting a repeated alarm that will trigger after 10 seconds from the current time and repeat every 30 minutes. The alarm will display a notification with the title "Take a break!" and the message "It's time to relax for a while."
Example 3: Cancelling an Alarm
To cancel an alarm, you can use the cancelAlarm
method provided by the Flutter Alarm package. Here's an example of how to cancel an alarm:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() {
FlutterAlarm.cancelAlarm();
}
In this example, we are cancelling any active alarms. This will prevent any scheduled alarms from triggering.
Example 4: Handling Alarm Callbacks
To handle alarm callbacks, you can use the onAlarm
event provided by the Flutter Alarm package. Here's an example of how to handle alarm callbacks:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() {
FlutterAlarm.onAlarm().listen((alarm) {
print("Alarm triggered: ${alarm.title}");
});
}
In this example, we are listening to the onAlarm
event and printing the title of the triggered alarm.
Example 5: Getting the List of Scheduled Alarms
To get the list of scheduled alarms, you can use the getScheduledAlarms
method provided by the Flutter Alarm package. Here's an example of how to get the list of scheduled alarms:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() async {
List<Alarm> alarms = await FlutterAlarm.getScheduledAlarms();
for (var alarm in alarms) {
print("Scheduled Alarm: ${alarm.title}");
}
}
In this example, we are getting the list of scheduled alarms and printing the title of each alarm.
Example 6: Checking if an Alarm is Scheduled
To check if an alarm is scheduled, you can use the isAlarmScheduled
method provided by the Flutter Alarm package. Here's an example of how to check if an alarm is scheduled:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() async {
bool isScheduled = await FlutterAlarm.isAlarmScheduled();
if (isScheduled) {
print("An alarm is scheduled.");
} else {
print("No alarm is scheduled.");
}
}
In this example, we are checking if an alarm is scheduled and printing the corresponding message.
Example 7: Customizing Alarm Notifications
To customize alarm notifications, you can use the setNotification
method provided by the Flutter Alarm package. Here's an example of how to customize alarm notifications:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() {
FlutterAlarm.setNotification(
"com.example.app",
"alarm_icon",
"Alarm Channel",
"Alarm notifications",
true,
);
}
In this example, we are customizing the alarm notification by specifying the Android package name, the icon name, the notification channel name, the notification channel description, and whether to enable vibration.
Example 8: Handling Alarm Dismissals
To handle alarm dismissals, you can use the onDismiss
event provided by the Flutter Alarm package. Here's an example of how to handle alarm dismissals:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() {
FlutterAlarm.onDismiss().listen((alarm) {
print("Alarm dismissed: ${alarm.title}");
});
}
In this example, we are listening to the onDismiss
event and printing the title of the dismissed alarm.
Example 9: Customizing Alarm Sound
To customize the alarm sound, you can use the setSound
method provided by the Flutter Alarm package. Here's an example of how to customize the alarm sound:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() {
FlutterAlarm.setSound("alarm_sound.mp3");
}
In this example, we are setting the alarm sound to a file named "alarm_sound.mp3".
Example 10: Handling Alarm Snooze
To handle alarm snooze, you can use the onSnooze
event provided by the Flutter Alarm package. Here's an example of how to handle alarm snooze:
import 'package:flutter_alarm/flutter_alarm.dart';
void main() {
FlutterAlarm.onSnooze().listen((alarm) {
print("Alarm snoozed: ${alarm.title}");
});
}
In this example, we are listening to the onSnooze
event and printing the title of the snoozed alarm.
Conclusion
In this tutorial, we covered the installation process of the Flutter Alarm package and provided 10 detailed examples of how to use it in your Flutter applications. You can now easily implement alarm functionality, schedule repeated alarms, handle alarm callbacks, and customize alarm notifications, sound, dismissals, and snooze.