Implement PrintingStatusCard in ReceiptSpeedDial and improve error handling for Bluetooth printer connection
parent
a2eedc8efc
commit
b88c301d7d
|
@ -1,5 +1,4 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'dart:typed_data';
|
||||
import 'package:cashumit/models/receipt_item.dart';
|
||||
import 'package:cashumit/screens/add_item_screen.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print.dart';
|
||||
|
@ -12,6 +11,7 @@ import 'package:cashumit/widgets/store_info_config_dialog.dart';
|
|||
import 'package:cashumit/widgets/custom_text_config_dialog.dart';
|
||||
import 'package:cashumit/screens/webview_screen.dart';
|
||||
import 'package:cashumit/widgets/receipt_speed_dial.dart';
|
||||
import 'package:cashumit/widgets/printing_status_card.dart';
|
||||
|
||||
// Import service baru
|
||||
import 'package:cashumit/services/account_dialog_service.dart';
|
||||
|
@ -21,6 +21,10 @@ import 'package:cashumit/services/esc_pos_print_service.dart';
|
|||
import 'package:provider/provider.dart';
|
||||
import 'package:cashumit/providers/receipt_provider.dart';
|
||||
|
||||
// Import untuk penanganan error
|
||||
import 'dart:io';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class ReceiptScreen extends StatefulWidget {
|
||||
const ReceiptScreen({super.key});
|
||||
|
||||
|
@ -32,6 +36,9 @@ class _ReceiptScreenState extends State<ReceiptScreen> {
|
|||
// Bluetooth service
|
||||
final BluetoothService _bluetoothService = BluetoothService();
|
||||
|
||||
// Printing status
|
||||
bool _isPrinting = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
@ -108,6 +115,7 @@ class _ReceiptScreenState extends State<ReceiptScreen> {
|
|||
final receiptProvider = context.read<ReceiptProvider>();
|
||||
final state = receiptProvider.state;
|
||||
|
||||
try {
|
||||
// Cek dan reconnect jika perlu
|
||||
final isConnected = await _bluetoothService.reconnectIfNeeded();
|
||||
if (!isConnected) {
|
||||
|
@ -126,7 +134,6 @@ class _ReceiptScreenState extends State<ReceiptScreen> {
|
|||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await EscPosPrintService.printToThermalPrinter(
|
||||
items: state.items,
|
||||
transactionDate: state.transactionDate,
|
||||
|
@ -138,7 +145,20 @@ class _ReceiptScreenState extends State<ReceiptScreen> {
|
|||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Perintah cetak dikirim ke printer')),
|
||||
);
|
||||
} on SocketException catch (e) {
|
||||
// Tangani error koneksi secara spesifik
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Koneksi ke printer terputus: ${e.message}')),
|
||||
);
|
||||
} on PlatformException catch (e) {
|
||||
// Tangani error platform secara spesifik
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Error printer: ${e.message}')),
|
||||
);
|
||||
} catch (e) {
|
||||
// Tangani error umum
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Gagal mencetak struk: $e')),
|
||||
|
@ -146,6 +166,19 @@ class _ReceiptScreenState extends State<ReceiptScreen> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Methods to control printing status
|
||||
void _startPrinting() {
|
||||
setState(() {
|
||||
_isPrinting = true;
|
||||
});
|
||||
}
|
||||
|
||||
void _endPrinting() {
|
||||
setState(() {
|
||||
_isPrinting = false;
|
||||
});
|
||||
}
|
||||
|
||||
void _addItem() async {
|
||||
final newItem = await showDialog<ReceiptItem>(
|
||||
context: context,
|
||||
|
@ -330,7 +363,9 @@ class _ReceiptScreenState extends State<ReceiptScreen> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
return Stack(
|
||||
children: [
|
||||
Scaffold(
|
||||
backgroundColor:
|
||||
Colors.grey[300], // Latar belakang abu-abu untuk efek struk
|
||||
floatingActionButton: Consumer<ReceiptProvider>(
|
||||
|
@ -346,6 +381,8 @@ class _ReceiptScreenState extends State<ReceiptScreen> {
|
|||
hasSourceAccount: state.sourceAccountId != null,
|
||||
hasDestinationAccount: state.destinationAccountId != null,
|
||||
onSendToFirefly: _sendToFirefly,
|
||||
onPrintingStart: _startPrinting,
|
||||
onPrintingEnd: _endPrinting,
|
||||
);
|
||||
}
|
||||
),
|
||||
|
@ -419,6 +456,16 @@ class _ReceiptScreenState extends State<ReceiptScreen> {
|
|||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
PrintingStatusCard(
|
||||
isVisible: _isPrinting,
|
||||
onDismiss: () {
|
||||
setState(() {
|
||||
_isPrinting = false;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
import 'package:bluetooth_print/bluetooth_print.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:io';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class BluetoothService {
|
||||
late BluetoothPrint _bluetoothPrint;
|
||||
|
@ -44,7 +45,13 @@ class BluetoothService {
|
|||
final isConnected = await _bluetoothPrint.isConnected ?? false;
|
||||
_isConnected = isConnected;
|
||||
return isConnected;
|
||||
} on SocketException {
|
||||
// Tangani error koneksi secara spesifik
|
||||
_isConnected = false;
|
||||
return false;
|
||||
} catch (e) {
|
||||
// Tangani error umum
|
||||
_isConnected = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +69,17 @@ class BluetoothService {
|
|||
await prefs.setString('bluetooth_device_name', device.name ?? '');
|
||||
|
||||
return true;
|
||||
} on SocketException {
|
||||
// Tangani error koneksi secara spesifik
|
||||
_isConnected = false;
|
||||
return false;
|
||||
} on PlatformException {
|
||||
// Tangani error platform secara spesifik
|
||||
_isConnected = false;
|
||||
return false;
|
||||
} catch (e) {
|
||||
// Tangani error umum
|
||||
_isConnected = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -80,12 +97,24 @@ class BluetoothService {
|
|||
/// Mencetak struk ke printer thermal
|
||||
Future<void> printReceipt(Uint8List data) async {
|
||||
if (!_isConnected) {
|
||||
throw Exception('Printer tidak terhubung');
|
||||
throw SocketException('Printer tidak terhubung');
|
||||
}
|
||||
|
||||
_isPrinting = true;
|
||||
try {
|
||||
await _bluetoothPrint.printRawData(data);
|
||||
} on SocketException {
|
||||
// Tangani error koneksi saat mencetak
|
||||
_isPrinting = false;
|
||||
rethrow;
|
||||
} on PlatformException {
|
||||
// Tangani error platform saat mencetak
|
||||
_isPrinting = false;
|
||||
rethrow;
|
||||
} catch (e) {
|
||||
// Tangani error umum saat mencetak
|
||||
_isPrinting = false;
|
||||
throw Exception('Gagal mencetak: $e');
|
||||
} finally {
|
||||
_isPrinting = false;
|
||||
}
|
||||
|
@ -124,7 +153,14 @@ class BluetoothService {
|
|||
isConnected = await checkConnection();
|
||||
|
||||
return isConnected;
|
||||
} on SocketException {
|
||||
// Tangani error koneksi secara spesifik
|
||||
return false;
|
||||
} on PlatformException {
|
||||
// Tangani error platform secara spesifik
|
||||
return false;
|
||||
} catch (e) {
|
||||
// Tangani error umum
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -5,8 +5,6 @@ import 'package:cashumit/models/receipt_item.dart';
|
|||
import 'package:flutter_esc_pos_utils/flutter_esc_pos_utils.dart';
|
||||
import 'package:image/image.dart' as img;
|
||||
import 'dart:io';
|
||||
import 'dart:ui' as ui;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Service untuk menghasilkan perintah ESC/POS menggunakan flutter_esc_pos_utils
|
||||
|
@ -342,7 +340,7 @@ class EscPosPrintService {
|
|||
final isConnectedBeforePrint = await bluetoothService.checkConnection();
|
||||
if (!isConnectedBeforePrint) {
|
||||
print('Printer tidak terhubung saat akan mencetak');
|
||||
throw Exception('Printer tidak terhubung saat akan mencetak');
|
||||
throw SocketException('Printer tidak terhubung saat akan mencetak');
|
||||
}
|
||||
|
||||
print('Mengirim byte array ke printer...');
|
||||
|
@ -352,10 +350,20 @@ class EscPosPrintService {
|
|||
final Uint8List data = Uint8List.fromList(bytes);
|
||||
await bluetoothService.printReceipt(data);
|
||||
print('Perintah cetak berhasil dikirim');
|
||||
} on SocketException catch (e) {
|
||||
print('Socket error saat mengirim perintah cetak ke printer: $e');
|
||||
throw SocketException('Koneksi ke printer terputus: ${e.message}');
|
||||
} on PlatformException catch (e) {
|
||||
print('Platform error saat mengirim perintah cetak ke printer: $e');
|
||||
throw PlatformException(code: e.code, message: 'Error printer: ${e.message}');
|
||||
} catch (printError) {
|
||||
print('Error saat mengirim perintah cetak ke printer: $printError');
|
||||
throw Exception('Gagal mengirim perintah cetak: $printError');
|
||||
}
|
||||
} on SocketException {
|
||||
rethrow; // Lempar ulang error koneksi
|
||||
} on PlatformException {
|
||||
rethrow; // Lempar ulang error platform
|
||||
} catch (e, stackTrace) {
|
||||
print('Error saat mencetak struk: $e');
|
||||
print('Stack trace: $stackTrace');
|
||||
|
|
|
@ -8,7 +8,7 @@ class PrintingStatusCard extends StatefulWidget {
|
|||
super.key,
|
||||
required this.isVisible,
|
||||
this.onDismiss,
|
||||
}) : super();
|
||||
});
|
||||
|
||||
@override
|
||||
State<PrintingStatusCard> createState() => _PrintingStatusCardState();
|
||||
|
@ -92,10 +92,10 @@ class _PrintingStatusCardState extends State<PrintingStatusCard>
|
|||
padding: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
gradient: const LinearGradient(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [
|
||||
colors: const [
|
||||
Color(0xFF6A11CB),
|
||||
Color(0xFF2575FC),
|
||||
],
|
||||
|
|
|
@ -12,9 +12,11 @@ class ReceiptSpeedDial extends StatelessWidget {
|
|||
final bool hasSourceAccount;
|
||||
final bool hasDestinationAccount;
|
||||
final Future<void> Function() onSendToFirefly;
|
||||
final VoidCallback onPrintingStart;
|
||||
final VoidCallback onPrintingEnd;
|
||||
|
||||
const ReceiptSpeedDial({
|
||||
Key? key,
|
||||
super.key,
|
||||
required this.bluetoothService,
|
||||
required this.onCheckConnection,
|
||||
required this.onPrint,
|
||||
|
@ -24,7 +26,9 @@ class ReceiptSpeedDial extends StatelessWidget {
|
|||
required this.hasSourceAccount,
|
||||
required this.hasDestinationAccount,
|
||||
required this.onSendToFirefly,
|
||||
}) : super(key: key);
|
||||
required this.onPrintingStart,
|
||||
required this.onPrintingEnd,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
@ -74,10 +78,14 @@ class ReceiptSpeedDial extends StatelessWidget {
|
|||
onTap: bluetoothService.isPrinting
|
||||
? null
|
||||
: () async {
|
||||
// Panggil callback untuk memulai printing status
|
||||
onPrintingStart();
|
||||
|
||||
try {
|
||||
// Periksa koneksi secara real-time
|
||||
final isConnected = await onCheckConnection();
|
||||
if (isConnected) {
|
||||
onPrint();
|
||||
await onPrint();
|
||||
} else {
|
||||
// Coba sambungkan kembali jika ada device yang tersimpan
|
||||
if (bluetoothService.connectedDevice != null) {
|
||||
|
@ -95,7 +103,7 @@ class ReceiptSpeedDial extends StatelessWidget {
|
|||
// Periksa koneksi lagi
|
||||
final isConnectedAfterConnect = await onCheckConnection();
|
||||
if (isConnectedAfterConnect) {
|
||||
onPrint();
|
||||
await onPrint();
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
|
@ -115,6 +123,16 @@ class ReceiptSpeedDial extends StatelessWidget {
|
|||
);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// Tangani error secara umum
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Terjadi kesalahan: $e')),
|
||||
);
|
||||
} finally {
|
||||
// Pastikan printing status selalu diakhiri
|
||||
onPrintingEnd();
|
||||
}
|
||||
},
|
||||
backgroundColor: Colors.purple,
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue