-
Notifications
You must be signed in to change notification settings - Fork 196
/
main.dart
233 lines (209 loc) · 10.1 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import 'dart:async';
import 'dart:convert';
import 'package:bluetooth_print/bluetooth_print.dart';
import 'package:bluetooth_print/bluetooth_print_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
BluetoothPrint bluetoothPrint = BluetoothPrint.instance;
bool _connected = false;
BluetoothDevice? _device;
String tips = 'no device connect';
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => initBluetooth());
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initBluetooth() async {
bluetoothPrint.startScan(timeout: Duration(seconds: 4));
bool isConnected=await bluetoothPrint.isConnected??false;
bluetoothPrint.state.listen((state) {
print('******************* cur device status: $state');
switch (state) {
case BluetoothPrint.CONNECTED:
setState(() {
_connected = true;
tips = 'connect success';
});
break;
case BluetoothPrint.DISCONNECTED:
setState(() {
_connected = false;
tips = 'disconnect success';
});
break;
default:
break;
}
});
if (!mounted) return;
if(isConnected) {
setState(() {
_connected=true;
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('BluetoothPrint example app'),
),
body: RefreshIndicator(
onRefresh: () =>
bluetoothPrint.startScan(timeout: Duration(seconds: 4)),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Text(tips),
),
],
),
Divider(),
StreamBuilder<List<BluetoothDevice>>(
stream: bluetoothPrint.scanResults,
initialData: [],
builder: (c, snapshot) => Column(
children: snapshot.data!.map((d) => ListTile(
title: Text(d.name??''),
subtitle: Text(d.address??''),
onTap: () async {
setState(() {
_device = d;
});
},
trailing: _device!=null && _device!.address == d.address?Icon(
Icons.check,
color: Colors.green,
):null,
)).toList(),
),
),
Divider(),
Container(
padding: EdgeInsets.fromLTRB(20, 5, 20, 10),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
OutlinedButton(
child: Text('connect'),
onPressed: _connected?null:() async {
if(_device!=null && _device!.address !=null){
setState(() {
tips = 'connecting...';
});
await bluetoothPrint.connect(_device!);
}else{
setState(() {
tips = 'please select device';
});
print('please select device');
}
},
),
SizedBox(width: 10.0),
OutlinedButton(
child: Text('disconnect'),
onPressed: _connected?() async {
setState(() {
tips = 'disconnecting...';
});
await bluetoothPrint.disconnect();
}:null,
),
],
),
Divider(),
OutlinedButton(
child: Text('print receipt(esc)'),
onPressed: _connected?() async {
Map<String, dynamic> config = Map();
List<LineText> list = [];
list.add(LineText(type: LineText.TYPE_TEXT, content: '**********************************************', weight: 1, align: LineText.ALIGN_CENTER,linefeed: 1));
list.add(LineText(type: LineText.TYPE_TEXT, content: '打印单据头', weight: 1, align: LineText.ALIGN_CENTER, fontZoom: 2, linefeed: 1));
list.add(LineText(linefeed: 1));
list.add(LineText(type: LineText.TYPE_TEXT, content: '物资名称规格型号', align: LineText.ALIGN_LEFT, absolutePos: 0,relativePos: 0, linefeed: 0));
list.add(LineText(type: LineText.TYPE_TEXT, content: '单位', align: LineText.ALIGN_LEFT, absolutePos: 350, relativePos: 0, linefeed: 0));
list.add(LineText(type: LineText.TYPE_TEXT, content: '数量', align: LineText.ALIGN_LEFT, absolutePos: 500, relativePos: 0, linefeed: 1));
list.add(LineText(type: LineText.TYPE_TEXT, content: '混凝土C30', align: LineText.ALIGN_LEFT, absolutePos: 0,relativePos: 0, linefeed: 0));
list.add(LineText(type: LineText.TYPE_TEXT, content: '吨', align: LineText.ALIGN_LEFT, absolutePos: 350, relativePos: 0, linefeed: 0));
list.add(LineText(type: LineText.TYPE_TEXT, content: '12.0', align: LineText.ALIGN_LEFT, absolutePos: 500, relativePos: 0, linefeed: 1));
list.add(LineText(type: LineText.TYPE_TEXT, content: '**********************************************', weight: 1, align: LineText.ALIGN_CENTER,linefeed: 1));
list.add(LineText(linefeed: 1));
ByteData data = await rootBundle.load("assets/images/bluetooth_print.png");
List<int> imageBytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
String base64Image = base64Encode(imageBytes);
// list.add(LineText(type: LineText.TYPE_IMAGE, content: base64Image, align: LineText.ALIGN_CENTER, linefeed: 1));
await bluetoothPrint.printReceipt(config, list);
}:null,
),
OutlinedButton(
child: Text('print label(tsc)'),
onPressed: _connected?() async {
Map<String, dynamic> config = Map();
config['width'] = 40; // 标签宽度,单位mm
config['height'] = 70; // 标签高度,单位mm
config['gap'] = 2; // 标签间隔,单位mm
// x、y坐标位置,单位dpi,1mm=8dpi
List<LineText> list = [];
list.add(LineText(type: LineText.TYPE_TEXT, x:10, y:10, content: 'A Title'));
list.add(LineText(type: LineText.TYPE_TEXT, x:10, y:40, content: 'this is content'));
list.add(LineText(type: LineText.TYPE_QRCODE, x:10, y:70, content: 'qrcode i\n'));
list.add(LineText(type: LineText.TYPE_BARCODE, x:10, y:190, content: 'qrcode i\n'));
List<LineText> list1 = [];
ByteData data = await rootBundle.load("assets/images/guide3.png");
List<int> imageBytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
String base64Image = base64Encode(imageBytes);
list1.add(LineText(type: LineText.TYPE_IMAGE, x:10, y:10, content: base64Image,));
await bluetoothPrint.printLabel(config, list);
await bluetoothPrint.printLabel(config, list1);
}:null,
),
OutlinedButton(
child: Text('print selftest'),
onPressed: _connected?() async {
await bluetoothPrint.printTest();
}:null,
)
],
),
)
],
),
),
),
floatingActionButton: StreamBuilder<bool>(
stream: bluetoothPrint.isScanning,
initialData: false,
builder: (c, snapshot) {
if (snapshot.data == true) {
return FloatingActionButton(
child: Icon(Icons.stop),
onPressed: () => bluetoothPrint.stopScan(),
backgroundColor: Colors.red,
);
} else {
return FloatingActionButton(
child: Icon(Icons.search),
onPressed: () => bluetoothPrint.startScan(timeout: Duration(seconds: 4)));
}
},
),
),
);
}
}