tsc_blue_print 0.0.1 tsc_blue_print: ^0.0.1 copied to clipboard
Impresion TSC Bluetooh / Network
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 = true;
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 {
print(mac);
final String? result = await BluetoothThermalPrinter.connect(mac);
print("state conneected $result");
if (result == "true") {
setState(() {
connected = true;
});
}
}
Future<void> inbound() async {
String printMessage;
String idAddress = device;
String product = "Prueba";
print(idAddress);
try {
printMessage = await _tscBluePrintPlugin.print_blue(
ipAddress: "20:20:11:24:36:15",
tara: "0.58",
bruto: "11.81",
fecha_prod: "03/22/2020",
peso: "11.23",
vendor: "PEC. SAN FRANCISCO(MAIZALES)",
expiryDate: '03/22/2020',
productCode: 'FV-VOS-EDFLMG',
num: 1
);
} 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"),
),
],
),
),
),
),
);
}
}