bluetooth_x_print 1.0.8 bluetooth_x_print: ^1.0.8 copied to clipboard
X Printer Support for Flutter
import 'dart:async';
import 'dart:convert';
import 'package:bluetooth_x_print/bluetooth_print.dart';
import 'package:bluetooth_x_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;
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('BluetoothXPrint 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) {
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 {
await bluetoothPrint.disconnect();
}
: null,
),
],
),
OutlinedButton(
child: Text('print label(tsc)'),
onPressed: _connected
? () async {
Map<String, dynamic> config = Map();
config['width'] = 70;
config['height'] = 30;
config['gap'] = 20;
// x、y坐标位置,单位dpi,1mm=8dpi
List<LineText> list = [];
list.add(LineText(
type: LineText.TYPE_QRCODE,
x: 20,
y: 40,
size: 50,
content: "ASD",
));
list.add(LineText(
type: LineText.TYPE_TEXT,
x: 180,
y: 42,
content: "Hello World"));
list.add(LineText(
type: LineText.TYPE_TEXT,
x: 180,
y: 112,
content: "Haha" ?? "-",
));
list.add(LineText(
type: LineText.TYPE_TEXT,
x: 180,
y: 142,
content: "${"sdasdsad" ?? ""}",
));
await bluetoothPrint.printLabel(config, list);
}
: 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) {
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)));
}
},
),
),
);
}
}