showAlarmPermissionDialog method

Future<bool> showAlarmPermissionDialog()

Shows a dialog specifically for alarm permissions on newer Android versions Returns true if the user chose to open settings, false otherwise

Implementation

Future<bool> showAlarmPermissionDialog() async {
  if (_context == null) return false;

  debugPrint('Showing alarm permission dialog');

  final bool? result = await showDialog<bool>(
    context: _context!,
    barrierDismissible: false, // User must tap a button to dismiss the dialog
    builder: (context) => AlertDialog(
      title: const Text('Alarm Permission Required'),
      content: const Text(
        'To schedule alarms, this app needs permission to schedule exact alarms.\n\n'
        'You will be redirected to system settings. Please tap "Allow" or "Allow precise alarms" on the next screen.',
      ),
      actions: [
        TextButton(
          child: const Text('Not Now'),
          onPressed: () => Navigator.pop(context, false),
        ),
        TextButton(
          style: TextButton.styleFrom(
            backgroundColor: Colors.blue,
            foregroundColor: Colors.white,
          ),
          child: const Text('Continue'),
          onPressed: () => Navigator.pop(context, true),
        ),
      ],
    ),
  );

  debugPrint('Alarm permission dialog result: $result');

  // If the user chose to open settings
  if (result == true) {
    try {
      debugPrint('Opening alarm settings');

      // Show a toast to guide the user
      if (_context != null) {
        ScaffoldMessenger.of(_context!).showSnackBar(
          const SnackBar(
            content: Text('Please tap "Allow" on the next screen'),
            duration: Duration(seconds: 3),
          ),
        );
      }

      return true;
    } catch (e) {
      debugPrint('Failed to show guidance for alarm settings: $e');
      return true;
    }
  }

  return false;
}