dollar_starxpand 0.0.5 copy "dollar_starxpand: ^0.0.5" to clipboard
dollar_starxpand: ^0.0.5 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.';

  @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"];
                    return InkWell(
                      child: ListTile(
                        leading: const Icon(Icons.print),
                        title: Text('Identifier: $identifier'),
                        subtitle: const Text(
                            'Tap to connect, press icons for actions'),
                        trailing: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            IconButton(
                              icon: const Icon(Icons.print, size: 40.0),
                              onPressed: () => _dollarStarxpandPlugin
                                  .printDocument(identifier),
                            ),
                            IconButton(
                              icon: const Icon(Icons.dashboard,
                                  size: 40.0),
                              onPressed: () => _dollarStarxpandPlugin
                                  .openCashDrawer(identifier, interface),
                            ),
                          ],
                        ),
                        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