flutter_thermal_printer 0.0.2 copy "flutter_thermal_printer: ^0.0.2" to clipboard
flutter_thermal_printer: ^0.0.2 copied to clipboard

Plugin for Flutter to print on thermal printers via ESC/POS commands.

example/lib/main.dart

import 'dart:async';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_thermal_printer/flutter_thermal_printer.dart';
import 'package:flutter_thermal_printer/utils/printer.dart';

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

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

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

class _MyAppState extends State<MyApp> {
  final _flutterThermalPrinterPlugin = FlutterThermalPrinter.instance;

  List<Printer> bleDevices = [];

  StreamSubscription<List<Printer>>? _devicesStreamSubscription;

  //  Start scanning for BLE devices
  Future<void> startScan() async {
    try {
      await _flutterThermalPrinterPlugin.startScan();
      _devicesStreamSubscription =
          _flutterThermalPrinterPlugin.devicesStream.listen((event) {
        setState(() {
          bleDevices = event.map((e) => Printer.fromJson(e.toJson())).toList();
          bleDevices.removeWhere(
            (element) => element.name == null || element.name!.isEmpty,
          );
        });
      });
    } catch (e) {
      log('Failed to start scanning for devices $e');
    }
  }

  // Stop scanning for BLE devices
  Future<void> stopScan() async {
    try {
      _devicesStreamSubscription?.cancel();
      await _flutterThermalPrinterPlugin.stopScan();
    } catch (e) {
      log('Failed to stop scanning for devices $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
          systemOverlayStyle: const SystemUiOverlayStyle(
            statusBarColor: Colors.transparent,
          ),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {
                if (bleDevices.isNotEmpty) {
                  stopScan();
                }
                startScan();
              },
              child: const Text('Get Others Ble Devices List'),
            ),
            Expanded(
              child: ListView.builder(
                itemCount: bleDevices.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    onTap: () async {
                      final isConnected = await _flutterThermalPrinterPlugin
                          .connect(bleDevices[index]);
                      log("Devices: $isConnected");
                    },
                    title: Text(bleDevices[index].name ?? 'No Name'),
                    subtitle: Text(
                        "VendorId: ${bleDevices[index].address} - Connected: ${bleDevices[index].isConnected}"),
                    trailing: IconButton(
                      icon: const Icon(Icons.connect_without_contact),
                      onPressed: () async {
                        final profile = await CapabilityProfile.load();
                        final generator = Generator(PaperSize.mm80, profile);
                        List<int> bytes = [];
                        bytes += generator.text('Hello World');
                        bytes +=
                            generator.text("|||| FLUTTER THERMAL PRINTER ||||");
                        bytes += generator.feed(2);
                        bytes += generator.cut();
                        await _flutterThermalPrinterPlugin.printData(
                          bleDevices[index],
                          bytes,
                          longData: true,
                        );
                      },
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
22
likes
0
pub points
90%
popularity

Publisher

verified publishersunilflutter.in

Plugin for Flutter to print on thermal printers via ESC/POS commands.

Homepage

License

unknown (license)

Dependencies

esc_pos_utils, flutter, flutter_blue_plus, plugin_platform_interface, win_ble

More

Packages that depend on flutter_thermal_printer