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

PlatformAndroid

A Flutter plugin to print documents on Android devices via native print service.

example/lib/main.dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';

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

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

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Demo Forward Print Service'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late StreamSubscription _forwardPrintStreamSubs;
  String pathFile = '';

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _requestPermissionSystemAlert();
      Future.delayed(const Duration(milliseconds: 320), () {
        _onForwardPrintStreamSubs();
      });
    });
  }

  void _onForwardPrintStreamSubs() {
    ForwardPrinter.setPrinterName("My_Printer_Service");
    // For sharing images coming from outside the app while the app is in the memory
    _forwardPrintStreamSubs = ForwardPrinter.getDocumentStream().listen(
      _onForwardFile,
    );
    // For sharing images coming from outside the app while the app is closed
    ForwardPrinter.getInitialDocument().then(_onForwardFile);
  }

  void _onForwardFile(String? jsonStr) {
    if (jsonStr == null) return;

    final file = PrintFile.fromJson(jsonDecode(jsonStr));

    _onPreviewFiles([file.path]);
  }

  void _onPreviewFiles(List<String> paths) async {
    setState(() {
      pathFile = paths.first;
    });
  }

  Future<void> _requestPermissionSystemAlert() async {
    if (Platform.isAndroid && context.mounted) {
      final status = await Permission.systemAlertWindow.status;
      if (status != PermissionStatus.granted) {
        final allow = await showDialog<bool>(
          context: context,
          builder: (context) => AlertDialog(
            title: const Text("Permission Required"),
            content: const Text(
              "This app needs permission to run the Printer Service properly. Do you want to allow it?",
            ),
            actions: [
              TextButton(
                onPressed: () => Navigator.of(context).pop(false),
                child: const Text("Deny"),
              ),
              TextButton(
                onPressed: () => Navigator.of(context).pop(true),
                child: const Text("Allow"),
              ),
            ],
          ),
        );

        if (allow == true) {
          await Future.delayed(
            const Duration(milliseconds: 150),
            () => Permission.systemAlertWindow.request(),
          );
        }
      }
    }
  }

  @override
  void dispose() {
    _forwardPrintStreamSubs.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[const Text('file Name:'), Text(pathFile)],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
3
likes
150
points
32
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin to print documents on Android devices via native print service.

Repository (GitHub)
View/report issues

Documentation

API reference

License

unknown (license)

Dependencies

flutter, permission_handler, plugin_platform_interface

More

Packages that depend on android_printer_service

Packages that implement android_printer_service