pickPrinter static method

Future<Printer?> pickPrinter({
  1. required BuildContext context,
  2. Rect? bounds,
  3. String? title,
})

Opens the native printer picker interface, and returns the URL of the selected printer.

This is not supported on all platforms. Check the result of info to find at runtime if this feature is available or not.

Implementation

static Future<Printer?> pickPrinter({
  required BuildContext context,
  Rect? bounds,
  String? title,
}) async {
  final _info = await info();

  if (_info.canListPrinters) {
    final printers = await listPrinters();
    printers.sort((a, b) {
      if (a.isDefault) {
        return -1;
      }
      if (b.isDefault) {
        return 1;
      }
      return a.name.compareTo(b.name);
    });

    return await showDialog<Printer>(
      context: context,
      builder: (context) => SimpleDialog(
        title: Text(title ?? 'Select Printer'),
        children: [
          for (final printer in printers)
            if (printer.isAvailable)
              SimpleDialogOption(
                onPressed: () => Navigator.of(context).pop(printer),
                child: Text(
                  printer.name,
                  style: TextStyle(
                    fontStyle: printer.isDefault
                        ? FontStyle.italic
                        : FontStyle.normal,
                  ),
                ),
              ),
        ],
      ),
    );
  }

  bounds ??= Rect.fromCircle(center: Offset.zero, radius: 10);

  return await PrintingPlatform.instance.pickPrinter(bounds);
}