Flutter热敏打印机图像打印插件flutter_pos_printer_platform_image_3的使用
Flutter热敏打印机图像打印插件flutter_pos_printer_platform_image_3的使用
简介
flutter_pos_printer_platform_image_3
是一个基于 flutter_pos_printer_platform
的分支,旨在解决在Flutter项目中同时使用 image v4
和 image v3
库的问题。自原库停止维护以来,该库进行了许多错误修复,并支持Android、iOS和Windows平台。
主要功能
- 支持Android、iOS和Windows
- 扫描蓝牙设备
- 发送原始字节数据到设备,可以使用
flutter_esc_pos_utils
生成ESC/POS命令
特性表
功能 | Android | iOS | Windows | 描述 |
---|---|---|---|---|
USB接口 | ✅ | ▢ | ✅ | 允许连接USB设备 |
经典蓝牙接口 | ✅ | ▢ | ▢ | 允许连接经典蓝牙设备 |
蓝牙低能耗(BLE)接口 | ✅ | ✅ | ▢ | 允许连接BLE设备 |
网络(以太网/WiFi)接口 | ✅ | ✅ | ✅ | 允许连接网络设备 |
扫描 | ✅ | ✅ | ✅ | 开始扫描蓝牙或网络设备(仅限Android/iOS) |
连接 | ✅ | ✅ | ✅ | 建立与设备的连接 |
断开连接 | ✅ | ✅ | ✅ | 取消活动或挂起的连接 |
状态监听 | ✅ | ✅ | ✅ | 设备状态变化的流 |
打印 | ✅ | ✅ | ✅ | 打印字节数据 |
使用方法
初始化PrinterManager实例
import 'package:flutter_pos_printer_platform/flutter_pos_printer_platform.dart';
var printerManager = PrinterManager.instance;
扫描设备
void _scan(PrinterType type, {bool isBle = false}) {
var devices = [];
PrinterManager.instance.discovery(type: type, isBle: isBle).listen((device) {
devices.add(device);
});
}
连接设备
Future<void> _connectDevice(PrinterDevice selectedPrinter, PrinterType type, {bool reconnect = false, bool isBle = false, String? ipAddress = null}) async {
switch (type) {
case PrinterType.usb:
await PrinterManager.instance.connect(
type: type,
model: UsbPrinterInput(name: selectedPrinter.name, productId: selectedPrinter.productId, vendorId: selectedPrinter.vendorId));
break;
case PrinterType.bluetooth:
await PrinterManager.instance.connect(
type: type,
model: BluetoothPrinterInput(
name: selectedPrinter.name,
address: selectedPrinter.address!,
isBle: isBle,
autoConnect: reconnect));
break;
case PrinterType.network:
await PrinterManager.instance.connect(type: type, model: TcpPrinterInput(ipAddress: ipAddress ?? selectedPrinter.address!));
break;
default:
}
}
断开连接
Future<void> _disconnectDevice(PrinterType type) async {
await PrinterManager.instance.disconnect(type: type);
}
监听蓝牙状态
PrinterManager.instance.stateBluetooth.listen((status) {
log(' ----------------- status bt $status ------------------ ');
});
发送字节数据进行打印
Future<void> _sendBytesToPrint(List<int> bytes, PrinterType type) async {
PrinterManager.instance.send(type: type, bytes: bytes);
}
示例代码
以下是一个完整的示例应用程序,展示了如何使用 flutter_pos_printer_platform_image_3
进行图像打印:
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:esc_pos_utils/esc_pos_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pos_printer_platform_image_3/flutter_pos_printer_platform_image_3.dart';
void main() {
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;
BTStatus _currentStatus = BTStatus.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;
});
}
}
});
}
@override
void dispose() {
_subscription?.cancel();
_subscriptionBtStatus?.cancel();
_subscriptionUsbStatus?.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.mm80, profile);
bytes += generator.setGlobalCodeTable('CP1252');
bytes += generator.text('Test Print', styles: const PosStyles(align: PosAlign.center));
bytes += generator.text('Product 1');
bytes += generator.text('Product 2');
_printEscPos(bytes, generator);
}
/// print ticket
void _printEscPos(List<int> bytes, Generator generator) async {
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();
await printerManager.connect(type: bluetoothPrinter.typePrinter, model: TcpPrinterInput(ipAddress: bluetoothPrinter.address!));
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,
});
}
这个示例应用程序展示了如何使用 flutter_pos_printer_platform_image_3
插件来扫描、连接和打印到热敏打印机。您可以根据需要调整代码以适应您的具体需求。
更多关于Flutter热敏打印机图像打印插件flutter_pos_printer_platform_image_3的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter热敏打印机图像打印插件flutter_pos_printer_platform_image_3的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,关于Flutter热敏打印机图像打印插件flutter_pos_printer_platform_image_3
的使用,下面是一个基本的代码示例,展示如何集成和使用该插件进行图像打印。
首先,确保你已经在pubspec.yaml
文件中添加了该插件的依赖:
dependencies:
flutter:
sdk: flutter
flutter_pos_printer_platform_interface: ^x.y.z # 确保版本与flutter_pos_printer_platform_image_3兼容
flutter_pos_printer_platform_image_3: ^x.y.z # 替换为最新版本号
然后,执行flutter pub get
来获取依赖。
接下来,在你的Flutter项目中,你可以按照以下步骤使用flutter_pos_printer_platform_image_3
插件进行图像打印:
- 导入必要的包:
import 'package:flutter/material.dart';
import 'package:flutter_pos_printer_platform_interface/flutter_pos_printer_platform_interface.dart';
import 'package:flutter_pos_printer_platform_image_3/flutter_pos_printer_platform_image_3.dart';
- 定义打印函数:
Future<void> printImage() async {
try {
// 获取图像数据(这里假设你有一个图像文件的路径)
final Uint8List imageData = await rootBundle.load('assets/images/your_image.png');
// 创建图像打印任务
final PosPrintImage image = PosPrintImage(
imageData: imageData,
width: 300, // 图像宽度(单位:点,1英寸=72点)
height: 200, // 图像高度(单位:点)
);
// 创建一个打印任务列表
final List<PosPrintTask> printTasks = [
PosPrintText(text: '打印图像示例', style: PosTextStyle(size: 24, align: PosTextAlign.center)),
PosPrintLine(),
image,
PosPrintCut() // 打印后切纸
];
// 获取并初始化打印机实例
final PosPrinter printer = PosPrinter.connect();
// 打印任务
await printer.printList(printTasks);
print('图像打印成功');
} catch (e) {
print('打印失败: $e');
}
}
- 在UI中触发打印:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('热敏打印机图像打印示例'),
),
body: Center(
child: ElevatedButton(
onPressed: printImage,
child: Text('打印图像'),
),
),
),
);
}
}
注意事项:
- 确保你的图像文件(如
your_image.png
)已经放在了assets
文件夹中,并在pubspec.yaml
中正确声明了资源:
flutter:
assets:
- assets/images/your_image.png
-
插件的具体API可能会随着版本的更新而变化,请参考最新的官方文档或插件的
README
文件获取最新的使用指南。 -
在实际使用中,你可能需要根据打印机的具体型号和配置调整图像的尺寸和打印设置。
这个示例代码展示了如何使用flutter_pos_printer_platform_image_3
插件在Flutter应用中打印图像。希望这对你有所帮助!