flutter_jc_printer_plugin/lib/jc_printer_method_channel.dart
2023-11-25 14:09:44 +08:00

186 lines
4.3 KiB
Dart

import 'package:flutter/services.dart';
import 'jc_printer_platform_interface.dart';
class MethodChannelJcPrinter extends JcPrinterPlatform {
final method = const MethodChannel('jc_printer');
final event = const EventChannel('printer_stream');
@override
Future<void> connect(String name) async {
return method.invokeMethod<void>('connect', name);
}
@override
Future<void> disconnect() async {
return method.invokeMethod<void>('disconnect');
}
@override
Future<void> setTotalPrints(int page) async {
return method.invokeMethod<void>('setTotalPrints', page);
}
@override
Future<void> initDrawingBoard({
required double width,
required double height,
required String font,
double horizontalShift = 0,
double verticalShift = 0,
int rotate = 0,
}) async {
final Map arguments = {
'width': width,
'height': height,
'horizontalShift': horizontalShift,
'verticalShift': verticalShift,
'rotate': rotate,
'font': font,
};
await method.invokeMethod<void>('initDrawingBoard', arguments);
}
@override
Future<bool> drawLabelImage({
required double x,
required double y,
required double width,
required double height,
required String base64,
int rotate = 0,
int type = 1,
double threshold = 127,
}) async {
final Map arguments = {
'x': x,
'y': y,
'w': width,
'h': height,
'base64': base64,
'rotate': rotate,
'type': type,
'threshold': threshold,
};
final result = await method.invokeMethod<bool>(
'drawLabelImage',
arguments,
);
return result ?? false;
}
@override
Future<bool> drawLabelText({
required double x,
required double y,
required double width,
required double height,
required String content,
required double fontSize,
int rotate = 0,
int textAlignHorizontal = 0,
int textAlignVertical = 0,
int lineMode = 1,
double letterSpacing = 0,
double lineSpacing = 0,
}) async {
final Map arguments = {
'x': x,
'y': y,
'width': width,
'height': height,
'content': content,
'fontSize': fontSize,
'rotate': rotate,
'textAlignHorizontal': textAlignHorizontal,
'textAlignVertical': textAlignVertical,
'lineMode': lineMode,
'letterSpacing': letterSpacing,
'lineSpacing': lineSpacing,
};
final result = await method.invokeMethod<bool>('drawLabelText', arguments);
return result ?? false;
}
@override
Future<bool> drawLabelBarcode({
required double x,
required double y,
required double width,
required double height,
required String text,
required double textHeight,
required double fontSize,
int rotate = 0,
int codeType = 20,
int textPosition = 0,
}) async {
final Map arguments = {
'x': x,
'y': y,
'width': width,
'height': height,
'text': text,
'fontSize': fontSize,
'rotate': rotate,
'textHeight': textHeight,
'codeType': codeType,
'textPosition': textPosition,
};
final result = await method.invokeMethod<bool>(
'drawLabelBarcode',
arguments,
);
return result ?? false;
}
@override
Future<String> getLabelData() async {
final result = await method.invokeMethod<String>('getLabelData');
return result ?? '';
}
@override
Future<bool> startJob({
required int blackRules,
required int paperStyle,
}) async {
final Map arguments = {
'blackRules': blackRules,
'paperStyle': paperStyle,
};
final result = await method.invokeMethod<bool>('startJob', arguments);
return result ?? false;
}
@override
Future<bool> cancelJob() async {
final result = await method.invokeMethod<bool>('cancelJob');
return result ?? false;
}
@override
Future<bool> endPrint() async {
final result = await method.invokeMethod<bool>('endPrint');
return result ?? false;
}
@override
Future<bool> commit({
required String data,
int count = 1,
}) async {
final Map arguments = {
'data': data,
'count': count,
};
final result = await method.invokeMethod<bool>(
'commit',
arguments,
);
return result ?? false;
}
@override
Stream get printerStream => event.receiveBroadcastStream();
}