flutter_alarm_clock Examples
flutter_alarm_clock Examples.
Flutter Alarm Clock - How To Examples
Introduction
Flutter Alarm Clock is a Flutter package that allows you to create alarm clock functionality in your Flutter applications. It provides various features such as setting alarms, managing alarms, and handling alarm notifications. In this tutorial, we will explore different examples to understand how to use the Flutter Alarm Clock package effectively.
Installation
To use the Flutter Alarm Clock package, follow these steps:
- Open your Flutter project in your preferred code editor.
- Open the
pubspec.yaml
file. - Add the following line under the
dependencies
section:
dependencies:
flutter_alarm_clock: ^1.0.0
- Save the
pubspec.yaml
file. - Run the following command in your terminal to fetch the package:
flutter pub get
- Import the Flutter Alarm Clock package in your Dart file:
import 'package:flutter_alarm_clock/flutter_alarm_clock.dart';
Now you are ready to use the Flutter Alarm Clock package in your Flutter application.
Examples
Example 1: Setting a Basic Alarm
In this example, we will set a basic alarm with a specific time and a custom message.
void setAlarm() {
FlutterAlarmClock.instance.setAlarm(
DateTime.now().add(Duration(minutes: 5)), // Set alarm after 5 minutes
message: "Wake up!", // Custom alarm message
);
}
- The alarm will be set to trigger after 5 minutes from the current time.
- A notification with the message "Wake up!" will be displayed when the alarm triggers.
Example 2: Setting a Recurring Alarm
In this example, we will set a recurring alarm that triggers every day at a specific time.
void setRecurringAlarm() {
FlutterAlarmClock.instance.setRecurringAlarm(
Time(8, 0, 0), // Set alarm time to 8:00 AM
recurrence: Recurrence.daily, // Set alarm to trigger every day
message: "Good morning!", // Custom alarm message
);
}
- The alarm will be set to trigger every day at 8:00 AM.
- A notification with the message "Good morning!" will be displayed when the alarm triggers.
Example 3: Cancelling an Alarm
In this example, we will cancel a previously set alarm.
void cancelAlarm() {
FlutterAlarmClock.instance.cancelAlarm();
}
- The previously set alarm, if any, will be cancelled.
Example 4: Handling Alarm Notifications
In this example, we will handle alarm notifications by defining a callback function.
void handleAlarmNotification() {
FlutterAlarmClock.instance.onAlarmTriggered.listen((alarmInfo) {
// Handle alarm notification here
print("Alarm triggered: ${alarmInfo.message}");
});
}
- When an alarm triggers, the callback function will be called.
- The alarm message will be printed to the console.
Example 5: Snoozing an Alarm
In this example, we will snooze an alarm for a specific duration.
void snoozeAlarm() {
FlutterAlarmClock.instance.snoozeAlarm(Duration(minutes: 10)); // Snooze alarm for 10 minutes
}
- The currently triggered alarm will be snoozed for 10 minutes.
Example 6: Getting Alarm Status
In this example, we will check the status of the alarm.
void checkAlarmStatus() async {
bool isAlarmSet = await FlutterAlarmClock.instance.isAlarmSet();
print("Alarm is set: $isAlarmSet");
}
- The status of the alarm (whether it is set or not) will be printed to the console.
Example 7: Handling Alarm Dismissal
In this example, we will handle the dismissal of an alarm.
void handleAlarmDismissal() {
FlutterAlarmClock.instance.onAlarmDismissed.listen((alarmInfo) {
// Handle alarm dismissal here
print("Alarm dismissed: ${alarmInfo.message}");
});
}
- When an alarm is dismissed, the callback function will be called.
- The alarm message will be printed to the console.
Example 8: Customizing Alarm Notifications
In this example, we will customize the alarm notification by providing additional options.
void customizeAlarmNotification() {
FlutterAlarmClock.instance.setAlarm(
DateTime.now().add(Duration(minutes: 5)),
message: "Wake up!",
sound: "custom_sound.mp3", // Custom sound file for the alarm
channelID: "alarm_channel", // Custom channel ID for the alarm notification
);
}
- The alarm will be set with a custom sound and a custom channel ID for the notification.
Example 9: Handling Alarm Action
In this example, we will handle the action performed on an alarm notification.
void handleAlarmAction() {
FlutterAlarmClock.instance.onAlarmAction.listen((alarmInfo) {
// Handle alarm action here
print("Alarm action performed: ${alarmInfo.action}");
});
}
- When an action is performed on an alarm notification, the callback function will be called.
- The action performed will be printed to the console.
Example 10: Getting Alarm Information
In this example, we will get information about the currently triggered alarm.
void getAlarmInfo() async {
AlarmInfo alarmInfo = await FlutterAlarmClock.instance.getAlarmInfo();
print("Alarm message: ${alarmInfo.message}");
print("Alarm time: ${alarmInfo.time}");
}
- The alarm message and time of the currently triggered alarm will be printed to the console.
These examples cover various scenarios and functionalities provided by the Flutter Alarm Clock package. You can use these examples as a reference to implement alarm clock functionality in your Flutter applications.