dollar_starxpand 0.0.1 dollar_starxpand: ^0.0.1 copied to clipboard
Star Micronics SDK for Dollar POS
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:dollar_starxpand/dollar_starxpand.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// VARIABLES
final _dollarStarxpandPlugin = DollarStarxpand();
List<Map<String, dynamic>> discoveredPrinters = [];
bool isDiscovering = false;
@override
void initState() {
super.initState();
setupPrinterDiscoveryListener();
}
void startDiscovery() {
setState(() {
isDiscovering = true;
discoveredPrinters.clear(); // Clear the previous discovery results
});
_dollarStarxpandPlugin.startDiscovery().then((_) {
setState(() {
isDiscovering = false;
});
}).catchError((error) {
setState(() {
isDiscovering = false;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Discovery failed: $error')),
);
});
}
void setupPrinterDiscoveryListener() {
// Listen for printer discovery events
_dollarStarxpandPlugin.onPrinterDiscovered((identifier, interfaceType) {
setState(() {
discoveredPrinters
.add({"identifier": identifier, "interface": interfaceType});
});
});
}
void connectToPrinter(String identifier, String interface) {
_dollarStarxpandPlugin.connectPrinter(identifier, interface).then((_) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Connected to printer: $identifier')),
);
}).catchError((error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to connect: $error')),
);
});
}
static const platform = MethodChannel('dollar_starxpand');
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Printer Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: ElevatedButton(
onPressed: isDiscovering
? null
: startDiscovery, // Disable the button while discovering
child: isDiscovering
? const CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(Colors.white),
)
: const Text('Discover Printers'),
),
),
Expanded(
child: Center(
child: discoveredPrinters.isEmpty
? const Text(
'No printers found. Press "Discover Printers" to search.')
: 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), // Printer icon
title: Text('Identifier: $identifier'),
subtitle: const Text(
'Tap to connect, press print icon to print'),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(Icons.print,
size: 40.0), // Print icon
onPressed: () => _dollarStarxpandPlugin
.printDocument(identifier),
),
IconButton(
icon: const Icon(Icons.dashboard,
size: 40.0), // Print icon
onPressed: () => _dollarStarxpandPlugin
.openCashDrawer(
identifier, interface)),
],
),
onTap: () =>
connectToPrinter(identifier, interface),
),
);
},
),
),
),
],
),
),
),
);
}
}