This commit is contained in:
Developer
2026-05-18 17:52:09 +08:00
commit cae04eead5
62 changed files with 11230 additions and 0 deletions

View File

@@ -0,0 +1,339 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'enums/multi_byte_encoding.dart';
import 'enums/printer_alignment.dart';
import 'enums/serial_flow_control.dart';
import 'enums/serial_parity.dart';
import 'enums/serial_stop_bits.dart';
import 'models/printer_exception.dart';
import 'printer_platform_interface.dart';
/// An implementation of [PrinterPlatform] that uses method channels.
class MethodChannelPrinter extends PrinterPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel('printer');
@override
Future<String?> getPlatformVersion() async {
final version = await methodChannel.invokeMethod<String>(
'getPlatformVersion',
);
return version;
}
@override
Future<int> openComPort({
required String portName,
required int baudRate,
int dataBits = 8,
SerialParity parity = SerialParity.none,
SerialStopBits stopBits = SerialStopBits.one,
SerialFlowControl flowControl = SerialFlowControl.none,
bool autoReplyMode = true,
}) async {
try {
final result = await methodChannel.invokeMethod<int>('openComPort', {
'portName': portName,
'baudRate': baudRate,
'dataBits': dataBits,
'parity': parity.value,
'stopBits': stopBits.value,
'flowControl': flowControl.value,
'autoReplyMode': autoReplyMode ? 1 : 0,
});
return result!;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to open COM port',
details: e.details,
);
}
}
@override
Future<int> openUsbPort({
required String portName,
bool autoReplyMode = true,
}) async {
try {
final result = await methodChannel.invokeMethod<int>('openUsbPort', {
'portName': portName,
'autoReplyMode': autoReplyMode ? 1 : 0,
});
return result!;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to open USB port',
details: e.details,
);
}
}
@override
Future<bool> closePort(int handle) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'closePort',
{'handle': handle},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to close port',
details: e.details,
);
}
}
@override
Future<bool> isPortOpened(int handle) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'isPortOpened',
{'handle': handle},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to check port status',
details: e.details,
);
}
}
@override
Future<List<String>> enumComPorts() async {
try {
final result = await methodChannel.invokeMethod<List>('enumComPorts');
return (result ?? []).map((e) => e.toString()).toList();
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to enumerate COM ports',
details: e.details,
);
}
}
@override
Future<List<String>> enumUsbPorts() async {
try {
final result = await methodChannel.invokeMethod<List>('enumUsbPorts');
return (result ?? []).map((e) => e.toString()).toList();
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to enumerate USB ports',
details: e.details,
);
}
}
/// Sets the printer to multi-byte encoding mode.
@override
Future<bool> setMultiByteMode(int handle) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'setMultiByteMode',
{'handle': handle},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to set multi-byte mode',
details: e.details,
);
}
}
/// Sets the multi-byte character encoding.
@override
Future<bool> setMultiByteEncoding(int handle, MultiByteEncoding encoding) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'setMultiByteEncoding',
{'handle': handle, 'encoding': encoding.value},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to set multi-byte encoding',
details: e.details,
);
}
}
/// Prints text using UTF-8 encoding.
@override
Future<bool> printText(int handle, String text) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'printText',
{'handle': handle, 'text': text},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to print text',
details: e.details,
);
}
}
/// Sets text alignment.
@override
Future<bool> setAlignment(int handle, PrinterAlignment alignment) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'setAlignment',
{'handle': handle, 'alignment': alignment.value},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to set alignment',
details: e.details,
);
}
}
/// Sets text scale (width and height magnification).
@override
Future<bool> setTextScale(int handle, {required int widthScale, required int heightScale}) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'setTextScale',
{
'handle': handle,
'widthScale': widthScale,
'heightScale': heightScale,
},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to set text scale',
details: e.details,
);
}
}
/// Sets text bold on or off.
@override
Future<bool> setTextBold(int handle, bool bold) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'setTextBold',
{'handle': handle, 'bold': bold ? 1 : 0},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to set text bold',
details: e.details,
);
}
}
/// Sets text underline level.
@override
Future<bool> setTextUnderline(int handle, int underline) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'setTextUnderline',
{'handle': handle, 'underline': underline},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to set text underline',
details: e.details,
);
}
}
/// Feeds paper by specified number of lines.
@override
Future<bool> feedLine(int handle, int numLines) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'feedLine',
{'handle': handle, 'numLines': numLines},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to feed line',
details: e.details,
);
}
}
/// Feeds paper by specified number of dots.
@override
Future<bool> feedDot(int handle, int numDots) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'feedDot',
{'handle': handle, 'numDots': numDots},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to feed dot',
details: e.details,
);
}
}
/// Performs a half cut of the paper.
@override
Future<bool> halfCutPaper(int handle) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'halfCutPaper',
{'handle': handle},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to half cut paper',
details: e.details,
);
}
}
/// Performs a full cut of the paper.
@override
Future<bool> fullCutPaper(int handle) async {
try {
final result = await methodChannel.invokeMethod<bool>(
'fullCutPaper',
{'handle': handle},
);
return result ?? false;
} on PlatformException catch (e) {
throw PrinterException(
code: e.code,
message: e.message ?? 'Failed to full cut paper',
details: e.details,
);
}
}
}