143 lines
4.3 KiB
Dart
143 lines
4.3 KiB
Dart
import 'dart:convert';
|
||
import 'dart:io';
|
||
|
||
import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_redux/flutter_redux.dart';
|
||
import 'package:satu/core/models/entity_data/transaction_data.dart';
|
||
import 'package:satu/core/models/flow/sell_response.dart';
|
||
import 'package:satu/core/models/settings/printer_setting.dart';
|
||
import 'package:satu/core/redux/store.dart';
|
||
import 'package:satu/core/services/dialog_service.dart';
|
||
import 'package:satu/core/utils/locator.dart';
|
||
import 'package:satu/core/utils/pos_printer.dart';
|
||
import 'package:satu/shared/app_colors.dart';
|
||
import 'package:satu/widgets/bar/products_app_bar.dart';
|
||
|
||
class ReceiptView extends StatefulWidget {
|
||
const ReceiptView({
|
||
required this.transactionData,
|
||
Key? key,
|
||
}) : super(key: key);
|
||
|
||
final TransactionData transactionData;
|
||
|
||
@override
|
||
State<ReceiptView> createState() => _ReceiptViewState();
|
||
}
|
||
|
||
class _ReceiptViewState extends State<ReceiptView> {
|
||
final DialogService _dialogService = locator<DialogService>();
|
||
PrinterBluetoothManager printerManager = PrinterBluetoothManager();
|
||
bool printerLocked = false;
|
||
|
||
void print() async {
|
||
setState(() {
|
||
printerLocked = true;
|
||
});
|
||
try {
|
||
PrinterSetting printerSetting = Redux.store!.state.settingState!.printer!;
|
||
printerManager.selectPrinter(
|
||
PrinterBluetooth(
|
||
printerDeviceToBluetoothDevice(printerSetting.device!),
|
||
),
|
||
);
|
||
final int chunkSizeBytes = getChunkSize();
|
||
final int queueSleepTimeMs = getQueueSleep();
|
||
|
||
List<int> data = List.empty();
|
||
if (PrinterConst.encodingBigEncoding == printerSetting.encoding) {
|
||
data = await getReceiptImg(
|
||
printerSetting.paperSize!,
|
||
base64Decode(widget.transactionData.sellResponse!.checkPng!),
|
||
);
|
||
} else {
|
||
data = await getReceipt(
|
||
printerSetting.encoding!,
|
||
printerSetting.paperSize!,
|
||
widget.transactionData.sellResponse!.check!
|
||
);
|
||
}
|
||
|
||
final PosPrintResult printResult = await printerManager.writeBytes(
|
||
data,
|
||
chunkSizeBytes: chunkSizeBytes,
|
||
queueSleepTimeMs: queueSleepTimeMs,
|
||
);
|
||
if (printResult.value != 1) {
|
||
_dialogService.showDialog(description: printResult.msg);
|
||
}
|
||
} finally {
|
||
await Future.delayed(const Duration(seconds: 7));
|
||
setState(() {
|
||
printerLocked = false;
|
||
});
|
||
}
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
backgroundColor: whiteColor,
|
||
appBar: ProductsAppBar(
|
||
title: 'Просмотр чека',
|
||
actions: [
|
||
StoreConnector<AppState, PrinterSetting>(
|
||
converter: (store) => store.state.settingState!.printer!,
|
||
builder: (context, snapshot) {
|
||
final bool success =
|
||
snapshot.device != null && printerLocked == false;
|
||
return IconButton(
|
||
onPressed: success ? print : null,
|
||
icon: Icon(
|
||
Icons.print,
|
||
color: success ? textColor : placeholderColor,
|
||
),
|
||
);
|
||
},
|
||
)
|
||
],
|
||
),
|
||
body: Column(
|
||
children: [
|
||
Expanded(
|
||
child: SingleChildScrollView(
|
||
physics: const BouncingScrollPhysics(),
|
||
child: Center(
|
||
child:
|
||
imageFromBase64String(widget.transactionData.sellResponse),
|
||
),
|
||
),
|
||
)
|
||
],
|
||
),
|
||
floatingActionButton: FloatingActionButton(
|
||
onPressed: () {
|
||
// Add your onPressed code here!
|
||
},
|
||
backgroundColor: successColor,
|
||
child: const Icon(
|
||
Icons.share_rounded,
|
||
size: 20,
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
Widget imageFromBase64String(SellResponse? sellResponse) {
|
||
final String? base64String = sellResponse?.checkPng;
|
||
if (base64String == null) {
|
||
return const Padding(
|
||
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 8.0),
|
||
child: Center(
|
||
child: Text('Отсутсвует информация о чеке'),
|
||
),
|
||
);
|
||
}
|
||
return Padding(
|
||
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 8.0),
|
||
child: Image.memory(base64Decode(base64String)),
|
||
);
|
||
}
|