291 lines
9.6 KiB
Dart
291 lines
9.6 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
import 'package:aman_kassa_flutter/core/locator.dart';
|
|
import 'package:aman_kassa_flutter/core/models/check_image_modal.dart';
|
|
import 'package:aman_kassa_flutter/core/models/dialog_models.dart';
|
|
import 'package:aman_kassa_flutter/core/route_names.dart';
|
|
import 'package:aman_kassa_flutter/core/services/blue_print_service.dart';
|
|
import 'package:aman_kassa_flutter/core/services/dialog_service.dart';
|
|
import 'package:aman_kassa_flutter/core/services/navigator_service.dart';
|
|
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
|
|
import 'package:aman_kassa_flutter/redux/state/setting_state.dart';
|
|
import 'package:aman_kassa_flutter/redux/store.dart';
|
|
import 'package:aman_kassa_flutter/shared/app_colors.dart';
|
|
import 'package:aman_kassa_flutter/shared/ui_helpers.dart';
|
|
import 'package:aman_kassa_flutter/views/settings/printer/PrinterTest.dart';
|
|
import 'package:aman_kassa_flutter/widgets/fields/busy_button_icon.dart';
|
|
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
|
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
|
|
import 'package:vocsy_esys_flutter_share/vocsy_esys_flutter_share.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
class ImageShowContainer extends StatefulWidget {
|
|
final ImageShowModel showModel;
|
|
|
|
ImageShowContainer(this.showModel);
|
|
|
|
@override
|
|
_ImageShowContainerState createState() => _ImageShowContainerState();
|
|
}
|
|
|
|
class _ImageShowContainerState extends State<ImageShowContainer> {
|
|
final BluePrintService printerManager = locator<BluePrintService>();
|
|
final DialogService _dialogService = locator<DialogService>();
|
|
final BluetoothDevice? printerBtDevice =
|
|
Redux.store!.state.settingState!.printerBT;
|
|
|
|
bool _printing = false;
|
|
|
|
void _preparePrint() async {
|
|
if (Platform.isIOS) {
|
|
_print();
|
|
} else {
|
|
printerManager.state.listen((val) {
|
|
print("state = $val");
|
|
if (!mounted) return;
|
|
if (val == 12) {
|
|
print('on');
|
|
_print();
|
|
} else if (val == 10) {
|
|
print('off');
|
|
_dialogService.showDialog(
|
|
description: 'Отсутвует соеденение Bluetooth или он отключен',
|
|
title: 'Bluetooth');
|
|
}
|
|
print('state is $val');
|
|
});
|
|
}
|
|
}
|
|
|
|
void _print() async {
|
|
final SettingState state = Redux.store!.state.settingState!;
|
|
if (state.printerBT == null) {
|
|
_dialogService.showDialog(
|
|
description: 'Укажите в настройках принтер для печати чеков');
|
|
return;
|
|
}
|
|
|
|
bool isIos = Platform.isIOS;
|
|
int chunkSizeBytes = 3096;
|
|
int queueSleepTimeMs = 100;
|
|
|
|
if (isIos) {
|
|
chunkSizeBytes = 75;
|
|
queueSleepTimeMs = 10;
|
|
}
|
|
|
|
setState(() {
|
|
_printing = true;
|
|
});
|
|
try {
|
|
printerManager.device = state.printerBT!;
|
|
await printerManager.connect();
|
|
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80
|
|
? PaperSize.mm80
|
|
: PaperSize.mm58;
|
|
if (SettingPrinterEncodingImage == state.printerEncoding) {
|
|
final bool res = await printerManager.printBytes(
|
|
Uint8List.fromList(await printImageCheck(paper, widget.showModel.data!.base64Data!)));
|
|
if (!res) {
|
|
_dialogService.showDialog(description: 'Ошибка при печати');
|
|
}
|
|
} else {
|
|
final bool res = await printerManager.printBytes(
|
|
Uint8List.fromList(await printTextCheck(paper, state.printerEncoding!,
|
|
jsonDecode(widget.showModel.data!.textData!)))
|
|
);
|
|
if (!res) {
|
|
_dialogService.showDialog(description: 'Ошибка при печати');
|
|
}
|
|
}
|
|
|
|
} catch (e) {
|
|
print(e);
|
|
}
|
|
await Future.delayed(Duration(seconds: 15));
|
|
await printerManager.disconnect();
|
|
setState(() {
|
|
_printing = false;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
//backgroundColor: fillColor,
|
|
title: Text(widget.showModel.title),
|
|
actions: [
|
|
if (_printing)
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 8.0),
|
|
child: SizedBox(
|
|
width: 36.0,
|
|
child: Center(
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
valueColor: new AlwaysStoppedAnimation<Color>(whiteColor),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
else
|
|
IconButton(icon: Icon(Icons.print), onPressed: _preparePrint)
|
|
],
|
|
),
|
|
body: ListView(
|
|
children: <Widget>[
|
|
imageFromBase64String(widget.showModel.data!.base64Data!)
|
|
],
|
|
),
|
|
floatingActionButton: MyFloatingActionButton(widget.showModel),
|
|
);
|
|
}
|
|
}
|
|
|
|
Padding imageFromBase64String(String base64String) {
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 8.0),
|
|
child: Image.memory(base64Decode(base64String)),
|
|
);
|
|
}
|
|
|
|
class ImageShowModel {
|
|
final CheckImageModal? data;
|
|
final String title;
|
|
final String? url;
|
|
|
|
ImageShowModel({this.data, required this.title, this.url});
|
|
}
|
|
|
|
class MyFloatingActionButton extends StatefulWidget {
|
|
final ImageShowModel data;
|
|
|
|
MyFloatingActionButton(this.data);
|
|
|
|
@override
|
|
_MyFloatingActionButtonState createState() => _MyFloatingActionButtonState();
|
|
}
|
|
|
|
class _MyFloatingActionButtonState extends State<MyFloatingActionButton> {
|
|
bool showFab = true;
|
|
DialogService _dialog = locator<DialogService>();
|
|
NavigatorService _navigatorService = locator<NavigatorService>();
|
|
|
|
double sheetHeight = 260;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return showFab
|
|
? FloatingActionButton(
|
|
child: Icon(Icons.share),
|
|
onPressed: () {
|
|
var bottomSheetController = showBottomSheet(
|
|
context: context,
|
|
builder: (bottomSheetContext) => Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10.0),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.all(Radius.circular(15)),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
blurRadius: 10,
|
|
color: Colors.grey[300]!,
|
|
spreadRadius: 5)
|
|
]),
|
|
height: sheetHeight,
|
|
child: Column(
|
|
children: <Widget>[
|
|
verticalSpaceSmall,
|
|
BusyButtonIcon(
|
|
title: 'WhatsApp',
|
|
onPressed: callWhatsApp,
|
|
mainColor: greenColor,
|
|
icon: MdiIcons.whatsapp,
|
|
enabled: widget.data.url != null,
|
|
),
|
|
verticalSpaceSmall,
|
|
BusyButtonIcon(
|
|
title: 'QR-код чека',
|
|
onPressed: qrGenerate,
|
|
mainColor: primaryColor,
|
|
icon: MdiIcons.qrcode,
|
|
enabled: widget.data.url != null,
|
|
),
|
|
verticalSpaceSmall,
|
|
BusyButtonIcon(
|
|
title: 'Поделиться',
|
|
onPressed: shareFile,
|
|
mainColor: redColor,
|
|
icon: Icons.share,
|
|
),
|
|
],
|
|
)));
|
|
showFoatingActionButton(false);
|
|
bottomSheetController.closed.then((value) {
|
|
showFoatingActionButton(true);
|
|
});
|
|
},
|
|
)
|
|
: Container();
|
|
}
|
|
|
|
void shareFile() async {
|
|
try {
|
|
await Share.file('Aman Kassa', 'aman_kassa_check.png',
|
|
base64Decode(widget.data.data!.base64Data!), 'image/png');
|
|
} catch (e) {
|
|
print('error: $e');
|
|
}
|
|
}
|
|
|
|
void qrGenerate() async {
|
|
_navigatorService.push(QrViewRoute,
|
|
arguments:
|
|
ImageShowModel(url: widget.data.url, title: 'Спасибо за покупку'));
|
|
}
|
|
|
|
void callWhatsApp() async {
|
|
DialogResponse response = await _dialog.showConfirmationDialogInput(
|
|
description: 'Номер телефона',
|
|
cancelTitle: 'Отмена',
|
|
confirmationTitle: 'Отправить',
|
|
formatType: 'phone');
|
|
if (response.confirmed) {
|
|
String phoneNumber = response.responseText!;
|
|
String msg = "Спасибо за покупку! \r\n ${widget.data.url} ";
|
|
launchWhatsApp(phone: phoneNumber, message: msg);
|
|
}
|
|
}
|
|
|
|
void launchWhatsApp({
|
|
required String phone,
|
|
required String message,
|
|
}) async {
|
|
String url() {
|
|
if (Platform.isIOS) {
|
|
return "whatsapp://wa.me/$phone/?text=${Uri.encodeFull(message)}";
|
|
} else {
|
|
return "whatsapp://send?phone=$phone&text=${Uri.encodeFull(message)}";
|
|
}
|
|
}
|
|
|
|
print(url());
|
|
if (await canLaunch(url())) {
|
|
await launch(url());
|
|
} else {
|
|
_dialog.showDialog(description: 'Отсутсвует приложение WhatsApp');
|
|
}
|
|
}
|
|
|
|
void showFoatingActionButton(bool value) {
|
|
setState(() {
|
|
showFab = value;
|
|
});
|
|
}
|
|
}
|