print_usb 0.0.3 print_usb: ^0.0.3 copied to clipboard
Print bytes in usb windows
import 'package:flutter/material.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> {
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 PrintUsb.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 PrintUsb.connect(name: device.name);
if (result) {
_connectedDevice = device;
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 PrintUsb.printBytes(bytes: bytes, device: device);
if (result) {
print("Printed");
}
} else {
print("Device not available");
}
},
icon: const Icon(Icons.print),
)
: const SizedBox(),
);
},
),
),
],
),
),
),
),
);
}
}