sunmi_flutter_helper 0.0.4 copy "sunmi_flutter_helper: ^0.0.4" to clipboard
sunmi_flutter_helper: ^0.0.4 copied to clipboard

Flutter plugin for Sunmi V2s Plus printers and scanners.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show ByteData, Uint8List, rootBundle;
import 'package:sunmi_flutter_helper/sunmi_flutter_helper.dart';

void main() {
  runApp(const MaterialApp(home: MyHomePage()));
}

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    startListening();
  }

  void startListening() {
    SunmiFlutterHelper.nfcStream.listen((data) {
      print('Full Data: $data');
    });
    SunmiFlutterHelper.scanStream.listen((scannedCode) async {
      print('🔍 Scanned: $scannedCode');
    });
  }

  Future<Uint8List> loadImageAsset(String path) async {
    final ByteData data = await rootBundle.load(path);
    return data.buffer.asUint8List();
  }

  void _printDemo() async {
    final isConnected = await SunmiFlutterHelper.isPrinterConnected();
    if (!isConnected) {
      print('❌ Printer not connected');
      return;
    }

    await SunmiFlutterHelper.printText({
      'text': "My Business Pvt Ltd.",
      'bold': true,
      'align': "center",
    });

    await SunmiFlutterHelper.printText({
      'text': "1234, Main Street\nIndustrial Area, Tech City\nState - 999999",
      'align': "center",
      'size': 12,
    });

    await SunmiFlutterHelper.printLine({'type': 'empty', 'height': 10});

    await SunmiFlutterHelper.printQrCode({
      'qrText': 'https://www.example.com',
      'align': 'center',
      'width': 200,
      'height': 200,
    });
    await SunmiFlutterHelper.printLine({'type': 'empty', 'height': 10});
    await SunmiFlutterHelper.printBarcode({
      'data': '987654321012',
      'width': 300,
      'height': 100,
      'align': 'center',
    });

    final Uint8List logoBytes = await loadImageAsset('assets/images/logo.jpg');
    await SunmiFlutterHelper.printImage({
      'image': logoBytes,
      'align': 'center',
      'width': 384,
    });

    await SunmiFlutterHelper.printLine({'type': 'dotted', 'height': 2});
    await SunmiFlutterHelper.printLine({'type': 'solid', 'height': 4});

    await SunmiFlutterHelper.printColumns({
      'texts': ['Item', 'Qty', 'Price'],
      'weights': [3, 1, 2],
      'align': 'right',
    });

    await SunmiFlutterHelper.printColumns({
      'texts': ['Bluetooth Speaker', '2', '₹4,000'],
      'weights': [3, 1, 2],
      'align': 'right',
    });

    await SunmiFlutterHelper.printLine({'type': 'empty', 'height': 10});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Sunmi Printer Demo')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _printDemo,
              child: const Text('Print Demo'),
            ),
            const SizedBox(height: 20),
          ],
        ),
      ),
    );
  }
}