Dart SDK
The Dart SDK supports cross-platform Flutter applications on iOS, Android, Windows, and macOS.
Installation
Section titled “Installation”All packages are published on pub.dev. Search for the psdk prefix.
Core packages
Section titled “Core packages”dependencies: psdk_frame_father: ^latest # Core framework psdk_device_adapter: ^latest # Device adapterCommand packages (choose as needed)
Section titled “Command packages (choose as needed)”dependencies: psdk_fruit_cpcl: ^latest # CPCL Commands psdk_fruit_esc: ^latest # ESC Commands psdk_fruit_tspl: ^latest # TSPL CommandsBluetooth packages (choose by platform)
Section titled “Bluetooth packages (choose by platform)”dependencies: psdk_bluetooth_ble: ^latest # BLE (iOS/Android/macOS) psdk_bluetooth_classic: ^latest # Bluetooth Classic (Android) psdk_bluetooth_windows: ^latest # Windows BluetoothAvailable packages
Section titled “Available packages”| Package name | Description | Platform |
|---|---|---|
| psdk_frame_father | Core framework | All platforms |
| psdk_device_adapter | Device adapter | All platforms |
| psdk_bluetooth_ble | BLE | iOS/Android/macOS |
| psdk_bluetooth_classic | Bluetooth Classic | Android |
| psdk_bluetooth_windows | Windows Bluetooth | Windows |
| psdk_fruit_cpcl | CPCL commands | All platforms |
| psdk_fruit_esc | ESC commands | All platforms |
| psdk_fruit_tspl | TSPL commands | All platforms |
| psdk_network | Wi-Fi connection | All platforms |
| psdk_usb_standard | USB connection | Android |
| psdk_usb_windows | USB connection | Windows |
Platform notes
Section titled “Platform notes”Quick start
Section titled “Quick start”1. Scan for devices
Section titled “1. Scan for devices”import 'package:psdk_bluetooth_classic/psdk_bluetooth_classic.dart';
// Start Bluetooth scanningawait ClassicBluetooth().startDiscovery( disconnectConnectedDevice: false);
// Listen for scan resultsClassicBluetooth().discovered().listen((devices) { for (final device in devices) { print('Device found: ${device.name}'); }});2. Connect to a device
Section titled “2. Connect to a device”// Connect to the devicefinal connectedDevice = await ClassicBluetooth().connect(device);print('Connected: ${connectedDevice.name}');3. Disconnect
Section titled “3. Disconnect”await ClassicBluetooth().disconnect();ESC command usage
Section titled “ESC command usage”ESC commands are widely used by thermal receipt printers. See the ESC command reference for details.
Basic printing
Section titled “Basic printing”import 'package:psdk_fruit_esc/psdk_fruit_esc.dart';
final esc = ESC.generic(connectedDevice);await esc .initialize() .align(Align.center) .text('收银小票') .align(Align.left) .text('商品A x1 ¥29.90') .cut() .write();Receipt printing example
Section titled “Receipt printing example”final esc = ESC.generic(connectedDevice);await esc .initialize() // Title .align(Align.center) .textSize(width: 2, height: 2) .bold(true) .text('XX 便利店') .bold(false) .textSize(width: 1, height: 1) .text('订单号: 20240115001') .text('================================') // Product list .align(Align.left) .text('可乐 500ml x2 ¥6.00') .text('薯片 大包 x1 ¥8.50') .text('--------------------------------') // Total .textSize(width: 1, height: 2) .text('合计: ¥14.50') .textSize(width: 1, height: 1) // QR code .align(Align.center) .qrcode('https://shop.example.com', size: 5) .text('扫码关注店铺') .feed(3) .cut() .write();Print an image
Section titled “Print an image”import 'dart:ui' as ui;
// Load an imagefinal image = await loadImage('assets/logo.png');
final esc = ESC.generic(connectedDevice);await esc .initialize() .image(EscImage(image: image)) .feed(3) .cut() .write();CPCL command usage
Section titled “CPCL command usage”CPCL commands are used by portable label printers. See the CPCL command reference for details.
Basic printing
Section titled “Basic printing”import 'package:psdk_fruit_cpcl/psdk_fruit_cpcl.dart';
final cpcl = CPCL.generic(connectedDevice);await cpcl .page(CPCLPage(width: 576, height: 400)) .text(CPCLText(x: 50, y: 50, content: '商品标签')) .barcode(CPCLBarcode(x: 50, y: 150, content: '1234567890')) .print() .write();Complete label example
Section titled “Complete label example”final cpcl = CPCL.generic(connectedDevice);await cpcl .page(CPCLPage(width: 576, height: 400)) // Title .center() .text(CPCLText( x: 0, y: 30, font: 4, size: 2, content: '商品标签', )) // Product information .left() .text(CPCLText(x: 30, y: 100, content: '名称: 有机苹果')) .text(CPCLText(x: 30, y: 140, content: '规格: 500g/袋')) .text(CPCLText(x: 30, y: 180, content: '价格: ¥25.90')) // Barcode .barcode(CPCLBarcode( x: 100, y: 230, type: CPCLBarcodeType.code128, height: 60, content: '6901234567890', )) // QR code .qrcode(CPCLQRCode( x: 400, y: 100, content: 'https://example.com', )) .print() .write();TSPL command usage
Section titled “TSPL command usage”TSPL commands are used by label and barcode printers. See the TSPL command reference for details.
Basic printing
Section titled “Basic printing”import 'package:psdk_fruit_tspl/psdk_fruit_tspl.dart';
final tspl = TSPL.generic(connectedDevice);await tspl .page(TPage().width(60).height(40)) .cls() .text(TText().x(50).y(50).content('标签内容')) .print(1) .write();Complete label example
Section titled “Complete label example”final tspl = TSPL.generic(connectedDevice);await tspl .page(TPage().width(60).height(40)) .gap(3) .direction(Direction.up) .cls() // Title .text(TText() .x(180).y(30) .font('TSS24.BF2') .xMulti(2).yMulti(2) .content('商品标签')) // Separator line .bar(TBar().x(30).y(80).width(420).height(2)) // Product information .text(TText().x(30).y(100).content('品名: 有机苹果')) .text(TText().x(30).y(140).content('规格: 500g/袋')) .text(TText().x(30).y(180).content('价格: ¥25.90')) // Barcode .barcode(TBarcode() .x(100).y(220) .type('128') .height(60) .content('6901234567890')) // QR code .qrcode(TQRCode() .x(350).y(100) .cellWidth(4) .content('https://example.com')) .print(1) .write();Listen for printer responses
Section titled “Listen for printer responses”connectedDevice.received.listen((data) { print('Printer response: $data'); // Parse printer status, Query results, etc.});Error handling
Section titled “Error handling”try { await cpcl.write(); print('Print successful');} on BluetoothException catch (e) { print('Bluetooth error: ${e.message}');} on PrinterException catch (e) { print('Printer error: ${e.message}');} catch (e) { print('Unknown error: $e');}BLE usage (iOS/macOS)
Section titled “BLE usage (iOS/macOS)”import 'package:psdk_bluetooth_ble/psdk_bluetooth_ble.dart';
// Initializefinal ble = BleBluetooth();
// Scan for devicesawait ble.startDiscovery();
// Listen for discovered devicesble.discovered().listen((devices) { print('Found ${devices.length} devices');});
// Connect to a devicefinal connectedDevice = await ble.connect(device);
// Disconnectawait ble.disconnect();Example projects
Section titled “Example projects”For complete examples, see: