273 lines
9.3 KiB
Dart
273 lines
9.3 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:aman_kassa_flutter/core/entity/Voucher.dart';
|
|
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/aman_dao.dart';
|
|
import 'package:aman_kassa_flutter/core/models/card_data.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/BankService.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/views/payment/payment_view.dart';
|
|
import 'package:aman_kassa_flutter/widgets/fields/busy_button_icon.dart';
|
|
import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart';
|
|
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
|
|
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
|
|
import 'package:esys_flutter_share/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 PrinterBluetoothManager printerManager = PrinterBluetoothManager();
|
|
final DialogService _dialogService = locator<DialogService>();
|
|
final BluetoothDevice printerBtDevice = Redux.store.state.settingState.printerBT;
|
|
|
|
bool _printing = false;
|
|
|
|
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 = 50;
|
|
|
|
if(isIos){
|
|
chunkSizeBytes = 75;
|
|
queueSleepTimeMs = 10;
|
|
}
|
|
|
|
setState(() {
|
|
_printing = true;
|
|
});
|
|
try {
|
|
printerManager.selectPrinter(PrinterBluetooth(state.printerBT));
|
|
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80
|
|
? PaperSize.mm80
|
|
: PaperSize.mm58;
|
|
if (SettingPrinterEncodingImage == state.printerEncoding) {
|
|
final PosPrintResult res = await printerManager.printTicket(
|
|
await printImageCheck(paper, widget.showModel.data.base64Data),
|
|
chunkSizeBytes: chunkSizeBytes,
|
|
queueSleepTimeMs: queueSleepTimeMs);
|
|
if (res.value != 1) {
|
|
_dialogService.showDialog(description: res.msg);
|
|
}
|
|
} else {
|
|
final PosPrintResult res = await printerManager.printTicket(
|
|
await printTextCheck(
|
|
paper, state.printerEncoding,
|
|
jsonDecode(widget.showModel.data.textData)),
|
|
chunkSizeBytes: chunkSizeBytes,
|
|
queueSleepTimeMs: queueSleepTimeMs);
|
|
if (res.value != 1) {
|
|
_dialogService.showDialog(description: res.msg);
|
|
}
|
|
}
|
|
} finally {
|
|
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: _print)
|
|
],
|
|
),
|
|
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;
|
|
final CardData cardData;
|
|
final Voucher voucher;
|
|
|
|
ImageShowModel({this.data, this.title, this.url, this.cardData, this.voucher});
|
|
}
|
|
|
|
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,
|
|
),
|
|
],
|
|
)));
|
|
bottomSheetController.closed.then((value) {
|
|
showFloatingActionButton(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)}";
|
|
}
|
|
}
|
|
|
|
if (await canLaunch(url())) {
|
|
await launch(url());
|
|
} else {
|
|
_dialog.showDialog(description: 'Отсутсвует приложение WhatsApp');
|
|
}
|
|
}
|
|
|
|
void showFloatingActionButton(bool value) {
|
|
setState(() {
|
|
showFab = value;
|
|
});
|
|
}
|
|
}
|