dollar_starxpand 0.0.18 copy "dollar_starxpand: ^0.0.18" to clipboard
dollar_starxpand: ^0.0.18 copied to clipboard

Star Micronics SDK for Dollar POS

example/lib/main.dart

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

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

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

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

class _MyAppState extends State<MyApp> {
  // DollarStarxpand instance for interacting with the printer plugin
  final _dollarStarxpandPlugin = DollarStarxpand();

  // List to store discovered printers
  List<Map<String, dynamic>> discoveredPrinters = [];

  // Track if discovery is in progress
  bool isDiscovering = false;

  // Status message to display updates and errors
  String statusMessage = 'Press "Discover Printers" to search.';

  Map<String, dynamic> receiptData = {
    "storeName": "Store Name",
    "orderNumber": "12345",
    "datetime": "2024-11-10 12:30",
    "orderTypes": "In-store",
    "staffName": "John Doe",
    "transaction": "67890",
    "itemList": [
      {"upc": "123456", "quantity": 1, "name": "Item 1", "price": 10.0, "detail": "Some detail"},
      {"upc": "654321", "quantity": 2, "name": "Item 2", "price": 20.0, "detail": "Some detail"}
    ],
    "subtotal": 50.0,
    "tax": 5.0,
    "total": 55.0,
    "paymentList": [
      {"method": "Card", "amount": 35.0, "detail": "Visa XXXX-XXXX-XXXX-1234"},
      {"method": "Cash", "amount": 20.0, "detail": "Received: 20.0 Change: 0.0"},
    ],
    "address": "123 Main St, City, Country",
    "telephoneNumber": "123-456-7890",
    "footer": "Thank you for shopping with us!",
    "barcode": "0123456789"
  };

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

  // Method to start discovery of printers
  void startDiscovery() {
    setState(() {
      isDiscovering = true;
      discoveredPrinters.clear(); // Clear previous discovery results
      statusMessage = "Starting discovery...";
    });

    // Start discovery through plugin
    _dollarStarxpandPlugin.startDiscovery().then((_) {
      setState(() {
        isDiscovering = false;
      });
    }).catchError((error) {
      setState(() {
        isDiscovering = false;
        statusMessage = "Discovery failed: $error";
      });

      // Display error message in a SnackBar
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Discovery failed: $error')),
      );
    });
  }

  // Set up listener for printer discovery events
  void setupPrinterDiscoveryListener() {
    _dollarStarxpandPlugin.onPrinterDiscovered((identifier, interfaceType, model) {
      setState(() {
        isDiscovering = false;
        discoveredPrinters.add({
          "identifier": identifier,
          "interface": interfaceType,
          "model": model,
        });
        statusMessage = "Printer found: $identifier $interfaceType $model";
      });
      print(identifier);
      print(interfaceType);
      print(model);
    });
  }

  // Method to connect to a specific printer
  void connectToPrinter(String identifier, String interface) {
    setState(() {
      statusMessage = "Connecting to printer: $identifier...";
    });

    _dollarStarxpandPlugin.connectPrinter(identifier, interface).then((_) {
      setState(() {
        statusMessage = "Connected to printer: $identifier";
      });

      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Connected to printer: $identifier')),
      );
    }).catchError((error) {
      setState(() {
        statusMessage = "Failed to connect: $error";
      });

      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Failed to connect: $error')),
      );
    });
  }

  // Main UI for the app
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Printer Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Status Message
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Text(
                statusMessage,
                textAlign: TextAlign.center,
                style: const TextStyle(fontSize: 16.0),
              ),
            ),
            // Discover Printers Button
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: ElevatedButton(
                onPressed: isDiscovering
                    ? null
                    : startDiscovery, // Disable button while discovering
                child: isDiscovering
                    ? const CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                )
                    : const Text('Discover Printers'),
              ),
            ),
            // Display List of Discovered Printers
            Expanded(
              child: Center(
                child: discoveredPrinters.isEmpty
                    ? const Text('No printers found.')
                    : ListView.builder(
                  itemCount: discoveredPrinters.length,
                  itemBuilder: (context, index) {
                    final identifier =
                    discoveredPrinters[index]["identifier"];
                    final interface =
                    discoveredPrinters[index]["interface"];
                    final model =
                    discoveredPrinters[index]["model"];
                    return InkWell(
                      child: ListTile(
                        leading: const Icon(Icons.print),
                        title: Text('$model: $interface'),
                        subtitle: Text(identifier),
                        trailing: SizedBox(
                          width: 180,
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              IconButton(
                                icon: const Icon(Icons.print, size: 40.0),
                                onPressed: () => _dollarStarxpandPlugin
                                    .printReceipt(receiptData),
                              ),
                              IconButton(
                                icon: const Icon(Icons.dashboard,
                                    size: 40.0),
                                onPressed: () => _dollarStarxpandPlugin
                                    .openCashDrawer(identifier),
                              ),
                              IconButton(
                                icon: const Icon(Icons.close,
                                    size: 40.0),
                                onPressed: () => _dollarStarxpandPlugin
                                    .disconnectPrinter(),
                              ),
                            ],
                          ),
                        ),
                        onTap: () =>
                            connectToPrinter(identifier, interface),
                      ),
                    );
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
0
points
545
downloads

Publisher

verified publisherdollarpos.ai

Weekly Downloads

Star Micronics SDK for Dollar POS

Homepage

License

unknown (license)

Dependencies

flutter, flutter_web_plugins, plugin_platform_interface, uuid, web

More

Packages that depend on dollar_starxpand