flutter_native_badge 1.0.4 copy "flutter_native_badge: ^1.0.4" to clipboard
flutter_native_badge: ^1.0.4 copied to clipboard

Flutter plugin for setting the badge count on native platforms. Wraps the native APIs for iOS and macOS to set the badge count on the app icon.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_native_badge/flutter_native_badge.dart';

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isSupported = false;
  int _badgeCount = 0;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    bool isSupported;
    int badgeCount = 0;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      isSupported = await FlutterNativeBadge.isSupported();
      if (isSupported) {
        badgeCount = await FlutterNativeBadge.getBadgeCount();
      }
    } on PlatformException {
      isSupported = false;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _isSupported = isSupported;
      _badgeCount = badgeCount;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              const SizedBox(height: 20),
              if (_isSupported)
                const Text('Native badge is supported')
              else
                const Text('Native badge is not supported'),
              const SizedBox(height: 20),
              if (_isSupported) Text('Badge count: $_badgeCount'),
              const SizedBox(height: 20),
              if (_isSupported) ...[
                ElevatedButton(
                  onPressed: () async {
                    await FlutterNativeBadge.setBadgeCount(_badgeCount + 1);
                    _incrementCounter();
                  },
                  child: const Text('Increment'),
                ),
                const SizedBox(height: 20),
                ElevatedButton(
                  onPressed: () async {
                    await FlutterNativeBadge.clearBadgeCount();
                    _resetCounter();
                  },
                  child: const Text('Reset'),
                ),
              ],
            ],
          ),
        ),
      ),
    );
  }

  Future<void> _incrementCounter() async {
    setState(() {
      _badgeCount++;
    });
  }

  Future<void> _resetCounter() async {
    setState(() {
      _badgeCount = 0;
    });
  }
}
15
likes
0
pub points
89%
popularity

Publisher

unverified uploader

Flutter plugin for setting the badge count on native platforms. Wraps the native APIs for iOS and macOS to set the badge count on the app icon.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_native_badge_foundation, flutter_native_badge_platform_interface

More

Packages that depend on flutter_native_badge