You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.0 KiB

3 years ago
import 'dart:async';
3 years ago
import 'dart:convert';
3 years ago
import 'package:dj_printer/dj_printer.dart';
3 years ago
import 'package:dj_printer/src/device_model.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
3 years ago
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
3 years ago
List<Device> devices = [];
3 years ago
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
3 years ago
var per = await Permission.bluetooth.isGranted;
if (!per) {
Permission.bluetooth.request();
3 years ago
}
3 years ago
var pers = await Permission.locationWhenInUse.isGranted;
if (!pers) {
Permission.locationWhenInUse.request();
}
DjPrinter.init();
DjPrinter.addDiscoveryListen(onReceive: (data) {
var js = json.decode(data.toString());
devices.add(Device(
name: js['name'], address: js['address'], isPaired: js['isPaired']));
setState(() {});
}, onStart: () {
print("————————————————————————");
}, onFinish: () {
print('——————————————————————————————');
DjPrinter.cancelDiscovery();
});
DjPrinter.addConnectListen(onConnect: () {
print("connected");
}, onDisconnect: () {
print('disconnected');
3 years ago
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
3 years ago
child: Column(
children: [
TextButton(
onPressed: () {
devices.clear();
DjPrinter.startSearch;
},
child: const Text('扫描设备')),
// TextButton(onPressed: () {}, child: const Text('打印')),
const SizedBox(
height: 20,
),
...devices
.map((e) => TextButton(
onPressed: () {
DjPrinter.connect(e.address);
},
child: Text(e.name)))
.toList(),
const SizedBox(
height: 20,
),
TextButton(
onPressed: () {
DjPrinter.print(
code: 'ASSZ2022012500010002',
channel: 'cosco定提-月达-卡派',
country: '美国',
countStr: '10/20',
offset: 0,
hasPlan: false);
},
child: const Text('打印'))
],
),
3 years ago
),
),
);
}
}