sinap_woosim_plugin 1.0.0 copy "sinap_woosim_plugin: ^1.0.0" to clipboard
sinap_woosim_plugin: ^1.0.0 copied to clipboard

Sinap Woosim Flutter plugin.

example/lib/main.dart

import 'dart:io';
import 'dart:ui';

/*// ignore: import_of_legacy_library_into_null_safe*/
import 'package:animated_dialog_box/animated_dialog_box.dart';
import 'package:bluetooth_enable/bluetooth_enable.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
import 'package:sinap_woosim_plugin/sinap_woosim_plugin.dart';

import 'DiscoveryPage.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Sinap Woosim Flutter Plugin'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //Barcode Types
  static final int Barcode_Type_UPC_A = 65;
  static final int Barcode_Type_UPC_E = 66;
  static final int Barcode_Type_EAN13 = 67;
  static final int Barcode_Type_EAN8 = 68;
  static final int Barcode_Type_CODE39 = 69;
  static final int Barcode_Type_ITF = 70;
  static final int Barcode_Type_CODEBAR = 71;
  static final int Barcode_Type_CODE93 = 72;
  static final int Barcode_Type_CODE128 = 73;

//

  final ctrlBarcode = new TextEditingController(text: '6260010538434');
  final ctrlName = new TextEditingController(
      text: 'شامپو مولتی ویتامین مو خشک گلرنگ  وزن ۹۰۰ گرم');
  final ctrlCustomerPrice = new TextEditingController(text: '۲۶۲,۱۵۰');
  final ctrlSalePrice = new TextEditingController(text: '۲۴۹,۰۰۰');

  static const platform =
      // const MethodChannel("com.sinap.sinap_woosim_plugin/battery");
      const MethodChannel("com.sinap.sinap_woosim_plugin/sinap_woosim");

  var deviceConnection;
  static bool _isConnected = false;

  void ShowMessage(String text) {
    ScaffoldMessenger.of(this.context).showSnackBar(SnackBar(
      content: Text(text),
      duration: const Duration(seconds: 1),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("Sinap Woosim Flutter Plugin"),
        brightness: Brightness.dark,
      ),
      body: SingleChildScrollView(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
              child: TextField(
                controller: ctrlBarcode,
                decoration: const InputDecoration(
                  labelText: "بارکد",
                  labelStyle: TextStyle(color: Colors.blue, fontSize: 18),
                ),
                style: Theme.of(context).textTheme.bodyText2,
              ),
            ),
            Container(
              margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
              child: TextField(
                controller: ctrlName,
                decoration: const InputDecoration(
                  labelText: "شرح کالا",
                  labelStyle: TextStyle(color: Colors.blue, fontSize: 18),
                ),
                style: Theme.of(context).textTheme.bodyText2,
              ),
            ),
            Container(
              margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
              child: TextField(
                controller: ctrlCustomerPrice,
                decoration: const InputDecoration(
                  labelText: "قیمت مصرف کننده",
                  labelStyle: TextStyle(color: Colors.blue, fontSize: 18),
                ),
                style: Theme.of(context).textTheme.bodyText2,
              ),
            ),
            Container(
              margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
              child: TextField(
                controller: ctrlSalePrice,
                decoration: const InputDecoration(
                  labelText: "قیمت فروش",
                  labelStyle: TextStyle(color: Colors.blue, fontSize: 18),
                ),
                style: Theme.of(context).textTheme.bodyText2,
              ),
            ),
            Row(children: [Text('')]),
            Row(
              children: [
                Container(
                    margin: EdgeInsets.fromLTRB(3, 10, 3, 3),
                    width: MediaQuery.of(context).size.width * 0.5 - 6,
                    child: MaterialButton(
                        onPressed: () {
                          ConnectBT();
                          // _getBatteryLevel();
                        },
                        child: Text('اتصال بلوتوث'))),
                Container(
                  margin: EdgeInsets.fromLTRB(3, 10, 3, 3),
                  width: MediaQuery.of(context).size.width * 0.5 - 6,
                  child: MaterialButton(
                      onPressed: () {
                        DisconnectBT();
                      },
                      child: Text('قطع بلوتوث')),
                )
              ],
            ),
            Row(children: [
              Container(
                margin: EdgeInsets.all(3),
                width: MediaQuery.of(context).size.width - 6,
                child: MaterialButton(
                    onPressed: () {
                      Print();
                    },
                    child: Text('چاپ')),
              ),
            ]),
          ],
        ),
      ),
    );
  }

  Future<bool> TurnOnBluetooth() async {
    // Request to turn on Bluetooth within an app
    return await BluetoothEnable.enableBluetooth.then((result) {
      if (result == "true") {
        //Bluetooth has been enabled
        return true;
      } else //if (result == "false") {
        //Bluetooth has not been enabled
        return false;
    });
  }

  Future<void> ConnectBT() async {
    bool isTurnedOn = false;
    isTurnedOn = await TurnOnBluetooth() as bool;
    if (isTurnedOn) {
      final BluetoothDevice? selectedDevice = await Navigator.push(
        this.context,
        MaterialPageRoute(
            builder: (context) => DiscoveryPage()), //FlutterBlueApp()),
      );
      if (selectedDevice != null) {
        if (!Platform.isAndroid) {
          ShowMessage("این ابزار برای اندروید پیاده سازی شده است");
          return;
        }

        final bool? result = await SinapWoosimPlugin.ConnectBlutooth(
                selectedDevice.address, selectedDevice.name) ??
            false;

        setState(() {
          _isConnected = result ?? false;
        });
      }
    }
  }

  Future<void> Print() async {
    await PrintHtml();
    await SinapWoosimPlugin.PrintBarcode(
        ctrlBarcode.text, Barcode_Type_CODE128, 356, 512);
  }

  Future<void> PrintHtml() async {
    // var html = await rootBundle.loadString('assets/Base.html');
    var html = '''
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
    <style type="text/css">
    @font-face { font-family: 'iranyekanmobile_medium'; src: url('file:///assets/fonts/iranyekanmobile_medium.ttf');}
	* {
		text-align:center;
	}
	.hidden{
		color: #fff;
	}
    </style>
</head>
<body style="padding:5px; height:200px;font-family:'iranyekanmobile_medium';font-size: 25px;float: left;margin:0px;max-width: 488px;"
      dir="rtl">
<div dir="rtl" style="margin:0px;height:50px; max-width: 488px;display:flex;">
    <p align="right" dir="rtl" style="margin:0px;height:50px; max-width: 488px;margin-right: 7px;">_____Name</p>
</div>
<div style="display:flex;">
    <div style="width:288px;margin-left:25px;">
        <div style="height:45px; margin:17px 50px 7px 7px;font-size: 20px;">قیمت فروش</div>
        <div style="margin:7px;font-size:55px">_____SalePrice ریال</div>
    </div>
    <div style="margin:17px auto 0;width:170px;" class="_____DiscountClass">
        <div style="height:12px; font-size:17px;">قیمت مصرف کننده</div>
        <div style="height:42px; margin:5px;font-size: 22px;">_____CustomerPrice ریال</div>
        <div style="height:70px; margin:5px; ">
            <span style="font-size:15px;">تخفیف گلرنگ</span>
            <span style="font-size:40px;">_____DiscountValue</span>
        </div>
    </div>
</div>
</body>
</html>
    ''';

    html = html
        .replaceAll("_____Stars", "*")
        .replaceAll("_____Name", ctrlName.text)
        .replaceAll("_____SalePrice", ctrlSalePrice.text)
        .replaceAll("_____CustomerPrice", ctrlCustomerPrice.text)
        .replaceAll("_____DiscountValue", '۵%');

    await SinapWoosimPlugin.PrintHtml(html);
  }

  Future<void> DisconnectBT() async {
    await animated_dialog_box.showScaleAlertBox(
        // title: Center(child: Text("ارتباط قطع شود؟")),
        // IF YOU WANT TO ADD
        context: this.context,
        firstButton: MaterialButton(
          // FIRST BUTTON IS REQUIRED
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(40),
          ),
          color: Colors.white,
          child: Text('بله'),
          onPressed: () async {
            Navigator.of(this.context).pop();

            ShowMessage("در حال قطع ارتباط...");

            await SinapWoosimPlugin.DisconnectBlutooth;
          },
        ),
        secondButton: MaterialButton(
          // OPTIONAL BUTTON
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(40),
          ),
          color: Colors.white,
          child: Text('خیر'),
          onPressed: () {
            Navigator.of(this.context).pop();
          },
        ),
        icon: Icon(
          // null,
          Icons.bluetooth_disabled_sharp,
          color: Colors.lightGreen,
        ),
        // IF YOU WANT TO ADD ICON
        yourWidget: Container(
          child: Text('ارتباط قطع شود؟'),
        ));
  }
}