tsc_blue_print 0.1.1 tsc_blue_print: ^0.1.1 copied to clipboard
Es un plugin, desarrollado para flutter con compatibilidad para Android y IOS para poder interactuar con las impresoras de ticket marca TSC.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:tsc_blue_print/tsc_blue_print.dart';
import 'package:bluetooth_thermal_printer/bluetooth_thermal_printer.dart';
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> {
String _platformVersion = 'Unknown';
final _tscBluePrintPlugin = TscBluePrint();
String device = "";
bool connected = false;
List availableBluetoothDevices = [];
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await _tscBluePrintPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> getBluetooth() async {
final List? bluetooths = await BluetoothThermalPrinter.getBluetooths;
print("Print $bluetooths");
setState(() {
availableBluetoothDevices = bluetooths == null ? [] : bluetooths;
});
}
Future<void> setConnect(String mac) async {
//final String? result = await BluetoothThermalPrinter.connect(mac);
device = mac;
print("state conneected $device");
setState(() {
connected = true;
});
}
Future<void> inbound() async {
String printMessage;
String idAddress = device;
print(idAddress);
try {
printMessage = await _tscBluePrintPlugin.printBlue(
ipAddress: device,
widthPaper: 47,
heightPaper: 25,
numero: '0001',
tara: "0.58",
bruto: "11.81",
peso: "11.23",
vendor: "PEC. SAN FRANCISCO(MAIZALES)",
expiryDate: '03/22/2020',
productCode: '000000001',
productName: 'HUEVO PARDO');
} on PlatformException {
printMessage = 'Failed to communcate with this printer. ';
}
print(printMessage);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Scaffold(
appBar: AppBar(
title: Text("Test de Impresion TSC",
style: TextStyle(color: Colors.brown)),
centerTitle: true,
backgroundColor: Colors.white,
iconTheme: IconThemeData(color: Colors.brown),
actions: [],
),
body: Container(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Search Paired Bluetooth"),
TextButton(
onPressed: () {
this.getBluetooth();
},
child: Text("Search"),
),
Container(
height: 200,
child: ListView.builder(
itemCount: availableBluetoothDevices.length > 0
? availableBluetoothDevices.length
: 0,
itemBuilder: (context, index) {
return ListTile(
onTap: () {
String select = availableBluetoothDevices[index];
List list = select.split("#");
// String name = list[0];
String mac = list[1];
this.setConnect(mac);
},
title: Text('${availableBluetoothDevices[index]}'),
subtitle: Text("Click to connect"),
);
},
),
),
SizedBox(
height: 30,
),
TextButton(
onPressed: connected ? this.inbound : null,
child: Text("Print"),
),
],
),
),
),
),
);
}
}