dollar_starxpand 0.0.10 dollar_starxpand: ^0.0.10 copied to clipboard
Star Micronics SDK for Dollar POS
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"];
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
.printDocument(identifier),
),
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),
),
);
},
),
),
),
],
),
),
);
}
}