This commit is contained in:
歪脖子 2023-10-20 17:45:24 +08:00
parent 6049c0fac0
commit 1a2a594e1d
6 changed files with 30 additions and 34 deletions

View File

@ -1,7 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:async'; import 'dart:async';
import 'package:flutter/services.dart';
import 'package:jc_printer/jc_printer.dart'; import 'package:jc_printer/jc_printer.dart';
void main() { void main() {
@ -16,35 +15,12 @@ class MyApp extends StatefulWidget {
} }
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown'; final _jcPrinter = JcPrinter();
final _jcPrinterPlugin = JcPrinter();
@override @override
void initState() { void initState() {
super.initState(); super.initState();
initPlatformState(); _jcPrinter.connect();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await _jcPrinterPlugin.getPlatformVersion() ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
} }
@override @override
@ -54,9 +30,6 @@ class _MyAppState extends State<MyApp> {
appBar: AppBar( appBar: AppBar(
title: const Text('Plugin example app'), title: const Text('Plugin example app'),
), ),
body: Center(
child: Text('Running on: $_platformVersion\n'),
),
), ),
); );
} }

View File

@ -1,4 +1,5 @@
#import "JcPrinterPlugin.h" #import "JcPrinterPlugin.h"
#import "JCAPI.h"
@implementation JcPrinterPlugin @implementation JcPrinterPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar { + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
@ -12,6 +13,14 @@
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"getPlatformVersion" isEqualToString:call.method]) { if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]); result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
} else if ([@"connect" isEqualToString:call.method]) {
[JCAPI openPrinter:@"123" completion:^(BOOL isSuccess) {
if (isSuccess) {
result(@"啊叫啊");
} else {
result(@"版本");
}
}];
} else { } else {
result(FlutterMethodNotImplemented); result(FlutterMethodNotImplemented);
} }

View File

@ -5,4 +5,8 @@ class JcPrinter {
Future<String?> getPlatformVersion() { Future<String?> getPlatformVersion() {
return JcPrinterPlatform.instance.getPlatformVersion(); return JcPrinterPlatform.instance.getPlatformVersion();
} }
void connect() {
JcPrinterPlatform.instance.connect();
}
} }

View File

@ -14,4 +14,10 @@ class MethodChannelJcPrinter extends JcPrinterPlatform {
final version = await methodChannel.invokeMethod<String>('getPlatformVersion'); final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
return version; return version;
} }
@override
void connect() async {
final result = await methodChannel.invokeMethod<String>('connect');
print(result);
}
} }

View File

@ -26,4 +26,8 @@ abstract class JcPrinterPlatform extends PlatformInterface {
Future<String?> getPlatformVersion() { Future<String?> getPlatformVersion() {
throw UnimplementedError('platformVersion() has not been implemented.'); throw UnimplementedError('platformVersion() has not been implemented.');
} }
void connect() {
throw UnimplementedError('connect() has not been implemented.');
}
} }

View File

@ -2,15 +2,15 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:jc_printer/jc_printer.dart'; import 'package:jc_printer/jc_printer.dart';
import 'package:jc_printer/jc_printer_platform_interface.dart'; import 'package:jc_printer/jc_printer_platform_interface.dart';
import 'package:jc_printer/jc_printer_method_channel.dart'; import 'package:jc_printer/jc_printer_method_channel.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart'; // import 'package:plugin_platform_interface/plugin_platform_interface.dart';
class MockJcPrinterPlatform /*class MockJcPrinterPlatform
with MockPlatformInterfaceMixin with MockPlatformInterfaceMixin
implements JcPrinterPlatform { implements JcPrinterPlatform {
@override @override
Future<String?> getPlatformVersion() => Future.value('42'); Future<String?> getPlatformVersion() => Future.value('42');
} }*/
void main() { void main() {
final JcPrinterPlatform initialPlatform = JcPrinterPlatform.instance; final JcPrinterPlatform initialPlatform = JcPrinterPlatform.instance;
@ -21,8 +21,8 @@ void main() {
test('getPlatformVersion', () async { test('getPlatformVersion', () async {
JcPrinter jcPrinterPlugin = JcPrinter(); JcPrinter jcPrinterPlugin = JcPrinter();
MockJcPrinterPlatform fakePlatform = MockJcPrinterPlatform(); // MockJcPrinterPlatform fakePlatform = MockJcPrinterPlatform();
JcPrinterPlatform.instance = fakePlatform; // JcPrinterPlatform.instance = fakePlatform;
expect(await jcPrinterPlugin.getPlatformVersion(), '42'); expect(await jcPrinterPlugin.getPlatformVersion(), '42');
}); });