Flutter收银台管理插件flutter_pos_platform的使用
Flutter收银台管理插件flutter_pos_platform的使用
flutter_pos_printer_platform
这是一个用于发现打印机并发送打印机命令的库。
该库允许在不同平台(如Android、iOS、Windows)上通过不同的接口(如蓝牙、BLE、USB、Wi-Fi/以太网)打印ESC命令。
灵感来自flutter_pos_printer。
主要功能
- 支持Android、iOS和Windows
- 扫描蓝牙设备
- 发送原始
List<int> bytes
数据到设备,可以参考flutter_esc_pos_utils生成ESC/POS命令。
特性
功能 | Android | iOS | Windows | 描述 |
---|---|---|---|---|
USB接口 | ✅ | 🟥 | ✅ | 允许连接USB设备。 |
蓝牙经典接口 | ✅ | 🟥 | 🟥 | 允许连接经典蓝牙设备。 |
蓝牙低功耗(BLE)接口 | ✅ | ✅ | 🟥 | 允许连接支持BLE的蓝牙设备。 |
网络接口 | ✅ | ✅ | ✅ | 允许连接网络设备。 |
扫描 | ✅ | ✅ | ✅ | 开始扫描仅限于蓝牙设备或网络设备(Android/iOS)。 |
连接 | ✅ | ✅ | ✅ | 建立与设备的连接。 |
断开连接 | ✅ | ✅ | ✅ | 取消与设备的活动或待处理连接。 |
状态 | ✅ | ✅ | ✅ | 流式状态变化的蓝牙设备。 |
打印 | ✅ | ✅ | ✅ | 打印字节。 |
开始使用
完整的示例请查看/example文件夹。以下是使用此库的重要部分代码。
生成打印字节
import 'package:esc_pos_utils/esc_pos_utils.dart';
final profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm58, profile);
List<int> bytes = [];
bytes += generator.text('Test Print', styles: const PosStyles(align: PosAlign.center));
bytes += generator.text('Product 1');
bytes += generator.text('Product 2');
Android
允许连接蓝牙(经典和BLE)、USB和网络设备。
更改Android的minSdkVersion
flutter_pos_printer_platform
仅从Android SDK版本21开始兼容,因此你应该在android/app/build.gradle
中更改此设置:
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 31
...
}
选择打印机类型 <code>PrinterType</code>
(蓝牙、USB、网络)
如果选择蓝牙,可以发送可选参数:
isBle
-> 允许连接支持此技术的蓝牙设备。autoconnect
-> 允许在设备状态为None时重新连接。
iOS
允许连接蓝牙(BLE)和网络设备。
Windows
允许连接USB和网络设备。 对于网络设备,必须设置ip地址。
如何使用
初始化一个PrinterManager实例
import 'package:flutter_pos_printer_platform/flutter_pos_printer_platform.dart';
var printerManager = PrinterManager.instance;
扫描设备
var devices = [];
_scan(PrinterType type, {bool isBle = false}) {
// 查找打印机
PrinterManager.instance.discovery(type: type, isBle: isBle).listen((device) {
devices.add(device);
});
}
连接设备
_connectDevice(PrinterDevice selectedPrinter, PrinterType type, {bool reconnect = false, bool isBle = false, String? ipAddress = null}) async {
switch (type) {
// 仅适用于Windows和Android
case PrinterType.usb:
await PrinterManager.instance.connect(
type: type,
model: UsbPrinterInput(name: selectedPrinter.name, productId: selectedPrinter.productId, vendorId: selectedPrinter.vendorId));
break;
// 仅适用于iOS和Android
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:
}
}
断开连接
_disconnectDevice(PrinterType type) async {
await PrinterManager.instance.disconnect(type: type);
}
监听蓝牙状态
PrinterManager.instance.stateBluetooth.listen((status) {
log(' ----------------- status bt $status ------------------ ');
});
发送字节进行打印
_sendBytesToPrint(List<int> bytes, PrinterType type) async {
PrinterManager.instance.send(type: type, bytes: bytes);
}
故障排查
错误:‘State restoration of CBCentralManager is only allowed for applications that have specified the “bluetooth-central” background mode’
在info.plist中添加:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Allow App use bluetooth?</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Allow App use bluetooth?</string>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
</array>
更多关于Flutter收银台管理插件flutter_pos_platform的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter收银台管理插件flutter_pos_platform的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中集成和使用flutter_pos_platform
插件来管理收银台功能的一个简单示例。flutter_pos_platform
是一个Flutter插件,用于与各种POS(Point of Sale)硬件进行交互,如打印机、扫描器等。
第一步:添加依赖
首先,在你的pubspec.yaml
文件中添加flutter_pos_platform
的依赖:
dependencies:
flutter:
sdk: flutter
flutter_pos_platform: ^最新版本号 # 请替换为实际的最新版本号
然后运行flutter pub get
来安装依赖。
第二步:配置权限(如果需要)
某些POS硬件可能需要特定的权限,例如蓝牙权限。确保在你的AndroidManifest.xml
(对于Android)和Info.plist
(对于iOS)中添加了必要的权限。
Android(AndroidManifest.xml
)
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
iOS(Info.plist
)
<key>NSBluetoothAlwaysUsageDescription</key>
<string>App needs access to bluetooth</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App needs access to bluetooth peripherals</string>
第三步:使用插件
在你的Flutter应用中,你可以使用flutter_pos_platform
提供的API与POS硬件进行交互。以下是一个简单的示例,展示如何初始化插件并打印一张小票。
import 'package:flutter/material.dart';
import 'package:flutter_pos_platform/flutter_pos_platform.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter POS Platform Demo'),
),
body: Center(
child: POSDemo(),
),
),
);
}
}
class POSDemo extends StatefulWidget {
@override
_POSDemoState createState() => _POSDemoState();
}
class _POSDemoState extends State<POSDemo> {
late FlutterPosPlatform _flutterPosPlatform;
@override
void initState() {
super.initState();
_initializeFlutterPosPlatform();
}
Future<void> _initializeFlutterPosPlatform() async {
_flutterPosPlatform = await FlutterPosPlatform.connect();
// 检查是否成功连接
if (_flutterPosPlatform.isConnected) {
print("Successfully connected to POS platform");
} else {
print("Failed to connect to POS platform");
}
}
Future<void> _printReceipt() async {
try {
// 构建小票内容
List<dynamic> receiptData = [
{"text": "Store Name", "align": "CENTER", "type": "header", "font_size": "B", "font_type": "A"},
{"text": "Address Line 1", "align": "LEFT", "type": "text", "font_size": "A", "font_type": "A"},
{"text": "Address Line 2", "align": "LEFT", "type": "text", "font_size": "A", "font_type": "A"},
{"text": "Date: " + DateTime.now().toLocal().toString(), "align": "LEFT", "type": "text", "font_size": "A", "font_type": "A"},
{"text": "--------------------------------", "align": "CENTER", "type": "line", "font_size": "A", "font_type": "A"},
{"text": "Item 1", "align": "LEFT", "type": "text", "font_size": "A", "font_type": "A", "price": "10.00", "quantity": "1", "amount": "10.00"},
{"text": "Item 2", "align": "LEFT", "type": "text", "font_size": "A", "font_type": "A", "price": "20.00", "quantity": "2", "amount": "40.00"},
{"text": "--------------------------------", "align": "CENTER", "type": "line", "font_size": "A", "font_type": "A"},
{"text": "Total: 50.00", "align": "RIGHT", "type": "total", "font_size": "B", "font_type": "A"},
];
// 打印小票
await _flutterPosPlatform.printReceipt(receiptData);
print("Receipt printed successfully");
} catch (e) {
print("Failed to print receipt: $e");
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _printReceipt,
child: Text('Print Receipt'),
),
],
);
}
}
注意事项
- 硬件兼容性:确保你的POS硬件与
flutter_pos_platform
插件兼容。 - 错误处理:在实际应用中,应该添加更多的错误处理逻辑来确保应用的稳定性。
- 平台差异:不同的POS硬件和平台可能有不同的配置要求,请查阅
flutter_pos_platform
的官方文档以获取更多信息。
以上示例展示了如何在Flutter项目中集成和使用flutter_pos_platform
插件来打印一张简单的小票。根据你的具体需求,你可能需要调整小票的内容和格式。