Flutter插件artemis_zebra的介绍与使用

Flutter插件artemis_zebra的介绍与使用

Flutter插件artemis_zebra的介绍

本项目是一个用于演示 artemis_zebra 插件的 Flutter 应用程序。该插件主要用于与 Zebra 打印机进行通信。

获取开始

这个项目是一个起点,用于开发一个包含 Android 和/或 iOS 平台特定实现代码的 Flutter 插件包。以下是帮助你开始 Flutter 开发的一些资源:

示例代码

import 'package:artemis_zebra/artemis_zebra.dart';
import 'package:artemis_zebra/zebra_printer.dart';
import 'package:artemis_zebra/zebra_printer_interface.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
// import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _artemisZebraUtilPlugin = ArtemisZebraUtil();
  late ZebraPrinter printer;
  List<ZebraPrinter> printers = [];
  ZebraPrinterStatus? status;

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      printer = await ArtemisZebraUtil.getPrinterInstance(notifier: (_) => setState(() {}));
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            ZebraPrinter p = await ArtemisZebraUtil.getPrinterInstance(
                label: "BP TEST",
                notifier: (p) {
                  print("Notifier called ${p.status.name}");
                  setState(() {});
                },
                statusListener: (ZebraPrinterStatus s) {
                  // print("Status recieved");
                  status = s;
                  setState(() {});
                });
            printers.add(p);
            setState(() {});
            // ArtemisZebraUtil().getPlatformVersion().then((value){
            //   print(value);
            // });
          },
        ),
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            TextButton(
                onPressed: () {
                  getPermissions();
                },
                child: const Text("Get Permissions")),
            ...printers
                .map((e) => Column(
                      children: [
                        ListTile(
                          onTap: () {
                            e.isPrinterConnected();
                          },
                          onLongPress: () {
                            e.disconnectPrinter();
                          },
                          leading: Icon(
                            Icons.print,
                            color: e.status.color,
                          ),
                          subtitle: Text("${e.instanceID} :${e.status.label}"),
                          // trailing: Text(""),
                          title: Row(
                            children: [
                              TextButton(
                                onPressed: () {
                                  // print("sda");
                                  e.discoverPrinters().then((value) {
                                    print(value);
                                  });
                                },
                                child: const Text("Find"),
                              ),
                              TextButton(
                                onPressed: () {
                                  // print(e.foundPrinters.first.address);
                                  // e.connectToPrinter("192.168.1.8");
                                  // print(e.foundPrinters.map((e) => e.address));
                                  // e.disconnectPrinter();
                                  if (e.status == PrinterStatus.disconnected) {
                                    if (e.foundPrinters.isEmpty) {
                                      e.connectToPrinter('192.168.45.151');
                                    } else {
                                      if (printers.indexOf(e) != 0) {
                                        e.connectToPrinter(e.foundPrinters.last.address);
                                      } else {
                                        e.connectToPrinter(e.foundPrinters.first.address);
                                      }
                                    }
                                  } else {
                                    e.disconnectPrinter();
                                  }
                                },
                                child: const Text("Connect"),
                              ),
                              TextButton(
                                onPressed: () {
                                  e.printData('''
                    ^XA

^FX Top section with logo, name and address.
^CF0,60
^FO50,50^GB100,100,100^FS
^FO75,75^FR^GB100,100,100^FS
^FO93,93^GB40,40,40^FS
^FO220,50^FDIntershipping, Inc.^FS
^CF0,30
^FO220,115^FD1000 Shipping Lane^FS
^FO220,155^FDShelbyville TN 38102^FS
^FO220,195^FDUnited States (USA)^FS
^FO50,250^GB700,3,3^FS

^FX Second section with recipient address and permit information.
^CFA,30
^FO50,300^FDJohn Doe^FS
^FO50,340^FD100 Main Street^FS
^FO50,380^FDSpringfield TN 39021^FS
^FO50,420^FDUnited States (USA)^FS
^CFA,15
^FO600,300^GB150,150,3^FS
^FO638,340^FDPermit^FS
^FO638,390^FD123456^FS
^FO50,500^GB700,3,3^FS

^FX Third section with bar code.
^BY5,2,270
^FO100,550^BC^FD12345678^FS

^FX Fourth section (the two boxes on the bottom).
^FO50,900^GB700,250,3^FS
^FO400,900^GB3,250,3^FS
^CF0,40
^FO100,960^FDCtr. X34B-1^FS
^FO100,1010^FDREF1 F00B47^FS
^FO100,1060^FDREF2 BL4H8^FS
^CF0,190
^FO470,955^FDCA^FS

^XZ
                    ''');
                                },
                                child: const Text("Print"),
                              ),
                            ],
                          ),
                        ),
                        Row(
                          children: [
                            TextButton(
                              onPressed: () async {
                                await e.setSettings(Command.mediaType, MediaType.label);

                                // await e.setSettings(Command.mediaType, MediaType.journal);
                                // await e.setSettings(Command.mediaType, MediaType.label);
                                // await e.setSettings(Command.mediaType, MediaType.journal);
                              },
                              child: const Text("Label"),
                            ),
                            TextButton(
                              onPressed: () async {
                                await e.setSettings(Command.mediaType, MediaType.journal);

                                // await e.setSettings(Command.mediaType, MediaType.journal);
                                // await e.setSettings(Command.mediaType, MediaType.label);
                                // await e.setSettings(Command.mediaType, MediaType.journal);
                              },
                              child: const Text("Journal"),
                            ),
                            TextButton(
                              onPressed: () async {
                                await e.setSettings(Command.mediaType, MediaType.blackMark);

                                // await e.setSettings(Command.mediaType, MediaType.journal);
                                // await e.setSettings(Command.mediaType, MediaType.label);
                                // await e.setSettings(Command.mediaType, MediaType.journal);
                              },
                              child: const Text("Black"),
                            ),
                          ],
                        ),
                        Row(
                          children: [
                            TextButton(
                              onPressed: () async {
                                await e.checkPrinterStatus();
                              },
                              child: const Text("Check Status"),
                            ),
                            TextButton(
                              onPressed: () async {
                                await e.sendZplOverTcp();
                              },
                              child: const Text("Zpl"),
                            ),
                            TextButton(
                              onPressed: () async {
                                await e.sendCpclOverTcp();
                              },
                              child: const Text("Cpcl"),
                            ),
                            TextButton(
                              onPressed: () async {
                                await e.sampleWithGCD();
                              },
                              child: const Text("GCD"),
                            ),
                          ],
                        ),
                        Text(status?.toString() ?? '--'),
                        Row(
                          children: e.foundPrinters.map((e) => Text("${e.name} ${e.address}")).toList(),
                        )
                      ],
                    ))
                .toList()
          ],
        ),
      ),
    );
  }

  void getPermissions() {
      ArtemisZebraUtil.getPermissions();
  }
}

更多关于Flutter插件artemis_zebra的介绍与使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter插件artemis_zebra的介绍与使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


artemis_zebra 是一个 Flutter 插件的名称,虽然具体的功能和介绍为 undefined,但从名称可以合理推测它可能与 Artemis(可能是一个库、框架或工具)和 Zebra(可能指 Zebra Technologies 或 Zebra 打印机)相关。以下是基于插件名称的合理推测和使用方法:


推测功能

  1. 与 Zebra 打印机交互

    • Zebra Technologies 是一家知名的标签打印机和条码扫描设备制造商。artemis_zebra 可能是一个用于连接和控制 Zebra 打印机的 Flutter 插件,支持打印标签、条码等。
  2. Artemis 框架的扩展

    • Artemis 是一个 GraphQL 客户端库,用于 Flutter 和 Dart。如果 artemis_zebra 与 Artemis 相关,可能是对 Artemis 的扩展,支持与 Zebra 设备或服务集成。
  3. 条码生成与打印

    • 插件可能提供条码生成功能,并与 Zebra 打印机配合使用,实现标签打印。

使用方法(假设功能为 Zebra 打印机支持)

以下是一个基于推测的使用示例:

1. 添加依赖

pubspec.yaml 中添加插件依赖:

dependencies:
  artemis_zebra: ^latest_version

2. 初始化插件

在代码中初始化插件并连接到 Zebra 打印机:

import 'package:artemis_zebra/artemis_zebra.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 初始化插件
  await ArtemisZebra.initialize();
  runApp(MyApp());
}

3. 查找并连接打印机

Future<void> connectToPrinter() async {
  // 查找可用的 Zebra 打印机
  List<ZebraPrinter> printers = await ArtemisZebra.discoverPrinters();
  if (printers.isNotEmpty) {
    // 连接到第一个找到的打印机
    ZebraPrinter printer = printers.first;
    bool isConnected = await ArtemisZebra.connect(printer);
    if (isConnected) {
      print("已连接到打印机: ${printer.name}");
    } else {
      print("连接失败");
    }
  } else {
    print("未找到可用的打印机");
  }
}

4. 打印标签

Future<void> printLabel() async {
  // 定义标签内容
  String labelContent = """
  ^XA
  ^FO100,100
  ^A0N,50,50
  ^FDHello, Zebra!^FS
  ^XZ
  """;

  // 打印标签
  bool isPrinted = await ArtemisZebra.printLabel(labelContent);
  if (isPrinted) {
    print("标签打印成功");
  } else {
    print("标签打印失败");
  }
}

5. 断开连接

Future<void> disconnectPrinter() async {
  bool isDisconnected = await ArtemisZebra.disconnect();
  if (isDisconnected) {
    print("已断开打印机连接");
  } else {
    print("断开连接失败");
  }
}
回到顶部