ezetap_sdk 0.2.6 copy "ezetap_sdk: ^0.2.6" to clipboard
ezetap_sdk: ^0.2.6 copied to clipboard

PlatformAndroid

Integrating the SDK will allow you to access Ezetap services in your application

example/lib/main.dart

import 'dart:async';
import 'dart:convert';
import 'dart:ui' as ui;

import 'package:eze_sdk_flutter_example/re_print.dart';
import 'package:ezetap_sdk/ezetap_sdk.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:screenshot/screenshot.dart';

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

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

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

class _MyAppState extends State<MyApp> {
  String _result = 'Unknown';

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initSdk() async {
    var json = {
      "prodAppKey": "",
      "demoAppKey": "",
      "merchantName": "",
      "userName": "",
      "currencyCode": 'INR',
      "appMode": "DEMO",
      "captureSignature": 'true',
      "prepareDevice": 'false',
      "captureReceipt": 'false'
    };
    String? result = await EzetapSdk.initialize(jsonEncode(json));
    if (!mounted) return;
    setState(() {
      _result = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Ezetap Sample'),
        ),
        body: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(onPressed: initSdk, child: const Text("SDK Init")),
              ElevatedButton(
                  onPressed: onPreparePressed,
                  child: const Text("Prepare Device")),
              ElevatedButton(
                  onPressed: () {
                    onPressed(context);
                  },
                  child: const Text("Print Bitmap")),
              ElevatedButton(
                  onPressed: onBarcodePressed,
                  child: const Text("Scan Barcode")),
              ElevatedButton(
                  onPressed: checkPrinterStatus,
                  child: const Text("Printer Status")),
              ElevatedButton(
                  onPressed: printTextReceipt,
                  child: const Text("Print Text Receipt")),
              ElevatedButton(
                  onPressed: showCustomReceiptView,
                  child: const Text("Print Custom Receipt")),
              ElevatedButton(
                  onPressed: onGetDeviceInfo,
                  child: const Text("Device Info")),
              Expanded(
                flex: 1,
                child: SingleChildScrollView(
                    scrollDirection: Axis.vertical, child: Text(_result)),
              ),
            ],
          ),
        ),
      ),
    );
  }
  Future<void> onGetDeviceInfo() async {
    String? result = await EzetapSdk.getDeviceInfo();
    if (!mounted) return;
    setState(() {
      _result = result;
    });
  }
  Future<void> onBarcodePressed() async {
    String? result = await EzetapSdk.scanBarcode();
    if (!mounted) return;
    setState(() {
      _result = result;
    });
  }

  Future<void> onPressed(BuildContext context) async {
    final controller = ScreenshotController();

    ///@author TIJO THOMAS
    ///BillWidget is a dummy receipt widget to create base64
    ///const BoxConstraints(maxHeight: 1500), change the height according to your receipt
    final bytes = await controller.captureFromLongWidget(
      InheritedTheme.captureAll(context, const Material(child: ReprintPage())),
      pixelRatio: 2.0,
      context: context,
      constraints: const BoxConstraints(maxHeight: 1500),
      delay: const Duration(milliseconds: 100),
    );
    var base64Image = base64Encode(bytes);
    debugPrint(base64Image);
    try {
      String? result = await EzetapSdk.printBitmap(base64Image);
      if (!mounted) return;
      setState(() {
        _result = result;
      });
    } on PlatformException catch (e) {
      if (!mounted) return;
      setState(() {
        _result = e.message.toString();
      });
    }
  }

  Future<void> onPreparePressed() async {
    String? result = await EzetapSdk.prepareDevice();
    if (!mounted) return;
    setState(() {
      _result = result;
    });
  }

  Future<void> checkPrinterStatus() async {
    String? result = await EzetapSdk.checkPrinterStatus();
    if (!mounted) return;
    setState(() {
      _result = result;
    });
  }

  Future<void> printTextReceipt() async {
    var txtReceipt = "-----------------------------------\n" +
        "           Receipt\n" +
        "-----------------------------------\n" +
        "\n" +
        "Date: January 9, 2024\n" +
        "Time: 12:30 PM\n" +
        "\n" +
        "-----------------------------------\n" +
        "\n" +
        "Items Purchased:\n" +
        "1. Item Name        10.00\n" +
        "2. Another Item     15.50\n" +
        "3. Yet Another Item 8.75\n" +
        "\n" +
        "-----------------------------------\n" +
        "\n" +
        "Subtotal:           34.25\n" +
        "Tax (8%):           2.74\n" +
        "Total Amount:       36.99\n" +
        "\n" +
        "-----------------------------------\n" +
        "\n" +
        "Payment Method:     Credit Card\n" +
        "\n" +
        "Card Number:        **** **** **** 1234\n" +
        "Expiry Date:        12/25\n" +
        "Authorization Code: 56789\n" +
        "\n" +
        "-----------------------------------\n" +
        "\n" +
        "Thank you for your purchase!\n" +
        "Please come again.\n" +
        "\n" +
        "-----------------------------------";
    try {
      String? result = await EzetapSdk.printTextReceipt(txtReceipt);
      if (!mounted) return;
      setState(() {
        _result = result;
      });
    } on PlatformException catch (e) {
      if (!mounted) return;
      setState(() {
        _result = e.message.toString();
      });
    }
  }

  Future<void> showCustomReceiptView() async {
    final ByteData data = await rootBundle.load('assets/logo.png');
    final Uint8List bytes = data.buffer.asUint8List();
    ui.decodeImageFromList(bytes, (ui.Image img) async {
      ByteData? byteData = await img.toByteData(format: ui.ImageByteFormat.png);
      var receipt = [
        base64.encode(byteData?.buffer.asUint8List() as List<int>),
        "<div class=\"item\" style=\"text-align: center;\"><span style=\"font-size: 34;\"><big>Heading</big></span></div>",
        "<div class=\"item\"><span>Date:</span> <span>2024-02-12</span></div>",
        "<div class=\"item\"><span>Time:</span> <span>15:30</span></div>",
        "<div class=\"item\"><span>Receipt No:</span> <span>123456</span></div>",
        "<div class=\"item\"><span>Items:</span><ul><li>Item 1 - \$10</li><li>Item 2 - \$20</li><li>Item 3 - \$15</li></ul></div>",
        "<div class=\"item\"><span>Total:</span> <span>\$45</span></div>",
        "<div class=\"item\"><span>Date:</span> <span>2024-02-12</span></div>",
        "<div class=\"item\"><span>Time:</span> <span>15:30</span></div>",
        "<div class=\"item\"><span>Receipt No:</span> <span>123456</span></div>",
        "<div class=\"item\"><span>Items:</span><ul><li>Item 1 - \$10</li><li>Item 2 - \$20</li><li>Item 3 - \$15</li></ul></div>",
        "<div class=\"item\"><span>Total:</span> <span>\$45</span></div>",
        "<div class=\"item\"><span>Date:</span> <span>2024-02-12</span></div>",
        "<div class=\"item\"><span>Time:</span> <span>15:30</span></div>",
        "<div class=\"item\"><span>Receipt No:</span> <span>123456</span></div>",
        "<div class=\"item\"><span>Items:</span><ul><li>Item 1 - \$10</li><li>Item 2 - \$20</li><li>Item 3 - \$15</li></ul></div>",
        "<div class=\"item\"><span>Total:</span> <span>\$45</span></div>",
        "<div class=\"item\"><span>Date:</span> <span>2024-02-12</span></div>",
        "<div class=\"item\"><span>Time:</span> <span>15:30</span></div>",
        "<div class=\"item\"><span>Receipt No:</span> <span>123456</span></div>",
        "<div class=\"item\"><span>Items:</span><ul><li>Item 1 - \$10</li><li>Item 2 - \$20</li><li>Item 3 - \$15</li></ul></div>",
        "<div class=\"item\"><span>Total:</span> <span>\$45</span></div>",
        base64.encode(byteData?.buffer.asUint8List() as List<int>)
      ];
      try {
        String? result = await EzetapSdk.showReceiptView(jsonEncode(receipt));
        if (!mounted) return;
        setState(() {
          _result = result;
        });
      } on PlatformException catch (e) {
        if (!mounted) return;
        setState(() {
          _result = e.message.toString();
        });
      }
    });
  }
}
4
likes
140
pub points
76%
popularity

Publisher

unverified uploader

Integrating the SDK will allow you to access Ezetap services in your application

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on ezetap_sdk