talabi_printer 2.1.0 copy "talabi_printer: ^2.1.0" to clipboard
talabi_printer: ^2.1.0 copied to clipboard

PlatformAndroid

A Flutter plugin for sending printing commands to a Urovo i9100 thermal printer. This plugin allows seamless integration with the Urovo i9100 printer to send commands and facilitate printing functiona [...]

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:talabi_printer/talabi_printer.dart';
import 'foreground_service.dart'; // Import the ForegroundService class


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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  int? _printerStatus;
  int _printingCount = 0;
  String _error = '';
  bool _printing = false;
  bool _printingInLoop = false;

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

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      final printer = TalabiPrinter();
      _platformVersion =
          await printer.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      _platformVersion = 'Failed to get platform version.';
    }

    try {
      final printer = TalabiPrinter();
      _printerStatus = await printer.getStatus();
    } on PlatformException catch (e) {
      _error = '${e.code}: ${e.message ?? ''} ${e.details ?? ''}';
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              Text(
                'Printer status: ${_printerStatus ?? "Unknown printer status"}\n',
              ),
              if (_error.isNotEmpty) Text('Printer error: $_error\n'),
              Text(
                'Prints count: $_printingCount',
              ),
              const SizedBox(
                height: 30,
              ),
              _printing
                  ? const CircularProgressIndicator()
                  : ElevatedButton.icon(
                      onPressed: _print,
                      icon: const Icon(Icons.print),
                      label: const Text('Print'),
                    ),
              const SizedBox(
                height: 30,
              ),
              _printingInLoop
                  ? const CircularProgressIndicator()
                  : ElevatedButton.icon(
                      onPressed: _loopPrinting,
                      icon: const Icon(Icons.print),
                      label: const Text('Loop Printing'),
                    ),
              const SizedBox(
                height: 30,
              ),
              ElevatedButton.icon(
                onPressed: () => setState(() {
                  _printingInLoop = false;
                }),
                icon: const Icon(Icons.stop),
                label: const Text('Stop Printing'),
              ),
              const SizedBox(
                height: 30,
              ),
              ElevatedButton(
                  onPressed: () async {
                    await ForegroundService.startForegroundService();
                  },
                child: const Text('Start Foreground Service'),
              )
            ],
          ),
        ),
      ),
    );
  }

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

  Future<void> _print() async {
    setState(() {
      _printing = true;
    });
    try {
      final printer = TalabiPrinter();
      for(int i=0; i<1; ++i) {
        //await ForegroundService.wakeScreen();
        await printer.wakeScreen();
        await printer.setGray(-1);
        await printer.printText('TALABI START', fontBold: true, fontSize: 36, align: 'center');
        await printer.feedLine(1, "solid");
        await printer.feedLine(1, "empty");
        await printer.feedLine(1, "dotted");

        int count = 2;
        await printer.addText('$count', align: 'right', fontBold: true, fontSize: 36);
        await printer.addText('X\t\t\t');
        await printer.printText('מוצר', fontBold: true, fontSize: 30, isUnderline: true);

        await printer.addText('ימין''\n', align: 'right');
        await printer.addText('מרכז''\n', align: 'center');
        await printer.printText('שמאל''\n', align: 'left');
        await printer.printText('ITALIC', align: 'center', isItalic: true);
        await printer.printText('UNDERLINE', align: 'center', isUnderline: true);
        await printer.printText('STRIKE THROUGH', align: 'center', isStrikethrough: true);
        await printer.addText('SPACE BETWEEN WORDS', wordSpacing: 10);
        await printer.addTextLeftRight('Left Large', 'Right Large', fontSize: 36, fontBold: true);
        await printer.addTextLeftCenterRight('Left', 'Center', 'Right', fontSize: 20, fontBold: false);
        //await _talabiPrinterPlugin.addBarCode('Talabi Orders');
        //await _talabiPrinterPlugin.addQrCode('Talabi Orders LTD');
        //await _talabiPrinterPlugin.addImage(await readAssetBytes('assets/talabi.png'));

        await printer.feedLine(1, "solid");
        await printer.feedLine(1, "empty");
        await printer.printText('TALABI END', fontSize: 36, align: 'center', fontBold: true);
      }
      _printerStatus = await printer.print();
    } on PlatformException catch (e) {
      _error = '${e.code}: ${e.message ?? ''} ${e.details ?? ''}';
    } catch (error) {
      _error = error.toString();
    } finally {
      setState(() {
        _printing = false;
        ++_printingCount;
      });
    }
  }

  void _loopPrinting() {
    setState(() {
      _printingInLoop = true;
    });
    Timer.periodic(const Duration(seconds: 20), (timer) async {
      if (!_printingInLoop) {
        timer.cancel();
      } else {
        await _print();
      }
    });
  }
}
1
likes
130
points
3
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for sending printing commands to a Urovo i9100 thermal printer. This plugin allows seamless integration with the Urovo i9100 printer to send commands and facilitate printing functionality in Flutter applications.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on talabi_printer

Packages that implement talabi_printer