Customized alarm using the system's local time in military hours

// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'dart:convert';
import 'dart:math' as math;
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '../../flutter_flow/lat_lng.dart';
import '../../flutter_flow/place.dart';
import '../../flutter_flow/uploaded_file.dart';
import 'dart:async';
// The following function is a custom action that creates a new alarm
// based on the provided hour and minute parameters. It checks the current time,
// and if the current hour and minute match the specified hour and minute,
// it returns a message indicating that the alarm is ringing. Otherwise, it waits
// for 1 second and calls itself recursively to keep checking the time until the alarm time is reached.
Future<String?> newCustomAction(int hour, int minutes) async {
  // Get the current time
  DateTime now = DateTime.now();
  int currentHour = now.hour;
  int currentMinute = now.minute;
  if (currentHour == hour && currentMinute == minutes) {
    return 'Alarm is ringing $hour:$minutes!'; // Replace this message with whatever you want to do when the alarm rings.
  } else {
    // Wait for 1 second before calling the newCustomAction recursively
    await Future.delayed(Duration(seconds: 1));
    return await newCustomAction(hour, minutes);
  }
}
void main() async {
  int alarmHour = 12;
  int alarmMinutes = 30;
  // Call the custom action function with the specified alarmHour and alarmMinutes.
  // The function will keep checking the time until the specified alarm time is reached.
  String? alarmStatus = await newCustomAction(alarmHour, alarmMinutes);
  print(alarmStatus); // Will print 'Alarm is ringing $hour:$minutes!' when the alarm time is reached.
}

When the alarm time is checked and it matches the specified time, the code will execute the action to send a notification and play a sound. The exact details of how the notification and sound are implemented are not provided in the given code snippet, but typically, a Flutter application can use plugins or platform-specific code to achieve this functionality.

4
4 replies