codenic_alarm 0.0.1-dev.3 copy "codenic_alarm: ^0.0.1-dev.3" to clipboard
codenic_alarm: ^0.0.1-dev.3 copied to clipboard

A Flutter plugin for scheduling alarms on Android and IOS.

example/lib/main.dart

import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:codenic_alarm/codenic_alarm.dart';
import 'package:flutter/material.dart';
import 'package:flutter_overlay_window/flutter_overlay_window.dart';
import 'package:flutter_ringtone_player/flutter_ringtone_player.dart';
import 'package:permission_handler/permission_handler.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await CodenicAlarm().initialize();
  await AndroidAlarmManager.initialize();

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Wrap(
            children: [
              FilledButton(
                child: const Text('Request Notification permission'),
                onPressed: () {
                  Permission.notification.request();
                },
              ),
              FilledButton(
                child: const Text('Show alarm'),
                onPressed: () {
                  CodenicAlarm().schedule(
                    delay: const Duration(seconds: 5),
                    alarmInfo: AlarmInfo(
                      id: 1,
                      title: 'Survey Reminder',
                      content: 'Report your health status',
                    ),
                  );
                },
              ),
              FilledButton(
                child: const Text('Has active alarm'),
                onPressed: () async {
                  final alarm = await CodenicAlarm().activeAlarm();

                  if (alarm == null) {
                    print('No active alarm');
                  } else {
                    print('Active alarm: ${alarm.title} ${alarm.content}');
                  }
                },
              ),
              FilledButton(
                child: const Text('Hide alarm'),
                onPressed: () {
                  CodenicAlarm().dismissAlarm();
                },
              ),
              FilledButton(
                child: const Text('Cancel all alarm'),
                onPressed: () {
                  CodenicAlarm().cancelAll();
                },
              ),
              FilledButton(
                child: const Text('Show alarm (Flutter overlay window)'),
                onPressed: () {
                  AndroidAlarmManager.oneShot(
                    const Duration(seconds: 0),
                    1,
                    a,
                  );
                },
              ),
              FilledButton(
                child: const Text('Play Ringtone'),
                onPressed: () {
                  FlutterRingtonePlayer.playRingtone();
                },
              ),
              FilledButton(
                child: const Text('Stop Ringtone'),
                onPressed: () {
                  FlutterRingtonePlayer.stop();
                },
              ),
            ],
          ),
        ),
      ),
      builder: (context, child) {
        return CodenicAlarmOverlayHandler(
          alarmOverlayBuilder: (context, alarmInfo) {
            return Material(
              color: Colors.black,
              child: Center(
                child: FilledButton(
                  onPressed: () {
                    CodenicAlarm().dismissAlarm();
                  },
                  child: Text('Close'),
                ),
              ),
            );
          },
          child: child!,
        );
      },
    );
  }
}

@pragma("vm:entry-point")
void a() {
  FlutterOverlayWindow.showOverlay();
}

// overlay entry point
@pragma("vm:entry-point")
void overlayMain() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Material(
        child: Center(
          child: FilledButton(
            onPressed: () {
              FlutterOverlayWindow.closeOverlay();
            },
            child: Text('Close'),
          ),
        ),
      ),
    ),
  );
}

@pragma('vm:entry-point')
Future<void> androidOverlayAlarmMain() async {
  WidgetsFlutterBinding.ensureInitialized();

  await CodenicAlarm().initialize();
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Material(
        child: Center(
          child: FilledButton(
            onPressed: () => CodenicAlarm().dismissAlarm(),
            child: const Text('Dismiss alarm'),
          ),
        ),
      ),
    ),
  );
}