Flutter票据打印插件pos_ticket_b68的使用

Flutter票据打印插件pos_ticket_b68的使用

重要:

此插件仅适用于Android设备!

支持的功能:

  • ✅ 跳(n)行
  • ✅ 开启/关闭加粗模式
  • ✅ 可调节字体大小
  • ✅ 打印二维码
  • ✅ 使用SignikaNegative-Bold字体作为大字体
  • ✅ 使用OpenSans-Bold字体作为大字体
  • ✅ 设置字体大小为20作为中心字体大小以区分两种字体
  • ✅ 切纸 - 专用方法用于切割线

测试设备

B68

导入包

import 'package:pos_ticket_b68/pos_ticket_b68.dart';
await PosTicket.bindPrinterService(); // 初始化打印机

示例:打印停车票

await PosTicket.bindPrinterService();
await PosTicket.printText(
  text: AppConst.addressConpany,
  posFormatText: PosFormatText(
    textSize: 18,
  ),
);
await PosTicket.printLine(3); // 跳3行

示例:开始打印机测试

await PosTicket.bindPrinterService();
await PosTicket.startPrinterExam();

示例:打印二维码

await PosTicket.bindPrinterService();
await PosTicket.setAlignment(1);
await PosTicket.printQr(
  dataQRCode: "https://github.com/HVLoc",
  modulesize: 20,
  align: PosAlignment.ALIGN_CENTER,
);
await PosTicket.printLine(3);

设置字体

await PosTicket.printText(
  text: "DEFAULT",
  posFormatText: PosFormatText(
    textFont: PosTextFont.DEFAULT,
  ),
);

完整示例Demo

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:pos_ticket_b68/pos_ticket_b68.dart';

Future<void> main() async {
  runApp(
    MaterialApp(
        debugShowCheckedModeBanner: false,
        title: "Application",
        home: HomePrinterView()),
  );
  await PosTicket.bindPrinterService();
}

class HomePrinterView extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            const SizedBox(
              height: 60,
            ),
            Center(
              child: ElevatedButton(
                onPressed: () async {
                  await PosTicket.bindPrinterService();

                  await PosTicket.printText(
                    text: AppConst.nameCompany,
                    posFormatText: PosFormatText(
                      textStyle: PosTextStyle.BOLD,
                    ),
                  );
                  await PosTicket.printText(
                    text: AppConst.addressConpany,
                    posFormatText: PosFormatText(
                      textSize: 18,
                    ),
                  );
                  await PosTicket.printText(
                    text: "${AppConst.taxCodeName} ${AppConst.taxCodeCustomer}",
                  );
                  await PosTicket.setAlignment(1);
                  await PosTicket.printText(
                    text: AppConst.nameTicket,
                    posFormatText: PosFormatText(
                      textSize: 27,
                      alignment: PosAlignment.ALIGN_CENTER,
                    ),
                  );
                  await PosTicket.setAlignment(1);
                  await PosTicket.printText(
                    text: "${AppConst.fareTicket} ${AppConst.moneyTicket} đồng",
                    posFormatText: PosFormatText(
                      textSize: 25,
                      textFont: PosTextFont.SERIF,
                    ),
                  );
                  await PosTicket.setAlignment(1);
                  // 时间
                  await PosTicket.printText(
                    text:
                        "${AppConst.ticketStartingDateHP} ${DateTime.now().hour} h ${DateTime.now().minute} p",
                  );
                  await PosTicket.setAlignment(1);
                  await PosTicket.printText(
                    text:
                        "${AppConst.day} ${DateTime.now().day} ${AppConst.month} ${DateTime.now().month} ${AppConst.year} ${DateTime.now().year}",
                  );
                  await PosTicket.setAlignment(1);
                  await PosTicket.printText(
                    text:
                        "${AppConst.ncc} ${AppConst.nameCompanyNCC} - ${AppConst.nameTaxCode} ${AppConst.taxCode} \n \t ${AppConst.custommerService} ${AppConst.phoneCustomerService}",
                    posFormatText: PosFormatText(
                      textSize: 17,
                    ),
                  );
                  await PosTicket.printLine(3);
                },
                child: const Text("打印票证"),
              ),
            ),
            Center(
              child: ElevatedButton(
                onPressed: () async {
                  await PosTicket.bindPrinterService();

                  await PosTicket.printBarCode(
                      dataBarCode: "0123648445",
                      symbology: 1,
                      height: 162,
                      width: 2,
                      textposition: 1);
                },
                child: const Text("条形码"),
              ),
            ),
            Center(
              child: ElevatedButton(
                onPressed: () async {
                  await PosTicket.bindPrinterService();
                  await PosTicket.startPrinterExam();
                },
                child: const Text("示例"),
              ),
            ),
            Center(
              child: ElevatedButton(
                onPressed: () async {
                  await PosTicket.bindPrinterService();

                  await PosTicket.printText(
                    text: "DEFAULT",
                    posFormatText: PosFormatText(
                      textFont: PosTextFont.DEFAULT,
                    ),
                  );
                  await PosTicket.printText(
                    text: "DEFAULT_BOLD",
                    posFormatText: PosFormatText(
                      textFont: PosTextFont.DEFAULT_BOLD,
                    ),
                  );
                  await PosTicket.printText(
                    text: "MONOSPACE",
                    posFormatText: PosFormatText(
                      textFont: PosTextFont.MONOSPACE,
                    ),
                  );
                  await PosTicket.printText(
                    text: "SANS_SERIF",
                    posFormatText: PosFormatText(
                      textFont: PosTextFont.SANS_SERIF,
                    ),
                  );
                  await PosTicket.printText(
                    text: "SERIF",
                    posFormatText: PosFormatText(
                      textFont: PosTextFont.SERIF,
                    ),
                  );
                  await PosTicket.printText(
                    text: "CUSTOM",
                    posFormatText: PosFormatText(
                      textFont: PosTextFont.CUSTOM,
                    ),
                  );
                  await PosTicket.printLine(3);
                },
                child: const Text("文本字体"),
              ),
            ),
            Center(
              child: ElevatedButton(
                onPressed: () async {
                  await PosTicket.bindPrinterService();

                  await PosTicket.setAlignment(1);
                  await PosTicket.printQr(
                    dataQRCode: "https://github.com/HVLoc",
                    modulesize: 20,
                    align: PosAlignment.ALIGN_CENTER,
                  );
                  await PosTicket.printLine(3);
                },
                child: const Text("二维码"),
              ),
            ),
            Center(
              child: ElevatedButton(
                onPressed: () async {
                  Uint8List byte =
                      await _getImageFromAsset('assets/images/dash.jpg');
                  await PosTicket.setAlignment(1);

                  await PosTicket.printImage(byte);
                  await PosTicket.printLine(2);
                },
                child: Text("打印图片"),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<Uint8List> _getImageFromAsset(String iconPath) async {
    return await readFileBytes(iconPath);
  }

  Future<Uint8List> readFileBytes(String path) async {
    ByteData fileData = await rootBundle.load(path);
    Uint8List fileUnit8List = fileData.buffer
        .asUint8List(fileData.offsetInBytes, fileData.lengthInBytes);
    return fileUnit8List;
  }
}

class AppConst {
  static const String nameCompany = "CÔNG TY TNHH GIẢI PHÁP ĐÔ THỊ NAM HẢI";
  static const String addressConpany = "Số 33 Ngõ 151 Láng Hạ, Đống Đa, Hà Nội";
  static const String nameTicket = "VÉ TRÔNG GIỮ XE Ô TÔ";
  static const String fareTicket = "Giá vé: ";
  static const String ticketStartingDateHP = "Giờ xe vào:";
  static const String day = "ngày";
  static const String month = "tháng";
  static const String year = "năm";
  static const String ncc = "NCC";
  static const String nameTaxCode = "MST";
  static const String taxCode = "0105987432";
  static const String nameCompanyNCC = "Softdreams";
  static const String custommerService = "CSKH";
  static const String moneyTicket = "25,000";
  static const String phoneCustomerService = "19003369";
  static const String taxCodeName = "Mã số thuế:";
  static const String taxCodeCustomer = "12589654";

  static const String nameCompany2 = "CÔNG TY CPTVXDMT VÀ VT THÀNH AN";
  static const String addressConpany2 =
      "Thôn 7, X.Thọ Lộc, H.Thọ Xuân, Thanh Hoá";
  static const String nameTicket2 = "VÉ XE KHÁCH";
  static const String moneyTicket2 = "90,000";
  static const String ticketStartingDate = "Thời gian xuất bến: ";
  static const String location = "Bắc Ninh - Thanh Hoá";
}

const String PATTERN_1 = "dd/MM/yyyy";
const String PATTERN_DD = "dd";
const String PATTERN_MM = "MM";
const String PATTERN_YY = "yyyy";
const String PATTERN_2 = "dd/MM";
const String PATTERN_3 = "yyyy-MM-dd'T'HHmmss";
const String PATTERN_4 = "h:mm a dd/MM";
const String PATTERN_5 = "yyyy-MM-dd HH:mm:ss";
const String PATTERN_6 = "dd/MM/yyyy HH:mm";
const String PATTERN_7 = "HH:mm dd/MM/yyyy";
const String PATTERN_8 = "yyyy-MM-ddTHH:mm:ss";
const String PATTERN_9 = "HH:mm - dd/MM/yyyy";
const String PATTERN_10 = "dd/MM/yyyy HH:mm:ss";
const String PATTERN_11 = "HH:mm";
const String PATTERN_DEFAULT = "yyyy-MM-dd";

String convertDateToString(DateTime dateTime, String pattern) {
  return DateFormat(pattern).format(dateTime);
}

更多关于Flutter票据打印插件pos_ticket_b68的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter票据打印插件pos_ticket_b68的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


pos_ticket_b68 是一个用于 Flutter 的票据打印插件,主要用于连接和打印到 POS 打印机,特别是 B68 系列打印机。以下是如何使用该插件的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 pos_ticket_b68 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  pos_ticket_b68: ^1.0.0  # 请根据实际情况使用最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入插件

在你的 Dart 文件中导入插件:

import 'package:pos_ticket_b68/pos_ticket_b68.dart';

3. 初始化插件

在使用插件之前,通常需要初始化它:

PosTicketB68 posTicket = PosTicketB68();

4. 连接到打印机

使用 connect 方法连接到打印机。你可以通过蓝牙、Wi-Fi 或 USB 连接打印机,具体取决于你的设备和打印机支持的连接方式。

bool isConnected = await posTicket.connect(printerAddress: '打印机地址', printerType: '蓝牙/BT');
if (isConnected) {
  print('打印机连接成功');
} else {
  print('打印机连接失败');
}

5. 打印票据

连接成功后,你可以使用 printTicket 方法来打印票据。你可以传递一个包含票据内容的字符串或自定义的票据格式。

String ticketContent = '''
========================
        收据
========================
商品1      ¥10.00
商品2      ¥20.00
商品3      ¥30.00
========================
总计      ¥60.00
========================
感谢您的光临!
''';

bool isPrinted = await posTicket.printTicket(ticketContent);
if (isPrinted) {
  print('票据打印成功');
} else {
  print('票据打印失败');
}

6. 断开连接

打印完成后,记得断开与打印机的连接:

await posTicket.disconnect();
print('打印机已断开连接');

7. 处理错误

在实际使用中,可能会遇到各种错误,如连接失败、打印失败等。你可以通过捕获异常来处理这些错误:

try {
  bool isConnected = await posTicket.connect(printerAddress: '打印机地址', printerType: '蓝牙/BT');
  if (isConnected) {
    bool isPrinted = await posTicket.printTicket(ticketContent);
    if (isPrinted) {
      print('票据打印成功');
    } else {
      print('票据打印失败');
    }
  } else {
    print('打印机连接失败');
  }
} catch (e) {
  print('发生错误: $e');
}
回到顶部