printer_service 2.0.3 printer_service: ^2.0.3 copied to clipboard
A flutter plugin that prints esc commands to printers in different platforms such as android, ios, windows and different interfaces Bluetooth and BLE, TCP and USB. This is a forked project of thermal_printer
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:printer_service/esc_pos_utils_platform/esc_pos_utils_platform.dart';
import 'package:printer_service/thermal_printer.dart';
import 'package:image/image.dart' as img;
import 'package:dart_ping_ios/dart_ping_ios.dart';
import 'image_utils.dart';
void main() {
// Register DartPingIOS
if (Platform.isIOS) {
DartPingIOS.register();
}
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// Printer Type [bluetooth, usb, network]
var defaultPrinterType = PrinterType.bluetooth;
var _isBle = false;
var _reconnect = false;
var _isConnected = false;
var printerManager = PrinterManager.instance;
var devices = <BluetoothPrinter>[];
StreamSubscription<PrinterDevice>? _subscription;
StreamSubscription<BTStatus>? _subscriptionBtStatus;
StreamSubscription<USBStatus>? _subscriptionUsbStatus;
StreamSubscription<TCPStatus>? _subscriptionTCPStatus;
BTStatus _currentStatus = BTStatus.none;
// ignore: unused_field
TCPStatus _currentTCPStatus = TCPStatus.none;
// _currentUsbStatus is only supports on Android
// ignore: unused_field
USBStatus _currentUsbStatus = USBStatus.none;
List<int>? pendingTask;
String _ipAddress = '';
String _port = '9100';
final _ipController = TextEditingController();
final _portController = TextEditingController();
BluetoothPrinter? selectedPrinter;
@override
void initState() {
if (Platform.isWindows) defaultPrinterType = PrinterType.usb;
super.initState();
_portController.text = _port;
_scan();
// subscription to listen change status of bluetooth connection
_subscriptionBtStatus = PrinterManager.instance.stateBluetooth.listen((status) {
log(' ----------------- status bt $status ------------------ ');
_currentStatus = status;
if (status == BTStatus.connected) {
setState(() {
_isConnected = true;
});
}
if (status == BTStatus.none) {
setState(() {
_isConnected = false;
});
}
if (status == BTStatus.connected && pendingTask != null) {
if (Platform.isAndroid) {
Future.delayed(const Duration(milliseconds: 1000), () {
PrinterManager.instance.send(type: PrinterType.bluetooth, bytes: pendingTask!);
pendingTask = null;
});
} else if (Platform.isIOS) {
PrinterManager.instance.send(type: PrinterType.bluetooth, bytes: pendingTask!);
pendingTask = null;
}
}
});
// PrinterManager.instance.stateUSB is only supports on Android
_subscriptionUsbStatus = PrinterManager.instance.stateUSB.listen((status) {
log(' ----------------- status usb $status ------------------ ');
_currentUsbStatus = status;
if (Platform.isAndroid) {
if (status == USBStatus.connected && pendingTask != null) {
Future.delayed(const Duration(milliseconds: 1000), () {
PrinterManager.instance.send(type: PrinterType.usb, bytes: pendingTask!);
pendingTask = null;
});
}
}
});
// PrinterManager.instance.stateUSB is only supports on Android
_subscriptionTCPStatus = PrinterManager.instance.stateTCP.listen((status) {
log(' ----------------- status tcp $status ------------------ ');
_currentTCPStatus = status;
});
}
@override
void dispose() {
_subscription?.cancel();
_subscriptionBtStatus?.cancel();
_subscriptionUsbStatus?.cancel();
_subscriptionTCPStatus?.cancel();
_portController.dispose();
_ipController.dispose();
super.dispose();
}
// method to scan devices according PrinterType
void _scan() {
devices.clear();
_subscription = printerManager.discovery(type: defaultPrinterType, isBle: _isBle).listen((device) {
devices.add(BluetoothPrinter(
deviceName: device.name,
address: device.address,
isBle: _isBle,
vendorId: device.vendorId,
productId: device.productId,
typePrinter: defaultPrinterType,
));
setState(() {});
});
}
void setPort(String value) {
if (value.isEmpty) value = '9100';
_port = value;
var device = BluetoothPrinter(
deviceName: value,
address: _ipAddress,
port: _port,
typePrinter: PrinterType.network,
state: false,
);
selectDevice(device);
}
void setIpAddress(String value) {
_ipAddress = value;
var device = BluetoothPrinter(
deviceName: value,
address: _ipAddress,
port: _port,
typePrinter: PrinterType.network,
state: false,
);
selectDevice(device);
}
void selectDevice(BluetoothPrinter device) async {
if (selectedPrinter != null) {
if ((device.address != selectedPrinter!.address) || (device.typePrinter == PrinterType.usb && selectedPrinter!.vendorId != device.vendorId)) {
await PrinterManager.instance.disconnect(type: selectedPrinter!.typePrinter);
}
}
selectedPrinter = device;
setState(() {});
}
Future _printReceiveTest() async {
List<int> bytes = [];
// Xprinter XP-N160I
final profile = await CapabilityProfile.load(name: 'XP-N160I');
// PaperSize.mm80 or PaperSize.mm58
final generator = Generator(PaperSize.mm58, profile);
bytes += generator.setGlobalCodeTable('CP1252');
bytes += generator.text('Test Print', styles: const PosStyles(align: PosAlign.left));
bytes += generator.text('Product 1');
bytes += generator.text('Product 2');
// bytes += generator.text('¥1,990', containsChinese: true, styles: const PosStyles(align: PosAlign.left));
// bytes += generator.emptyLines(1);
// sum width total column must be 12
bytes += generator.row([
PosColumn(width: 8, text: 'Lemon lime export quality per pound x 5 units', styles: const PosStyles(align: PosAlign.left, codeTable: 'CP1252')),
PosColumn(width: 4, text: 'USD 2.00', styles: const PosStyles(align: PosAlign.right, codeTable: 'CP1252')),
]);
final ByteData data = await rootBundle.load('assets/ic_launcher.png');
if (data.lengthInBytes > 0) {
final Uint8List imageBytes = data.buffer.asUint8List();
// decode the bytes into an image
final decodedImage = img.decodeImage(imageBytes)!;
// Create a black bottom layer
// Resize the image to a 130x? thumbnail (maintaining the aspect ratio).
img.Image thumbnail = img.copyResize(decodedImage, height: 130);
// creates a copy of the original image with set dimensions
img.Image originalImg = img.copyResize(decodedImage, width: 380, height: 130);
// fills the original image with a white background
img.fill(originalImg, color: img.ColorRgb8(255, 255, 255));
var padding = (originalImg.width - thumbnail.width) / 2;
//insert the image inside the frame and center it
drawImage(originalImg, thumbnail, dstX: padding.toInt());
// convert image to grayscale
var grayscaleImage = img.grayscale(originalImg);
bytes += generator.feed(1);
// bytes += generator.imageRaster(img.decodeImage(imageBytes)!, align: PosAlign.center);
bytes += generator.imageRaster(grayscaleImage, align: PosAlign.center);
bytes += generator.feed(1);
}
// // Chinese characters
bytes += generator.row([
PosColumn(width: 8, text: '豚肉・木耳と玉子炒め弁当', styles: const PosStyles(align: PosAlign.left), containsChinese: true),
PosColumn(width: 4, text: '¥1,990', styles: const PosStyles(align: PosAlign.right), containsChinese: true),
]);
_printEscPos(bytes, generator);
}
/// print ticket
void _printEscPos(List<int> bytes, Generator generator) async {
var connectedTCP = false;
if (selectedPrinter == null) return;
var bluetoothPrinter = selectedPrinter!;
switch (bluetoothPrinter.typePrinter) {
case PrinterType.usb:
bytes += generator.feed(2);
bytes += generator.cut();
await printerManager.connect(
type: bluetoothPrinter.typePrinter,
model: UsbPrinterInput(name: bluetoothPrinter.deviceName, productId: bluetoothPrinter.productId, vendorId: bluetoothPrinter.vendorId));
pendingTask = null;
break;
case PrinterType.bluetooth:
bytes += generator.cut();
await printerManager.connect(
type: bluetoothPrinter.typePrinter,
model: BluetoothPrinterInput(
name: bluetoothPrinter.deviceName,
address: bluetoothPrinter.address!,
isBle: bluetoothPrinter.isBle ?? false,
autoConnect: _reconnect));
pendingTask = null;
if (Platform.isAndroid) pendingTask = bytes;
break;
case PrinterType.network:
bytes += generator.feed(2);
bytes += generator.cut();
connectedTCP = await printerManager.connect(type: bluetoothPrinter.typePrinter, model: TcpPrinterInput(ipAddress: bluetoothPrinter.address!));
if (!connectedTCP) print(' --- please review your connection ---');
break;
default:
}
if (bluetoothPrinter.typePrinter == PrinterType.bluetooth && Platform.isAndroid) {
if (_currentStatus == BTStatus.connected) {
printerManager.send(type: bluetoothPrinter.typePrinter, bytes: bytes);
pendingTask = null;
}
} else {
printerManager.send(type: bluetoothPrinter.typePrinter, bytes: bytes);
}
}
// conectar dispositivo
_connectDevice() async {
_isConnected = false;
if (selectedPrinter == null) return;
switch (selectedPrinter!.typePrinter) {
case PrinterType.usb:
await printerManager.connect(
type: selectedPrinter!.typePrinter,
model: UsbPrinterInput(name: selectedPrinter!.deviceName, productId: selectedPrinter!.productId, vendorId: selectedPrinter!.vendorId));
_isConnected = true;
break;
case PrinterType.bluetooth:
await printerManager.connect(
type: selectedPrinter!.typePrinter,
model: BluetoothPrinterInput(
name: selectedPrinter!.deviceName,
address: selectedPrinter!.address!,
isBle: selectedPrinter!.isBle ?? false,
autoConnect: _reconnect));
break;
case PrinterType.network:
await printerManager.connect(type: selectedPrinter!.typePrinter, model: TcpPrinterInput(ipAddress: selectedPrinter!.address!));
_isConnected = true;
break;
default:
}
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Pos Plugin Platform example app'),
),
body: Center(
child: Container(
height: double.infinity,
constraints: const BoxConstraints(maxWidth: 400),
child: SingleChildScrollView(
padding: EdgeInsets.zero,
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: selectedPrinter == null || _isConnected
? null
: () {
_connectDevice();
},
child: const Text("Connect", textAlign: TextAlign.center),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton(
onPressed: selectedPrinter == null || !_isConnected
? null
: () {
if (selectedPrinter != null) printerManager.disconnect(type: selectedPrinter!.typePrinter);
setState(() {
_isConnected = false;
});
},
child: const Text("Disconnect", textAlign: TextAlign.center),
),
),
],
),
),
DropdownButtonFormField<PrinterType>(
value: defaultPrinterType,
decoration: const InputDecoration(
prefixIcon: Icon(
Icons.print,
size: 24,
),
labelText: "Type Printer Device",
labelStyle: TextStyle(fontSize: 18.0),
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
),
items: <DropdownMenuItem<PrinterType>>[
if (Platform.isAndroid || Platform.isIOS)
const DropdownMenuItem(
value: PrinterType.bluetooth,
child: Text("bluetooth"),
),
if (Platform.isAndroid || Platform.isWindows)
const DropdownMenuItem(
value: PrinterType.usb,
child: Text("usb"),
),
const DropdownMenuItem(
value: PrinterType.network,
child: Text("Wifi"),
),
],
onChanged: (PrinterType? value) {
setState(() {
if (value != null) {
setState(() {
defaultPrinterType = value;
selectedPrinter = null;
_isBle = false;
_isConnected = false;
_scan();
});
}
});
},
),
Visibility(
visible: defaultPrinterType == PrinterType.bluetooth && Platform.isAndroid,
child: SwitchListTile.adaptive(
contentPadding: const EdgeInsets.only(bottom: 20.0, left: 20),
title: const Text(
"This device supports ble (low energy)",
textAlign: TextAlign.start,
style: TextStyle(fontSize: 19.0),
),
value: _isBle,
onChanged: (bool? value) {
setState(() {
_isBle = value ?? false;
_isConnected = false;
selectedPrinter = null;
_scan();
});
},
),
),
Visibility(
visible: defaultPrinterType == PrinterType.bluetooth && Platform.isAndroid,
child: SwitchListTile.adaptive(
contentPadding: const EdgeInsets.only(bottom: 20.0, left: 20),
title: const Text(
"reconnect",
textAlign: TextAlign.start,
style: TextStyle(fontSize: 19.0),
),
value: _reconnect,
onChanged: (bool? value) {
setState(() {
_reconnect = value ?? false;
});
},
),
),
Column(
children: devices
.map(
(device) => ListTile(
title: Text('${device.deviceName}'),
subtitle: Platform.isAndroid && defaultPrinterType == PrinterType.usb
? null
: Visibility(visible: !Platform.isWindows, child: Text("${device.address}")),
onTap: () {
// do something
selectDevice(device);
},
leading: selectedPrinter != null &&
((device.typePrinter == PrinterType.usb && Platform.isWindows
? device.deviceName == selectedPrinter!.deviceName
: device.vendorId != null && selectedPrinter!.vendorId == device.vendorId) ||
(device.address != null && selectedPrinter!.address == device.address))
? const Icon(
Icons.check,
color: Colors.green,
)
: null,
trailing: OutlinedButton(
onPressed: selectedPrinter == null || device.deviceName != selectedPrinter?.deviceName
? null
: () async {
_printReceiveTest();
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 2, horizontal: 20),
child: Text("Print test ticket", textAlign: TextAlign.center),
),
),
),
)
.toList()),
Visibility(
visible: defaultPrinterType == PrinterType.network && Platform.isWindows,
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: _ipController,
keyboardType: const TextInputType.numberWithOptions(signed: true),
decoration: const InputDecoration(
label: Text("Ip Address"),
prefixIcon: Icon(Icons.wifi, size: 24),
),
onChanged: setIpAddress,
),
),
),
Visibility(
visible: defaultPrinterType == PrinterType.network && Platform.isWindows,
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: _portController,
keyboardType: const TextInputType.numberWithOptions(signed: true),
decoration: const InputDecoration(
label: Text("Port"),
prefixIcon: Icon(Icons.numbers_outlined, size: 24),
),
onChanged: setPort,
),
),
),
Visibility(
visible: defaultPrinterType == PrinterType.network && Platform.isWindows,
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: OutlinedButton(
onPressed: () async {
if (_ipController.text.isNotEmpty) setIpAddress(_ipController.text);
_printReceiveTest();
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 50),
child: Text("Print test ticket", textAlign: TextAlign.center),
),
),
),
)
],
),
),
),
),
),
);
}
}
class BluetoothPrinter {
int? id;
String? deviceName;
String? address;
String? port;
String? vendorId;
String? productId;
bool? isBle;
PrinterType typePrinter;
bool? state;
BluetoothPrinter(
{this.deviceName,
this.address,
this.port,
this.state,
this.vendorId,
this.productId,
this.typePrinter = PrinterType.bluetooth,
this.isBle = false});
}