active_package 0.0.1 copy "active_package: ^0.0.1" to clipboard
active_package: ^0.0.1 copied to clipboard

PlatformAndroid

A Flutter plugin that helps you know the package name of any application running in foreground (Android only).

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:active_package/active_package.dart';

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

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

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

class _MyAppState extends State<MyApp> {
  String _packageName = 'Unknown';
  final _activePackagePlugin = ActivePackage();

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

  Future<void> getPackageName() async {
    String packageName;
    try {
      packageName = await _activePackagePlugin.getActivePackageName() ??
          'Unknown package name';
    } catch (e) {
      packageName = 'Failed to get the active package name.';
    }

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

    setState(() {
      _packageName = packageName;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              TextButton(
                  onPressed: () => getPackageName(),
                  child: const Text("Get package name")),
              const Text(
                'Running on:\n',
                textAlign: TextAlign.center,
              ),
              Text(
                _packageName,
                key: const Key("package_name_text_key"),
                textAlign: TextAlign.center,
              )
            ]),
      ),
    );
  }
}
1
likes
120
pub points
0%
popularity

Publisher

unverified uploader

A Flutter plugin that helps you know the package name of any application running in foreground (Android only).

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on active_package