单例模式

master
张萌 3 years ago
parent bef52be875
commit d5783fa4ab

@ -3,7 +3,10 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="2f8e3712-04e2-429a-bcea-c8db1a54008e" name="默认变更列表" comment=""> <list default="true" id="2f8e3712-04e2-429a-bcea-c8db1a54008e" name="默认变更列表" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/example/lib/main.dart" beforeDir="false" afterPath="$PROJECT_DIR$/example/lib/main.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/example/pubspec.lock" beforeDir="false" afterPath="$PROJECT_DIR$/example/pubspec.lock" afterDir="false" />
<change beforePath="$PROJECT_DIR$/lib/dj_printer.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/dj_printer.dart" afterDir="false" /> <change beforePath="$PROJECT_DIR$/lib/dj_printer.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/dj_printer.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pubspec.lock" beforeDir="false" afterPath="$PROJECT_DIR$/pubspec.lock" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -48,7 +51,6 @@
</component> </component>
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="dart.analysis.tool.window.force.activate" value="true" /> <property name="dart.analysis.tool.window.force.activate" value="true" />
<property name="dart.analysis.tool.window.visible" value="false" />
<property name="io.flutter.reload.alreadyRun" value="true" /> <property name="io.flutter.reload.alreadyRun" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" /> <property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="flutter.settings" /> <property name="settings.editor.selected.configurable" value="flutter.settings" />

@ -36,8 +36,8 @@ class _MyAppState extends State<MyApp> {
if (!pers) { if (!pers) {
Permission.locationWhenInUse.request(); Permission.locationWhenInUse.request();
} }
DjPrinter.init(); DjPrinter().init();
DjPrinter.addDiscoveryListen(onReceive: (data) { DjPrinter().addDiscoveryListen(onReceive: (data) {
var js = json.decode(data.toString()); var js = json.decode(data.toString());
devices.add(Device( devices.add(Device(
name: js['name'], address: js['address'], isPaired: js['isPaired'])); name: js['name'], address: js['address'], isPaired: js['isPaired']));
@ -46,9 +46,9 @@ class _MyAppState extends State<MyApp> {
print("————————————————————————"); print("————————————————————————");
}, onFinish: () { }, onFinish: () {
print('——————————————————————————————'); print('——————————————————————————————');
DjPrinter.cancelDiscovery(); DjPrinter().cancelDiscovery();
}); });
DjPrinter.addConnectListen(onConnect: () { DjPrinter().addConnectListen(onConnect: () {
print("connected"); print("connected");
}, onDisconnect: () { }, onDisconnect: () {
print('disconnected'); print('disconnected');
@ -68,7 +68,7 @@ class _MyAppState extends State<MyApp> {
TextButton( TextButton(
onPressed: () { onPressed: () {
devices.clear(); devices.clear();
DjPrinter.startSearch; DjPrinter().startSearch;
}, },
child: const Text('扫描设备')), child: const Text('扫描设备')),
// TextButton(onPressed: () {}, child: const Text('打印')), // TextButton(onPressed: () {}, child: const Text('打印')),
@ -78,7 +78,7 @@ class _MyAppState extends State<MyApp> {
...devices ...devices
.map((e) => TextButton( .map((e) => TextButton(
onPressed: () { onPressed: () {
DjPrinter.connect(e.address); DjPrinter().connect(e.address);
}, },
child: Text(e.name))) child: Text(e.name)))
.toList(), .toList(),
@ -87,7 +87,7 @@ class _MyAppState extends State<MyApp> {
), ),
TextButton( TextButton(
onPressed: () { onPressed: () {
DjPrinter.print( DjPrinter().print(
code: 'ASSZ2022012500010002', code: 'ASSZ2022012500010002',
channel: 'cosco定提-月达-卡派', channel: 'cosco定提-月达-卡派',
country: '美国', country: '美国',

@ -102,6 +102,13 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "0.12.11" version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.2"
meta: meta:
dependency: transitive dependency: transitive
description: description:
@ -204,7 +211,7 @@ packages:
name: test_api name: test_api
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "0.4.3" version: "0.4.8"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:

@ -12,9 +12,9 @@ class DjPrinter {
static const MethodChannel _channel = MethodChannel('dj_printer'); static const MethodChannel _channel = MethodChannel('dj_printer');
static const EventChannel _deviceChannel = static const EventChannel _deviceChannel =
EventChannel("com.discovery.devices"); EventChannel("com.discovery.devices");
static StreamSubscription? _discoveryStream; StreamSubscription? _discoveryStream;
static Future<StreamSubscription> addDiscoveryListen( Future<StreamSubscription> addDiscoveryListen(
{required void Function(dynamic data) onReceive, {required void Function(dynamic data) onReceive,
void Function()? onStart, void Function()? onStart,
void Function()? onFinish}) async { void Function()? onFinish}) async {
@ -33,7 +33,7 @@ class DjPrinter {
} }
} }
static void cancelDiscovery() { void cancelDiscovery() {
if (_discoveryStream != null) { if (_discoveryStream != null) {
_discoveryStream!.cancel(); _discoveryStream!.cancel();
_discoveryStream = null; _discoveryStream = null;
@ -41,9 +41,9 @@ class DjPrinter {
} }
static const EventChannel _connectChannel = EventChannel("com.connect"); static const EventChannel _connectChannel = EventChannel("com.connect");
static StreamSubscription? _connectStream; StreamSubscription? _connectStream;
static Future<StreamSubscription> addConnectListen( Future<StreamSubscription> addConnectListen(
{required void Function() onConnect, {required void Function() onConnect,
required void Function() onDisconnect}) async { required void Function() onDisconnect}) async {
if (_connectStream == null) { if (_connectStream == null) {
@ -59,24 +59,24 @@ class DjPrinter {
} }
} }
static void cancelConnect() { void cancelConnect() {
if (_connectStream != null) { if (_connectStream != null) {
_connectStream!.cancel(); _connectStream!.cancel();
_connectStream = null; _connectStream = null;
} }
} }
static Future<bool?> get startSearch async { Future<bool?> get startSearch async {
final res = await _channel.invokeMethod('startSearch'); final res = await _channel.invokeMethod('startSearch');
return res; return res;
} }
static Future<bool?> connect(String address) async { Future<bool?> connect(String address) async {
final res = await _channel.invokeMethod('connect', {'address': address}); final res = await _channel.invokeMethod('connect', {'address': address});
return res; return res;
} }
static Future<bool?> init() async { Future<bool?> init() async {
final res = await _channel.invokeMethod('init'); final res = await _channel.invokeMethod('init');
return res; return res;
} }
@ -86,7 +86,7 @@ class DjPrinter {
//2 paper empty //2 paper empty
//4 cover open //4 cover open
//8 battery low //8 battery low
static Future<PRINT_STATUS?> getStatus() async { Future<PRINT_STATUS?> getStatus() async {
final res = await _channel.invokeMethod('getStatus'); final res = await _channel.invokeMethod('getStatus');
switch (res) { switch (res) {
case 0: case 0:
@ -104,7 +104,7 @@ class DjPrinter {
} }
} }
static Future<bool?> print( Future<bool?> print(
{required String code, {required String code,
required String channel, required String channel,
required String country, required String country,

@ -81,6 +81,13 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "0.12.11" version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.2"
meta: meta:
dependency: transitive dependency: transitive
description: description:
@ -141,7 +148,7 @@ packages:
name: test_api name: test_api
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "0.4.3" version: "0.4.8"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:

Loading…
Cancel
Save