blue_print_pos_table 0.0.2 copy "blue_print_pos_table: ^0.0.2" to clipboard
blue_print_pos_table: ^0.0.2 copied to clipboard

outdated

Plugin to connecting bluetooth printer device, support on Android and iOS with table

example/lib/main.dart

import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';

import 'package:blue_print_pos_table/blue_print_pos.dart';
import 'package:blue_print_pos_table/models/models.dart';
import 'package:blue_print_pos_table/receipt/receipt.dart';
import 'package:blue_print_pos_table/receipt/receipt_text_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final BluePrintPos _bluePrintPos = BluePrintPos.instance;
  List<BlueDevice> _blueDevices = <BlueDevice>[];
  BlueDevice? _selectedDevice;
  bool _isLoading = false;
  int _loadingAtIndex = -1;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Blue Print Pos'),
        ),
        body: SafeArea(
          child: _isLoading && _blueDevices.isEmpty
              ? const Center(
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
                  ),
                )
              : _blueDevices.isNotEmpty
                  ? SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Column(
                            children: List<Widget>.generate(_blueDevices.length,
                                (int index) {
                              return Row(
                                children: <Widget>[
                                  Expanded(
                                    child: GestureDetector(
                                      onTap: _blueDevices[index].address ==
                                              (_selectedDevice?.address ?? '')
                                          ? _onDisconnectDevice
                                          : () => _onSelectDevice(index),
                                      child: Padding(
                                        padding: const EdgeInsets.all(8.0),
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Text(
                                              _blueDevices[index].name,
                                              style: TextStyle(
                                                color:
                                                    _selectedDevice?.address ==
                                                            _blueDevices[index]
                                                                .address
                                                        ? Colors.blue
                                                        : Colors.black,
                                                fontSize: 20,
                                                fontWeight: FontWeight.w500,
                                              ),
                                            ),
                                            Text(
                                              _blueDevices[index].address,
                                              style: TextStyle(
                                                color:
                                                    _selectedDevice?.address ==
                                                            _blueDevices[index]
                                                                .address
                                                        ? Colors.blueGrey
                                                        : Colors.grey,
                                                fontSize: 14,
                                                fontWeight: FontWeight.w500,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                                  if (_loadingAtIndex == index && _isLoading)
                                    Container(
                                      height: 24.0,
                                      width: 24.0,
                                      margin: const EdgeInsets.only(right: 8.0),
                                      child: const CircularProgressIndicator(
                                        valueColor:
                                            AlwaysStoppedAnimation<Color>(
                                          Colors.blue,
                                        ),
                                      ),
                                    ),
                                  if (!_isLoading &&
                                      _blueDevices[index].address ==
                                          (_selectedDevice?.address ?? ''))
                                    TextButton(
                                      onPressed: _onPrintReceipt,
                                      child: Container(
                                        color: _selectedDevice == null
                                            ? Colors.grey
                                            : Colors.blue,
                                        padding: const EdgeInsets.all(8.0),
                                        child: const Text(
                                          'Test Print',
                                          style: TextStyle(color: Colors.white),
                                        ),
                                      ),
                                      style: ButtonStyle(
                                        backgroundColor: MaterialStateProperty
                                            .resolveWith<Color>(
                                          (Set<MaterialState> states) {
                                            if (states.contains(
                                                MaterialState.pressed)) {
                                              return Theme.of(context)
                                                  .colorScheme
                                                  .primary
                                                  .withOpacity(0.5);
                                            }
                                            return Theme.of(context)
                                                .primaryColor;
                                          },
                                        ),
                                      ),
                                    ),
                                ],
                              );
                            }),
                          ),
                        ],
                      ),
                    )
                  : Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: const <Widget>[
                          Text(
                            'Scan bluetooth device',
                            style: TextStyle(fontSize: 24, color: Colors.blue),
                          ),
                          Text(
                            'Press button scan',
                            style: TextStyle(fontSize: 14, color: Colors.grey),
                          ),
                        ],
                      ),
                    ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _isLoading ? null : _onScanPressed,
          child: const Icon(Icons.search),
          backgroundColor: _isLoading ? Colors.grey : Colors.blue,
        ),
      ),
    );
  }

  Future<void> _onScanPressed() async {
    setState(() => _isLoading = true);
    _bluePrintPos.scan().then((List<BlueDevice> devices) {
      if (devices.isNotEmpty) {
        setState(() {
          _blueDevices = devices;
          _isLoading = false;
        });
      } else {
        setState(() => _isLoading = false);
      }
    });
  }

  void _onDisconnectDevice() {
    _bluePrintPos.disconnect().then((ConnectionStatus status) {
      if (status == ConnectionStatus.disconnect) {
        setState(() {
          _selectedDevice = null;
        });
      }
    });
  }

  void _onSelectDevice(int index) {
    setState(() {
      _isLoading = true;
      _loadingAtIndex = index;
    });
    final BlueDevice blueDevice = _blueDevices[index];
    _bluePrintPos.connect(blueDevice).then((ConnectionStatus status) {
      if (status == ConnectionStatus.connected) {
        setState(() => _selectedDevice = blueDevice);
      } else if (status == ConnectionStatus.timeout) {
        _onDisconnectDevice();
      } else {
        print('$runtimeType - something wrong');
      }
      setState(() => _isLoading = false);
    });
  }

  Future<void> _onPrintReceipt() async {
    // / Example for Print Image
    final ByteData logoBytes = await rootBundle.load(
      'assets/sato_logo.jpg',
    );

    /// Example for Print Text
    final ReceiptSectionText receiptText = ReceiptSectionText();
    receiptText.addImage(
      base64.encode(Uint8List.view(logoBytes.buffer)),
      width: 150,
    );
    receiptText.addSpacer();
    receiptText.addText(
      'SATO Robotic',
      size: ReceiptTextSizeType.medium,
      style: ReceiptTextStyleType.bold,
    );
    receiptText.addText(
      'Carwash Park Paramount Gading Serpong',
      size: ReceiptTextSizeType.medium,
    );
    receiptText.addSpacer(useDashed: true);
    receiptText.addLeftRightText('ID', '#1239');
    receiptText.addLeftRightText('Date', '04/06/21, 10:00');
    receiptText.addLeftRightText('Cashier', 'John Doe');
    receiptText.addLeftRightText('Customer', 'John Doe');
    receiptText.addLeftRightText('Payment', 'Cash');
    receiptText.addSpacer(useDashed: true);

    double widthPercentCol1 = 70;
    double widthPercentCol2 = 5;
    double widthPercentCol3 = 15;

    const ReceiptTextStyle textStyleHeader = ReceiptTextStyle(
      size: ReceiptTextSizeType.medium,
      type: ReceiptTextStyleType.bold,
    );

    final List<ColumnTextTable> headerColum = [
      ColumnTextTable(
        'Name',
        isHeader: true,
        widthPercent: widthPercentCol1,
        textStyle: textStyleHeader,
      ),
      ColumnTextTable(
        'Qty',
        padding: const PaddingColumn(
          right: 10,
          left: 10,
        ),
        widthPercent: widthPercentCol2,
        isHeader: true,
        textStyle: textStyleHeader,
      ),
      ColumnTextTable(
        'Price',
        isHeader: true,
        widthPercent: widthPercentCol3,
        textStyle: textStyleHeader,
      ),
    ];

    const ReceiptTextStyle textStyle = ReceiptTextStyle(
      size: ReceiptTextSizeType.medium,
    );
    List<String> foodAndBeverageProducts = [
      "FlavorFusion Elixirs",
      "SavoryTwist Gourmet Bites",
      "FrothMaster Deluxe Latte Machine",
      "CrispDelight Air Fryer Oven",
      "ZestZing Citrus Infuser Water Bottle",
      "SpiceCraft Electric Spice Grinder",
      "BrewMingle Artisan Tea Collection",
      "SweetSculpt Dessert Sculpting Kit",
      "MunchieDash Snack Subscription Box",
      "SipSync Smart Hydration Tracker"
    ];

    List<String> automotiveProducts = [
      "TurboCharge Pro Performance Chip",
      "GearShift Prodigy Manual Transmission Kit",
      "AutoGlow LED Underglow Lights",
      "DriveGuardian 360 Dash Cam",
      "NitroPulse Fuel Additive Boost",
      "AeroFlow Precision Wind Deflectors",
      "VeloMax Alloy Wheel Protectors",
      "EngineEagle Oil Additive Enhancer",
      "SparkSynch Plug-and-Play Ignition Kit",
      "MechMinder Car Maintenance Journal"
    ];

    List<String> mergedProducts = [];
    mergedProducts.addAll(foodAndBeverageProducts);
    mergedProducts.addAll(automotiveProducts);

    List<String> priceRandoms = [
      "Rp30.000",
      "Rp300.000",
      "Rp30.000",
      "Rp300.000",
      "Rp1.000.000",
      // "Rp10.000.000",
      "Rp50.000",
      "Rp500.000",
    ];

    List<RowTextTable> listRow = [];
    for (int i = 0; i < 1; i++) {
      // text random for test

      int randomIndexPrice = Random().nextInt(priceRandoms.length);

      listRow.add(RowTextTable([
        ColumnTextTable(
          'Cuci Hidrolic Reguler + Fogging',
          widthPercent: widthPercentCol1,
          textStyle: textStyle,
          // useEllipsis: true,
        ),
        ColumnTextTable(
          '',
          widthPercent: widthPercentCol2,
          textStyle: textStyle,
        ),
        ColumnTextTable(
          priceRandoms[randomIndexPrice],
          widthPercent: widthPercentCol3,
          textStyle: textStyle,
        ),
      ]));
    }

    List<RowTextTable> addonRow = [];

    for (int i = 0; i < 1; i++) {
      // text random for test
      int randomIndex = Random().nextInt(mergedProducts.length);

      int randomIndexPrice = Random().nextInt(priceRandoms.length);

      addonRow.add(RowTextTable([
        ColumnTextTable(
          mergedProducts[randomIndex],
          widthPercent: widthPercentCol1,
          textStyle: textStyle,
          useEllipsis: true,
        ),
        ColumnTextTable(
          '',
          widthPercent: widthPercentCol2,
          textStyle: textStyle,
          padding: const PaddingColumn(
            right: 10,
            left: 10,
          ),
        ),
        ColumnTextTable(
          priceRandoms[randomIndexPrice],
          widthPercent: widthPercentCol3,
          textStyle: textStyle,
        ),
      ]));
    }

    List<RowTextTable> fnb = [];

    for (int i = 0; i < 1; i++) {
      // text random for test
      int randomIndex = Random().nextInt(mergedProducts.length);

      int randomIndexPrice = Random().nextInt(priceRandoms.length);

      fnb.add(RowTextTable([
        ColumnTextTable(
          mergedProducts[randomIndex],
          widthPercent: widthPercentCol1,
          textStyle: textStyle,
          useEllipsis: true,
        ),
        ColumnTextTable(
          randomIndexPrice.toString(),
          widthPercent: widthPercentCol2,
          textStyle: textStyle,
          padding: const PaddingColumn(
            right: 10,
            left: 10,
          ),
        ),
        ColumnTextTable(
          priceRandoms[randomIndexPrice],
          widthPercent: widthPercentCol3,
          textStyle: textStyle,
        ),
      ]));
    }

    var rec = ReceiptTextTable(
      <RowTextTable>[
        RowTextTable([
          ColumnTextTable(
            'Service/Layanan',
            // isHeader: true,
            widthPercent: widthPercentCol1,
            textStyle: textStyleHeader,
          ),
          ColumnTextTable(
            '',
            isHeader: true,
            widthPercent: widthPercentCol2,
            textStyle: textStyleHeader,
          ),
          ColumnTextTable(
            'Price',
            // isHeader: true,
            widthPercent: widthPercentCol3,
            textStyle: textStyleHeader,
          ),
        ]),
        ...listRow,
        RowTextTable([
          ColumnTextTable(
            '------------',
            widthPercent: 100,
            textStyle: textStyleHeader,
          ),
        ]),
        RowTextTable([
          ColumnTextTable(
            'Addon',
            // isHeader: true,
            widthPercent: widthPercentCol1,
            textStyle: textStyleHeader,
          ),
          ColumnTextTable(
            '',
            padding: const PaddingColumn(
              right: 10,
              left: 10,
            ),
            widthPercent: widthPercentCol2,
            // isHeader: true,
            textStyle: textStyleHeader,
          ),
          ColumnTextTable(
            'Price',
            // isHeader: true,
            widthPercent: widthPercentCol3,
            textStyle: textStyleHeader,
          ),
        ]),
        ...addonRow,
        RowTextTable([
          ColumnTextTable(
            '<hr/>',
            widthPercent: 100,
            textStyle: textStyleHeader,
          ),
        ]),
        RowTextTable([
          ColumnTextTable(
            'FnB & Store',
            // isHeader: true,
            widthPercent: widthPercentCol1,
            textStyle: textStyleHeader,
          ),
          ColumnTextTable(
            'Qty',
            padding: const PaddingColumn(
              right: 10,
              left: 10,
            ),
            widthPercent: widthPercentCol2,
            // isHeader: true,
            textStyle: textStyleHeader,
          ),
          ColumnTextTable(
            'Price',
            // isHeader: true,
            widthPercent: widthPercentCol3,
            textStyle: textStyleHeader,
          ),
        ]),
        ...fnb,
      ],
    );

    receiptText.addTextTable(
      rec,
    );
    receiptText.addSpacer(useDashed: true);
    receiptText.addLeftRightText('Subtotal', 'Rp 1.000.000');
    receiptText.addLeftRightText('Discount', 'Rp 0');
    receiptText.addLeftRightText('Tax', 'Rp 0');
    receiptText.addLeftRightText('Grand Total', 'Rp 1.000.000');
    receiptText.addSpacer(useDashed: true);
    receiptText.addText('Thank you for coming',);

    await _bluePrintPos.printReceiptText(receiptText,
        useCut: true, feedCount: 2, useRaster: true);

    /// Example for print QR
    // await _bluePrintPos.printQR('www.google.com', size: 250);

    /// Text after QR
    // final ReceiptSectionText receiptSecondText = ReceiptSectionText();
    // receiptSecondText.addText('Powered by ayeee',
    //     size: ReceiptTextSizeType.small);
    // receiptSecondText.addSpacer();
    // await _bluePrintPos.printReceiptText(receiptSecondText, feedCount: 1);
  }
}
1
likes
0
points
34
downloads

Publisher

unverified uploader

Weekly Downloads

Plugin to connecting bluetooth printer device, support on Android and iOS with table

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

blue_thermal_printer, esc_pos_utils_plus, flutter, flutter_blue_plus, image, qr_flutter

More

Packages that depend on blue_print_pos_table

Packages that implement blue_print_pos_table