zebrautility 0.0.41 copy "zebrautility: ^0.0.41" to clipboard
zebrautility: ^0.0.41 copied to clipboard

A flutter plugin for working with zebra printers

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:zebrautility/ZebraPrinter.dart';
import 'package:zebrautility/zebrautility.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const MyHomePage(title: "test"),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  ZebraPrinter zebraPrinter;
  bool searchingWifi = false;
  bool searchingBluetooth = false;
  List<AvailablePrinter> availablePrinters = <AvailablePrinter>[];
  AvailablePrinter printer = AvailablePrinter();

  @override
  void initState() {
    initializePrinter();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: const Center(
        child: Text('just an example'),
      ),
    );
  }

  void initializePrinter() async {
    zebraPrinter ??= await Zebrautility.getPrinterInstance(onPrinterFound: (name, ipAddress, isWifi) {
      debugPrint("PrinterFound :$name$ipAddress${isWifi.toString()}");
      if (searchingWifi && isWifi) {
        availablePrinters.add(AvailablePrinter(name: name, ipAddress: ipAddress, isWifi: isWifi));
      }
      if (searchingBluetooth && !isWifi) {
        availablePrinters.add(AvailablePrinter(name: name, ipAddress: ipAddress, isWifi: isWifi));
      }
    }, onPrinterDiscoveryDone: () async {
      debugPrint("Discovery Done");
      await Future.delayed(const Duration(seconds: 1));
    }, onDiscoveryError: (int errorCode, String error) {
      debugPrint("Discovery Failed $error");
    }, onChangePrinterStatus: (status, color) {
      status = status == "Done" ? "Connected" : status;
      List<String> statuses = ["R", "Y", "G"];
      int statusID = statuses.indexOf(color);
      debugPrint("change bt printer status: $status$color");
      if (statusID == 0) {
        printer = null;
      } else if (statusID == 2) {
        debugPrint("Successfully connected to ${printer.ipAddress}");
      }
    }, onPermissionDenied: () {
      debugPrint("Permission Deny.");
    });
  }

  void changeMediaType(String item) {
    zebraPrinter.setMediaType(EnumMediaType.values.firstWhere((element) => element.name == item));
  }

  void setDarkness(int value) {
    zebraPrinter.setDarkness(value);
  }

  void calibrate() {
    zebraPrinter.calibratePrinter();
  }

  void disconnect() {
    zebraPrinter.disconnect();
    printer = null;
  }

  void refreshStatus() {
    zebraPrinter.isPrinterConnected();
  }
}

class AvailablePrinter {
  String name;
  String ipAddress;
  bool isWifi;

  AvailablePrinter({this.name, this.ipAddress, this.isWifi = false});
}
39
likes
120
pub points
85%
popularity

Publisher

verified publisherabomis.com

A flutter plugin for working with zebra printers

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on zebrautility