单例模式

master
张萌 3 years ago
parent bef52be875
commit d5783fa4ab

@ -3,7 +3,10 @@
<component name="ChangeListManager">
<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$/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$/pubspec.lock" beforeDir="false" afterPath="$PROJECT_DIR$/pubspec.lock" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -48,7 +51,6 @@
</component>
<component name="PropertiesComponent">
<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="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="flutter.settings" />

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

@ -102,6 +102,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
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:
dependency: transitive
description:
@ -204,7 +211,7 @@ packages:
name: test_api
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.4.3"
version: "0.4.8"
typed_data:
dependency: transitive
description:

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

@ -81,6 +81,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
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:
dependency: transitive
description:
@ -141,7 +148,7 @@ packages:
name: test_api
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.4.3"
version: "0.4.8"
typed_data:
dependency: transitive
description:

Loading…
Cancel
Save