import 'dart:async'; import 'dart:convert'; import 'package:barcode_widget/barcode_widget.dart'; import 'package:dj_printer/dj_printer.dart'; import 'package:dj_printer/src/device_model.dart'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override State createState() => _MyAppState(); } class _MyAppState extends State { List devices = []; @override void initState() { super.initState(); initPlatformState(); } // Platform messages are asynchronous, so we initialize in an async method. Future initPlatformState() async { var per = await Permission.bluetooth.isGranted; if (!per) { Permission.bluetooth.request(); } var pers = await Permission.locationWhenInUse.isGranted; if (!pers) { Permission.locationWhenInUse.request(); } DjPrinter().init(); DjPrinter().addDiscoveryListen(onReceive: (data) { var js = json.decode(data.toString()); if ((js['name'] as String).startsWith('3R20P')) { 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'); }); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Plugin example app'), ), body: Center( child: Column( children: [ TextButton( onPressed: () { devices.clear(); setState(() {}); 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().printNewAScode( code: 'ASSZ87658907', barCode: 'ASSZ2010000001111', channel: '加拿大温哥华海派快线-\n卡派/UPS派送', country: '伊苏尔', num: '2', sum:'20', offset: 0, hasPlan: true, ); }, child: const Text('打印')), const SizedBox( height: 20, ), TextButton( onPressed: () { DjPrinter().disposeConnect(); }, child: const Text('取消链接')), _getCard('ASSZ2022012500010002', '加拿大温哥华海派快线-卡派/UPS派送', '加拿大', '10/20', 0, true) ], ), ), ), ); } _getCard(String code, String channel, String country, String countStr, int offset, bool hasPlan) { return Container( margin: EdgeInsets.all(5), width: 90, height: 200, color: Colors.red, // decoration: BoxDecoration( // border: Border.all(color: Colors.black,width: 2) // ), child: RotatedBox( quarterTurns: 1, child: BarcodeWidget( barcode: Barcode.code128(), data: code, style: TextStyle( fontSize: 10 ), ), ) // // Row( // crossAxisAlignment: CrossAxisAlignment.center, // children: [ // Container( // width: 197 / 3.7, // height: 950 / 3.7, // decoration: BoxDecoration( // color: Colors.white, // border: Border( // right: BorderSide( // color: Colors.black, // width: 0.5, // )), // ), // alignment: Alignment.center, // child: RotatedBox( // quarterTurns: 3, // child: Text( // code, // style: TextStyle( // color: Colors.black, // fontSize: 20, // fontWeight: FontWeight.bold), // ), // ), // ), // Container( // width: 215 / 3.7, // height: 950 / 3.7, // decoration: BoxDecoration( // border: Border( // right: BorderSide( // color: Colors.black, // width: 0.5, // )), // ), // alignment: Alignment.center, // child: Column( // children: [ // Container( // width: 215 / 3.7, // height: 950 / 3.7 / 3, // decoration: BoxDecoration( // border: Border( // bottom: BorderSide( // color: Colors.black, // width: 0.5, // )), // ), // alignment: Alignment.center, // child: RotatedBox( // quarterTurns: 3, // child: Text( // country, // style: TextStyle( // color: Colors.black, // fontSize: 17, // fontWeight: FontWeight.bold), // ), // ), // ), // Container( // width: 215 / 3.7, // height: 950 / 3.7 / 3 * 2 - 4, // padding: EdgeInsets.symmetric(vertical: 10), // alignment: Alignment.center, // child: RotatedBox( // quarterTurns: 3, // child: Text( // channel, // style: TextStyle( // color: Colors.black, // fontSize: 15, // ), // ), // ), // ) // ], // )), // Container( // width: 358 / 3.7 -20, // height: 950 / 3.7, // alignment: Alignment.center, // child: Column( // children: [ // Container( // width: 358 / 3.7 -20, // height: 950 / 3.7 / 3, // decoration: BoxDecoration( // border: Border( // bottom: BorderSide( // color: Colors.black, // width: 0.5, // )), // ), // alignment: Alignment.center, // child: RotatedBox( // quarterTurns: 3, // child: Text( // countStr, // style: TextStyle( // color: Colors.black, // fontSize: 17, // fontWeight: FontWeight.bold), // ), // ), // ), // Container( // width: 358 / 3.7 -30, // height: 950 / 3.7 / 3 * 2 - 1.5, // padding: EdgeInsets.only(left: 15,top: 15,bottom: 15,right: 10) , // child: RotatedBox( // quarterTurns: 3, // child: BarcodeWidget( // // barcode: Barcode.code128(), // data: code, // style: TextStyle( // fontSize: 10 // ), // ), // ), // ) // ], // )), // ], // ), ); } }