flutter_usb_printer 0.3.0
flutter_usb_printer: ^0.3.0 copied to clipboard
This plugin will allow develop send data and work with usb printer on android
example/lib/main.dart
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_usb_printer/flutter_usb_printer.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Map<String, dynamic>> devices = [];
FlutterUsbPrinter flutterUsbPrinter = FlutterUsbPrinter();
bool connected = false;
@override
initState() {
super.initState();
_getDevicelist();
}
_getDevicelist() async {
List<Map<String, dynamic>> results = [];
results = await FlutterUsbPrinter.getUSBDeviceList();
print(" length: ${results.length}");
setState(() {
devices = results;
});
}
_connect(int vendorId, int productId) async {
bool? returned = false;
try {
returned = await flutterUsbPrinter.connect(vendorId, productId);
} on PlatformException {
//response = 'Failed to get platform version.';
}
if (returned!) {
setState(() {
connected = true;
});
}
}
_print() async {
try {
var data = Uint8List.fromList(
utf8.encode(" Hello world Testing ESC POS printer...\n\n\n"));
await flutterUsbPrinter.write(data);
} on PlatformException {
//response = 'Failed to get platform version.';
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('USB PRINTER'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () => _getDevicelist()),
connected == true
? IconButton(
icon: const Icon(Icons.print),
onPressed: () {
_print();
})
: const SizedBox.shrink(),
],
),
body: devices.isNotEmpty
? ListView(
scrollDirection: Axis.vertical,
children: _buildList(devices),
)
: null,
),
);
}
List<Widget> _buildList(List<Map<String, dynamic>> devices) {
return devices
.map((device) => ListTile(
onTap: () {
_connect(int.parse(device['vendorId']),
int.parse(device['productId']));
},
leading: const Icon(Icons.usb),
title: Text(
"${device['manufacturer'] ?? ''} ${device['productName'] ?? ''}".trim()),
subtitle:
Text("${device['vendorId'] ?? ''} ${device['productId'] ?? ''}".trim()),
))
.toList();
}
}