pda_608_plugin 0.0.6 copy "pda_608_plugin: ^0.0.6" to clipboard
pda_608_plugin: ^0.0.6 copied to clipboard

unlisted

A plugin for PDA608 hardware

example/lib/main.dart

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

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

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

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

class _MyAppState extends State<MyApp> {
  // Store the scanned barcode result
  String _scannedBarcode = 'No barcode scanned yet';

  // Store the print operation result
  String _printResult = 'Not printed yet';

  // Store form values for invoice
  final _formKey = GlobalKey<FormState>();
  final _customerNameController = TextEditingController();
  final _item1Controller = TextEditingController(text: 'Product A');
  final _price1Controller = TextEditingController(text: '10.00');
  final _item2Controller = TextEditingController(text: 'Product B');
  final _price2Controller = TextEditingController(text: '15.00');

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('PDA-608 Plugin Example')),
        body: SingleChildScrollView(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              // Barcode Scanner Section
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        'Barcode Scanner',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 16),
                      Text('Scanned value: $_scannedBarcode'),
                      const SizedBox(height: 16),
                      ElevatedButton(
                        onPressed: _scanBarcode,
                        child: const Text('Scan Barcode'),
                      ),
                    ],
                  ),
                ),
              ),

              const SizedBox(height: 24),

              // Invoice Form Section
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Form(
                    key: _formKey,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          'Create Invoice',
                          style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        const SizedBox(height: 16),

                        // Customer Name Field
                        TextFormField(
                          controller: _customerNameController,
                          decoration: const InputDecoration(
                            labelText: 'Customer Name',
                            border: OutlineInputBorder(),
                          ),
                          validator: (value) {
                            if (value == null || value.isEmpty) {
                              return 'Please enter customer name';
                            }
                            return null;
                          },
                        ),
                        const SizedBox(height: 16),

                        // Item 1 Row
                        Row(
                          children: [
                            Expanded(
                              flex: 3,
                              child: TextFormField(
                                controller: _item1Controller,
                                decoration: const InputDecoration(
                                  labelText: 'Item 1',
                                  border: OutlineInputBorder(),
                                ),
                              ),
                            ),
                            const SizedBox(width: 8),
                            Expanded(
                              flex: 1,
                              child: TextFormField(
                                controller: _price1Controller,
                                decoration: const InputDecoration(
                                  labelText: 'Price',
                                  prefixText: '\$',
                                  border: OutlineInputBorder(),
                                ),
                                keyboardType: TextInputType.number,
                              ),
                            ),
                          ],
                        ),
                        const SizedBox(height: 16),

                        // Item 2 Row
                        Row(
                          children: [
                            Expanded(
                              flex: 3,
                              child: TextFormField(
                                controller: _item2Controller,
                                decoration: const InputDecoration(
                                  labelText: 'Item 2',
                                  border: OutlineInputBorder(),
                                ),
                              ),
                            ),
                            const SizedBox(width: 8),
                            Expanded(
                              flex: 1,
                              child: TextFormField(
                                controller: _price2Controller,
                                decoration: const InputDecoration(
                                  labelText: 'Price',
                                  prefixText: '\$',
                                  border: OutlineInputBorder(),
                                ),
                                keyboardType: TextInputType.number,
                              ),
                            ),
                          ],
                        ),
                        const SizedBox(height: 24),

                        // Print Result Message
                        Center(
                          child: Text(
                            _printResult,
                            style: TextStyle(
                              color:
                                  _printResult.contains('Success')
                                      ? Colors.green
                                      : _printResult.contains('Failed')
                                      ? Colors.red
                                      : Colors.black,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        const SizedBox(height: 16),

                        // Print Button
                        Center(
                          child: ElevatedButton(
                            onPressed: _printInvoice,
                            style: ElevatedButton.styleFrom(
                              backgroundColor: Colors.blue,
                              padding: const EdgeInsets.symmetric(
                                horizontal: 32,
                                vertical: 16,
                              ),
                            ),
                            child: const Text(
                              'Print Invoice',
                              style: TextStyle(fontSize: 16),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// Scans a barcode using the PDA-608 hardware
  ///
  /// This function calls the plugin's scanBarcode method and updates
  /// the UI with the result
  Future<void> _scanBarcode() async {
    try {
      // Call the plugin's scanBarcode method
      final String? result = await Pda608Plugin.scanBarcode();

      // Update the UI with the result
      setState(() {
        _scannedBarcode = result ?? 'Failed to get barcode';
      });
    } catch (e) {
      // Handle any errors
      setState(() {
        _scannedBarcode = 'Error: $e';
      });
    }
  }

  /// Prints an invoice using the PDA-608 thermal printer
  ///
  /// This function generates an invoice based on the form data
  /// and sends it to the printer using the plugin's printInvoice method
  Future<void> _printInvoice() async {
    if (!_formKey.currentState!.validate()) {
      return;
    }

    try {
      // Get form values
      final customerName = _customerNameController.text;
      final item1 = _item1Controller.text;
      final price1 = double.tryParse(_price1Controller.text) ?? 0.0;
      final item2 = _item2Controller.text;
      final price2 = double.tryParse(_price2Controller.text) ?? 0.0;
      final total = price1 + price2;

      // Generate invoice number (using timestamp)
      final invoiceNumber = DateTime.now().millisecondsSinceEpoch
          .toString()
          .substring(7);

      // Format current date
      final now = DateTime.now();
      final date =
          '${now.year}/${now.month.toString().padLeft(2, '0')}/${now.day.toString().padLeft(2, '0')}';

      // Generate invoice content
      final String invoiceContent = """
INVOICE #$invoiceNumber
------------------------
Date: $date
Customer: $customerName

Items:
1. $item1${' ' * (20 - item1.length)}\$${price1.toStringAsFixed(2)}
2. $item2${' ' * (20 - item2.length)}\$${price2.toStringAsFixed(2)}
------------------------
Total:${' ' * 15}\$${total.toStringAsFixed(2)}

Thank you for your business!
""";

      // Print the invoice using the plugin
      final bool result = await Pda608Plugin.printInvoice(invoiceContent);

      // Update the UI with the result
      setState(() {
        _printResult =
            result ? 'Success! Invoice printed.' : 'Failed to print invoice.';
      });
    } catch (e) {
      // Handle any errors
      setState(() {
        _printResult = 'Error: $e';
      });
    }
  }

  @override
  void dispose() {
    // Clean up controllers
    _customerNameController.dispose();
    _item1Controller.dispose();
    _price1Controller.dispose();
    _item2Controller.dispose();
    _price2Controller.dispose();
    super.dispose();
  }
}
0
likes
0
points
600
downloads

Publisher

unverified uploader

Weekly Downloads

A plugin for PDA608 hardware

Homepage

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on pda_608_plugin

Packages that implement pda_608_plugin