pda608_scanner_printer 0.0.1
pda608_scanner_printer: ^0.0.1 copied to clipboard
A Flutter plugin for PDA608 device thermal printer and barcode scanner.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:pda608_scanner_printer/pda608_scanner_printer.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key}); // Pass 'key' directly to the parent class
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'PDA608 Tester',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const PDA608Tester(),
);
}
}
class PDA608Tester extends StatefulWidget {
const PDA608Tester({super.key}); // Pass 'key' directly to the parent class
@override
State<PDA608Tester> createState() => _PDA608TesterState();
}
class _PDA608TesterState extends State<PDA608Tester> {
String _status = 'Not initialized';
bool _isInitialized = false;
bool _isLoading = false;
String _lastScannedBarcode = '';
// Print options
final TextEditingController _textController = TextEditingController(
text: 'Test print from Flutter'
);
int _selectedFontSize = 0; // 0 = normal, 1 = large
int _selectedAlignment = 1; // 0 = left, 1 = center, 2 = right
@override
void initState() {
super.initState();
// Setup a listener for barcode scan events - CRUCIAL FOR SCAN EVENTS
Pda608ScannerPrinter.onBarcodeScanned.listen((barcode) {
debugPrint('Barcode received in Flutter UI: $barcode');
setState(() {
_lastScannedBarcode = barcode;
_status = 'Barcode scanned: $barcode';
});
});
}
@override
void dispose() {
_textController.dispose();
Pda608ScannerPrinter.dispose();
super.dispose();
}
Future<void> _initializePrinter() async {
setState(() {
_isLoading = true;
_status = 'Initializing...';
});
try {
bool initialized = await Pda608ScannerPrinter.initializePrinter();
debugPrint("Printer initialized: $initialized");
setState(() {
_isInitialized = initialized;
_status = initialized ? 'Initialized successfully' : 'Failed to initialize';
});
} catch (e) {
setState(() {
_status = 'Error: ${e.toString()}';
});
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _printText() async {
if (!_isInitialized) {
setState(() {
_status = 'Please initialize the printer first';
});
return;
}
setState(() {
_isLoading = true;
_status = 'Printing...';
});
try {
// Use the enhanced plugin with formatting parameters
bool printed = await Pda608ScannerPrinter.printText(
text: _textController.text,
fontSize: _selectedFontSize,
alignment: _selectedAlignment,
paperWidth: 52, // Standard width for PDA608
isLabel: false,
tearPaper: true,
);
debugPrint("Text printed: $printed");
setState(() {
_status = printed ? 'Text printed successfully' : 'Failed to print text';
});
} catch (e) {
setState(() {
_status = 'Error: ${e.toString()}';
});
} finally {
setState(() {
_isLoading = false;
});
}
}
Widget _buildPrinterOptions() {
return Card(
margin: const EdgeInsets.all(8.0),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Printer Options', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
const SizedBox(height: 16),
TextField(
controller: _textController,
decoration: const InputDecoration(
labelText: 'Text to Print',
border: OutlineInputBorder(),
),
maxLines: 3,
),
const SizedBox(height: 16),
// Fix the overflow by using a Wrap widget instead of Row
Wrap(
spacing: 8,
children: [
const Text('Font Size:', style: TextStyle(fontWeight: FontWeight.bold)),
ChoiceChip(
label: const Text('Normal'),
selected: _selectedFontSize == 0,
onSelected: (selected) {
if (selected) {
setState(() {
_selectedFontSize = 0;
});
}
},
),
ChoiceChip(
label: const Text('Large'),
selected: _selectedFontSize == 1,
onSelected: (selected) {
if (selected) {
setState(() {
_selectedFontSize = 1;
});
}
},
),
],
),
const SizedBox(height: 16),
// Fix the overflow by using a Wrap widget instead of Row
Wrap(
spacing: 8,
children: [
const Text('Alignment:', style: TextStyle(fontWeight: FontWeight.bold)),
ChoiceChip(
label: const Text('Left'),
selected: _selectedAlignment == 0,
onSelected: (selected) {
if (selected) {
setState(() {
_selectedAlignment = 0;
});
}
},
),
ChoiceChip(
label: const Text('Center'),
selected: _selectedAlignment == 1,
onSelected: (selected) {
if (selected) {
setState(() {
_selectedAlignment = 1;
});
}
},
),
ChoiceChip(
label: const Text('Right'),
selected: _selectedAlignment == 2,
onSelected: (selected) {
if (selected) {
setState(() {
_selectedAlignment = 2;
});
}
},
),
],
),
],
),
),
);
}
Widget _buildScannerSection() {
return Card(
margin: const EdgeInsets.all(8.0),
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),
Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5),
color: _lastScannedBarcode.isEmpty ? Colors.white : Colors.green.shade50,
),
child: Row(
children: [
Expanded(
child: Text(
_lastScannedBarcode.isEmpty ? 'No barcode scanned yet' : _lastScannedBarcode,
style: TextStyle(
fontSize: 16,
fontWeight: _lastScannedBarcode.isEmpty ? FontWeight.normal : FontWeight.bold,
color: _lastScannedBarcode.isEmpty ? Colors.grey.shade600 : Colors.black,
),
),
),
if (_lastScannedBarcode.isNotEmpty)
IconButton(
icon: const Icon(Icons.copy),
tooltip: 'Copy barcode',
onPressed: () {
// Copy to clipboard functionality could be added here
setState(() {
_status = 'Barcode copied to clipboard';
});
},
),
],
),
),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _isInitialized ? () async {
setState(() {
_status = 'Activating barcode scanner...';
_isLoading = true;
});
try {
bool success = await Pda608ScannerPrinter.startScan();
setState(() {
_status = success ? 'Barcode scanner activated - please scan a barcode' : 'Failed to activate scanner';
});
} catch (e) {
setState(() {
_status = 'Scanner error: ${e.toString()}';
});
} finally {
setState(() {
_isLoading = false;
});
}
} : null,
icon: const Icon(Icons.qr_code_scanner),
label: const Text('Activate Scanner'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
),
),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('PDA608 Scanner & Printer Test'),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: RefreshIndicator(
onRefresh: _initializePrinter,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
color: _isInitialized ? Colors.green.shade100 : Colors.orange.shade100,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text(
'Status: $_status',
style: const TextStyle(fontSize: 16),
),
const SizedBox(height: 8),
Text(
'Printer: ${_isInitialized ? 'Initialized' : 'Not Initialized'}',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: _isInitialized ? Colors.green.shade800 : Colors.orange.shade800,
),
),
],
),
),
),
const SizedBox(height: 16),
// Make sure buttons are responsive
Wrap(
spacing: 8,
runSpacing: 8,
alignment: WrapAlignment.center,
children: [
ElevatedButton.icon(
onPressed: _initializePrinter,
icon: const Icon(Icons.power_settings_new),
label: const Text('Initialize'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 20),
minimumSize: Size(MediaQuery.of(context).size.width * 0.4, 50),
),
),
ElevatedButton.icon(
onPressed: _isInitialized ? _printText : null,
icon: const Icon(Icons.print),
label: const Text('Print'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 20),
minimumSize: Size(MediaQuery.of(context).size.width * 0.4, 50),
backgroundColor: _isInitialized ? Colors.blue : Colors.grey,
foregroundColor: Colors.white,
),
),
],
),
const SizedBox(height: 16),
_buildPrinterOptions(),
const SizedBox(height: 16),
_buildScannerSection(),
],
),
),
),
);
}
}