print_usb 0.0.1 print_usb: ^0.0.1 copied to clipboard
Print bytes in usb windows
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:print_usb/model/usb_device.dart';
import 'package:print_usb/print_usb.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final PrintUsb _printUsbPlugin = PrintUsb();
List<UsbDevice> devices = [];
UsbDevice? _connectedDevice;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('PrintUsb Plugin example app'),
),
body: Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 500),
child: Column(
children: [
ElevatedButton(
onPressed: () async {
devices = await _printUsbPlugin.getList();
setState(() {});
},
child: const Text('Get List'),
),
Expanded(
child: ListView.builder(
itemCount: devices.length,
itemBuilder: (context, index) {
UsbDevice device = devices[index];
return ListTile(
onTap: () async {
bool result = await _printUsbPlugin.connect(name: device.name);
if (result) {
_connectedDevice = device;
print("Connected");
setState(() {});
}
},
title: Text(device.name),
subtitle: Text(device.model),
leading: Text(device.available.toString()),
trailing: _connectedDevice != null && _connectedDevice!.name == device.name
? IconButton(
onPressed: () async {
String paperFeed = '\x1B\x64\x04'; // x04 lines feed
String cutPaper = '\x1D\x56\x00';
if (device.available) {
List<int> bytes = "Hello developer flutter $paperFeed $cutPaper".codeUnits;
bool result = await _printUsbPlugin.printBytes(bytes);
if (result) {
print("Printed");
}
} else {
print("Device not available");
}
},
icon: const Icon(Icons.print),
)
: const SizedBox(),
);
},
),
),
],
),
),
),
),
);
}
}