headset_connection_event 4.0.2 copy "headset_connection_event: ^4.0.2" to clipboard
headset_connection_event: ^4.0.2 copied to clipboard

Flutter Plugin for headset events. Detect headset is plugged and unplugged. Get current headset state.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:headset_connection_event/headset_event.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  final _headsetPlugin = HeadsetEvent();
  HeadsetState? _headsetState;

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

    ///Request Permissions (Required for Android 12)
    _headsetPlugin.requestPermission();

    /// if headset is plugged
    _headsetPlugin.getCurrentState.then((value) {
      setState(() {
        _headsetState = value;
      });
    });

    /// Detect the moment headset is plugged or unplugged
    _headsetPlugin.setListener((value) {
      setState(() {
        _headsetState = value;
      });
    });
  }

  @override
  Widget build(BuildContext context) => MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Headset Event Plugin'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Icon(
                  Icons.headset,
                  color: _headsetState == HeadsetState.CONNECT
                      ? Colors.green
                      : Colors.red,
                ),
                Text('State : $_headsetState\n'),
              ],
            ),
          ),
        ),
      );
}
28
likes
140
points
2.1k
downloads

Publisher

verified publisherthemobilecoder.com

Weekly Downloads

Flutter Plugin for headset events. Detect headset is plugged and unplugged. Get current headset state.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, permission_handler

More

Packages that depend on headset_connection_event

Packages that implement headset_connection_event