notification_cleaner 0.0.3 copy "notification_cleaner: ^0.0.3" to clipboard
notification_cleaner: ^0.0.3 copied to clipboard

Updated fork of clear_all_notifications

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:notification_cleaner/notification_cleaner.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        useMaterial3: true,
        colorSchemeSeed: Colors.deepPurple,
      ),
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  bool _isClearing = false;

  Future<void> _handleClearNotifications() async {
    setState(() => _isClearing = true);

    try {
      // Handle Android 13+ notification permission
      if (await Permission.notification.isDenied) {
        await Permission.notification.request();
      }

      await NotificationCleaner.clearAllNotifications();
      
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Notifications cleared successfully!')),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Error: $e'), backgroundColor: Colors.red),
        );
      }
    } finally {
      if (mounted) {
        setState(() => _isClearing = false);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Notification Cleaner'),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(
              Icons.notifications_off_outlined,
              size: 100,
              color: Colors.deepPurple,
            ),
            const SizedBox(height: 24),
            const Padding(
              padding: EdgeInsets.symmetric(horizontal: 32),
              child: Text(
                'Press the button below to clear all active notifications from your device.',
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 16),
              ),
            ),
            const SizedBox(height: 48),
            _isClearing
                ? const CircularProgressIndicator()
                : ElevatedButton.icon(
                    onPressed: _handleClearNotifications,
                    icon: const Icon(Icons.delete_sweep),
                    label: const Text('Clear All Notifications'),
                    style: ElevatedButton.styleFrom(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 32,
                        vertical: 16,
                      ),
                    ),
                  ),
          ],
        ),
      ),
    );
  }
}