sunmi_printer_huayi 0.1.0 copy "sunmi_printer_huayi: ^0.1.0" to clipboard
sunmi_printer_huayi: ^0.1.0 copied to clipboard

PlatformAndroid

用于 Sunmi 打印机标签打印功能的 Flutter 插件,支持文本、条形码、二维码等元素打印(废弃,不好用)

example/lib/main.dart

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sunmi_printer_huayi/sunmi_printer_huayi.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> {
  bool _isConnected = false;
  Map<String, dynamic> _printerInfo = {};

  @override
  void initState() {
    super.initState();
    // _initPrinter();
  }

  Future<void> _initPrinter() async {
    try {
      final bool connected = await SunmiPrinterHuayi.isConnected;
      final Map<String, dynamic> info = await SunmiPrinterHuayi.getPrinterInfo();
      
      setState(() {
        _isConnected = connected;
        _printerInfo = info;
      });
    } catch (e) {
      debugPrint('初始化打印机出错: $e');
    }
  }

  Future<void> _printSimpleLabel() async {
    // if (!_isConnected) {
    //   _showMessage('打印机未连接');
    //   return;
    // }

    try {
      await SunmiPrinterHuayi.printLabel(
        width: 384,
        height: 240,
        text: '这是一个简单的标签',
        fontSize: 24,
        x: 10,
        y: 10,
      );
      _showMessage('打印成功');
    } catch (e) {
      _showMessage('打印失败: $e');
    }
  }

  Future<void> _printAdvancedLabel() async {
    // if (!_isConnected) {
    //   _showMessage('打印机未连接');
    //   return;
    // }

    try {
      // 创建标签元素列表
      final elements = <LabelElement>[
        // 标题
        TextElement(
          x: 192,
          y: 10,
          text: '商品标签',
          fontSize: 28,
          bold: true,
          alignment: LabelAlignment.center,
        ),
        
        // 分隔线
        LineElement(
          x: 10,
          y: 50,
          endX: 374,
          endY: 50,
          thickness: 2,
        ),
        
        // 商品名称
        TextElement(
          x: 10,
          y: 60,
          text: '商品名称:高级测试商品',
          fontSize: 20,
        ),
        
        // 商品价格
        TextElement(
          x: 10,
          y: 90,
          text: '价格:¥199.99',
          fontSize: 24,
          bold: true,
          color: '#FF0000',
        ),
        
        // 商品描述
        MultilineTextElement(
          x: 10,
          y: 130,
          text: '这是一个多行文本示例,用于展示商品的详细描述信息。可以自动换行,支持长文本。',
          width: 364,
          fontSize: 16,
        ),
        
        // 二维码
        QRCodeElement(
          x: 270,
          y: 60,
          data: 'https://example.com/product/12345',
          size: 100,
        ),
        
        // 矩形边框
        RectElement(
          x: 5,
          y: 5,
          width: 374,
          height: 230,
          thickness: 1,
          cornerRadius: 10,
        ),
      ];

      await SunmiPrinterHuayi.printLabelAdvanced(
        width: 384,  // 标签宽度
        height: 240, // 标签高度
        elements: elements,
      );
      
      _showMessage('打印成功');
    } catch (e) {
      _showMessage('打印失败: $e');
    }
  }

  Future<void> _printProductLabel() async {
    // if (!_isConnected) {
    //   _showMessage('打印机未连接');
    //   return;
    // }

    try {
      // 加载示例图片
      final ByteData logoData = await rootBundle.load('assets/logo.png');
      final Uint8List logoBytes = logoData.buffer.asUint8List();
      
      // 创建标签元素列表
      final elements = <LabelElement>[
        // 公司Logo
        ImageElement(
          x: 10,
          y: 10,
          imageData: logoBytes,
          width: 80,
          height: 40,
        ),
        
        // 标题
        TextElement(
          x: 192,
          y: 20,
          text: '产品标签',
          fontSize: 24,
          bold: true,
          alignment: LabelAlignment.center,
        ),
        
        // 分隔线
        LineElement(
          x: 10,
          y: 60,
          endX: 374,
          endY: 60,
          thickness: 1,
          dashEffect: true,
        ),
        
        // 产品名称
        TextElement(
          x: 10,
          y: 70,
          text: '产品名称:智能手表',
          fontSize: 18,
        ),
        
        // 产品型号
        TextElement(
          x: 10,
          y: 95,
          text: '型号:SW-2023',
          fontSize: 18,
        ),
        
        // 产品价格
        TextElement(
          x: 10,
          y: 120,
          text: '价格:¥299.00',
          fontSize: 20,
          bold: true,
        ),
        
        // 生产日期
        TextElement(
          x: 10,
          y: 145,
          text: '生产日期:2023-06-15',
          fontSize: 16,
        ),
        
        // 条形码
        BarcodeElement(
          x: 10,
          y: 170,
          data: '6923450657713',
          barcodeType: BarcodeType.ean13,
          height: 50,
          showText: true,
        ),
        
        // 二维码
        QRCodeElement(
          x: 280,
          y: 80,
          data: 'https://example.com/product/SW-2023',
          size: 90,
        ),
        
        // 边框
        RectElement(
          x: 5,
          y: 5,
          width: 374,
          height: 230,
          thickness: 1,
        ),
      ];

      await SunmiPrinterHuayi.printLabelAdvanced(
        width: 384,  // 标签宽度
        height: 240, // 标签高度
        elements: elements,
      );
      
      _showMessage('打印成功');
    } catch (e) {
      _showMessage('打印失败: $e');
    }
  }

  Future<void> _printQRCode() async {
    // if (!_isConnected) {
    //   _showMessage('打印机未连接');
    //   return;
    // }

    try {
      await SunmiPrinterHuayi.printQRCode(
        data: 'https://example.com',
        size: 200,
        errorLevel: 2,
      );
      _showMessage('打印成功');
    } catch (e) {
      _showMessage('打印失败: $e');
    }
  }

  Future<void> _printBarcode() async {
    if (!_isConnected) {
      _showMessage('打印机未连接');
      return;
    }

    try {
      await SunmiPrinterHuayi.printBarcode(
        data: '6923450657713',
        symbology: BarcodeType.ean13,
        height: 80,
        width: 2,
        textPosition: TextPosition.below,
      );
      _showMessage('打印成功');
    } catch (e) {
      _showMessage('打印失败: $e');
    }
  }

  void _showMessage(String message) {
    print(message);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('商米标签打印机示例'),
        ),
        body: SingleChildScrollView(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('打印机状态: ${_isConnected ? '已连接' : '未连接'}',
                          style: const TextStyle(fontSize: 16)),
                      const SizedBox(height: 8),
                      if (_printerInfo.isNotEmpty) ...[
                        Text('型号: ${_printerInfo['model'] ?? '未知'}'),
                        Text('序列号: ${_printerInfo['serialNumber'] ?? '未知'}'),
                        Text('版本: ${_printerInfo['version'] ?? '未知'}'),
                        Text('纸张宽度: ${_printerInfo['paperWidth'] ?? '未知'} mm'),
                      ],
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: _printSimpleLabel,
                child: const Text('打印简单标签'),
              ),
              const SizedBox(height: 8),
              ElevatedButton(
                onPressed: _printAdvancedLabel,
                child: const Text('打印高级标签'),
              ),
              const SizedBox(height: 8),
              ElevatedButton(
                onPressed: _printProductLabel,
                child: const Text('打印产品标签'),
              ),
              const SizedBox(height: 8),
              ElevatedButton(
                onPressed: _printQRCode,
                child: const Text('打印二维码'),
              ),
              const SizedBox(height: 8),
              ElevatedButton(
                onPressed: _printBarcode,
                child: const Text('打印条形码'),
              ),
              const SizedBox(height: 8),
              ElevatedButton(
                onPressed: () async {
                  try {
                    await SunmiPrinterHuayi.cutPaper();
                    _showMessage('切纸成功');
                  } catch (e) {
                    _showMessage('切纸失败: $e');
                  }
                },
                child: const Text('切纸'),
              ),
              const SizedBox(height: 16),
              const Divider(),
              const SizedBox(height: 16),
              const Text('自定义标签打印示例', 
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: _printCustomLabel,
                child: const Text('打印自定义标签'),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> _printCustomLabel() async {
    if (!_isConnected) {
      _showMessage('打印机未连接');
      return;
    }

    // 显示自定义标签对话框
    final result = await showDialog<Map<String, dynamic>>(
      context: context,
      builder: (context) => const CustomLabelDialog(),
    );

    if (result == null) return;

    try {
      final elements = <LabelElement>[
        // 标题
        TextElement(
          x: 192,
          y: 10,
          text: result['title'] ?? '自定义标签',
          fontSize: 24,
          bold: true,
          alignment: LabelAlignment.center,
        ),
        
        // 内容
        MultilineTextElement(
          x: 10,
          y: 50,
          text: result['content'] ?? '',
          width: 364,
          fontSize: 18,
        ),
        
        // 二维码
        if (result['showQR'] == true)
          QRCodeElement(
            x: 142,
            y: 120,
            data: result['qrData'] ?? 'https://example.com',
            size: 100,
          ),
        
        // 边框
        RectElement(
          x: 5,
          y: 5,
          width: 374,
          height: 230,
          thickness: 1,
          cornerRadius: 5,
        ),
      ];

      await SunmiPrinterHuayi.printLabelAdvanced(
        width: 384,
        height: 240,
        elements: elements,
      );
      
      _showMessage('打印成功');
    } catch (e) {
      _showMessage('打印失败: $e');
    }
  }
}

class CustomLabelDialog extends StatefulWidget {
  const CustomLabelDialog({Key? key}) : super(key: key);

  @override
  State<CustomLabelDialog> createState() => _CustomLabelDialogState();
}

class _CustomLabelDialogState extends State<CustomLabelDialog> {
  final _titleController = TextEditingController(text: '自定义标签');
  final _contentController = TextEditingController();
  final _qrDataController = TextEditingController(text: 'https://example.com');
  bool _showQR = true;

  @override
  void dispose() {
    _titleController.dispose();
    _contentController.dispose();
    _qrDataController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text('创建自定义标签'),
      content: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextField(
              controller: _titleController,
              decoration: const InputDecoration(labelText: '标题'),
            ),
            const SizedBox(height: 8),
            TextField(
              controller: _contentController,
              decoration: const InputDecoration(labelText: '内容'),
              maxLines: 3,
            ),
            const SizedBox(height: 8),
            Row(
              children: [
                Checkbox(
                  value: _showQR,
                  onChanged: (value) {
                    setState(() {
                      _showQR = value ?? false;
                    });
                  },
                ),
                const Text('显示二维码'),
              ],
            ),
            if (_showQR)
              TextField(
                controller: _qrDataController,
                decoration: const InputDecoration(labelText: '二维码内容'),
              ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: const Text('取消'),
        ),
        ElevatedButton(
          onPressed: () {
            Navigator.of(context).pop({
              'title': _titleController.text,
              'content': _contentController.text,
              'showQR': _showQR,
              'qrData': _qrDataController.text,
            });
          },
          child: const Text('打印'),
        ),
      ],
    );
  }
}
0
likes
145
points
1
downloads

Publisher

unverified uploader

Weekly Downloads

用于 Sunmi 打印机标签打印功能的 Flutter 插件,支持文本、条形码、二维码等元素打印(废弃,不好用)

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on sunmi_printer_huayi

Packages that implement sunmi_printer_huayi