Код: Выделить всё
# ? Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton(
heroTag:
'uniqueTag1', // Add a unique heroTag to avoid conflicts
backgroundColor: const Color(0xff01443A),
child: const Icon(
Uiicon.angleSmallRight2_1,
color: Colors.white,
),
onPressed: () async {
if (_isPrinting) return; // Prevent back while printing
if (mounted) {
try {
await Future.delayed(const Duration(
milliseconds:
300)); // 🛑 Delay for smooth transition
Get.back();
} catch (e) {
print("Error navigating back: $e");
}
}
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton(
backgroundColor: const Color(0xff01443A),
child: const Icon(
Uiicon.fiRrPrint,
color: Colors.white,
),
onPressed: () async {
if (_isPrinting)
return; // Prevent triggering print multiple times
setState(() {
_isPrinting =
true; // Set flag to indicate printing in progress
});
try {
// Retrieve the saved printer name
String? printerName = await _getSavedPrinterName();
if (printerName != null && printerName.isNotEmpty) {
// Proceed with direct printing
await Printing.directPrintPdf(
printer: Printer(
url: printerName,
name: printerName,
),
onLayout: (format) => widget.bytes,
);
} else {
Alert.showAlertMessange(
"Printer name is not set or is empty.");
}
} catch (e) {
// Detailed error logging
print("Error during printing: $e");
} finally {
// Reset the flag when printing is finished
if (mounted) {
setState(() {
_isPrinting = false;
});
}
}
},
),
),
],
)
< /code>
и этот код печати: < /p>
static Future printInvoiceWithoutPriview(
CartController cartController,
TabControllerX tabControllerX,
PaymentBloc paymentBloc) async {
try {
// Validate the selected tab index
int? selectedTabIndex = tabControllerX.selectedIndex.value;
if (selectedTabIndex == null || selectedTabIndex < 0 || selectedTabIndex >= tabControllerX.tabs.length) {
print("❌ Error: Invalid selectedTabIndex: $selectedTabIndex");
return;
}
print("✅ Selected tab index is valid: $selectedTabIndex");
Uint8List? pdfBytes;
// Try to generate the invoice
try {
pdfBytes = await InvoicePrinting.invoice(cartController, selectedTabIndex, paymentBloc);
if (pdfBytes == null) {
print("❌ Error: Invoice generation returned null.");
return;
}
} catch (e, stackTrace) {
print("❌ Error generating invoice: $e\n$stackTrace");
return; // Stop execution if invoice generation fails
}
print("✅ Invoice generated successfully");
// Check if auto-printing is enabled
bool autoDirectPrintInvoice = await PreferencesService.isAutoPrintInvoiceEnabled();
print("🔄 Auto-printing enabled: $autoDirectPrintInvoice");
if (!autoDirectPrintInvoice) {
return;
}
try {
String? printerName = await PreferencesService.getSavedPrinterName(true);
print("🖨 Printer name retrieved: $printerName");
if (printerName != null && printerName.isNotEmpty) {
// Proceed with direct printing
print("✅ Sending invoice to printer: $printerName");
await Printing.directPrintPdf(
printer: Printer(url: printerName, name: printerName),
onLayout: (format) => pdfBytes!,
);
} else {
print("❌ Error: Printer name is not set or empty.");
Alert.showAlertMessange("Printer name is not set or is empty.");
}
} catch (e, stackTrace) {
print("❌ Error during printing: $e\n$stackTrace");
}
} catch (e, stackTrace) {
print("❌ Unexpected error in printInvoiceWithoutPriview: $e\n$stackTrace");
}
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... -due-crash