pos_printer_lib 0.0.5
pos_printer_lib: ^0.0.5 copied to clipboard
pos printer lib
example/lib/main.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:pos_printer_lib/pos_printer_lib.dart';
import 'dart:ui' as ui;
import 'dart:math' as math;
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _numberOfPrinters = 0;
bool? _hasPermission;
final _posPrinterLibPlugin = PosPrinterLib();
String? _capturedImageBase64;
final GlobalKey _repaintBoundaryKey = GlobalKey();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
int numberOfPrinters = 0;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
_hasPermission = await _posPrinterLibPlugin.checkPrinterPermission();
if (_hasPermission != null && _hasPermission!) {
numberOfPrinters =
await _posPrinterLibPlugin.getNumberOfPrinters() ?? 0;
}
} on Exception {
numberOfPrinters = 0;
}
// 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(() {
_numberOfPrinters = numberOfPrinters;
});
}
Future<void> _captureWidget() async {
try {
RenderRepaintBoundary boundary = _repaintBoundaryKey.currentContext!
.findRenderObject() as RenderRepaintBoundary;
// Capture the widget with higher quality
ui.Image image = await boundary.toImage(
pixelRatio: 3.0); // Increased pixel ratio for better quality
// Convert to PNG with maximum quality
ByteData? byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
if (byteData != null) {
Uint8List bytes = byteData.buffer.asUint8List();
String base64String = base64Encode(bytes);
print(
"Base64 image preview: ${base64String.substring(0, math.min(100, base64String.length))}");
print("Base64 length: ${base64String.length}");
print("Base64 image: $base64String");
setState(() {
_capturedImageBase64 = base64String; // Store the base64 string
});
await _posPrinterLibPlugin.printReceipt(base64String);
// Verify the image starts with PNG header in base64
if (!base64String.startsWith("iVBORw0KGgo")) {
print(
"Warning: Base64 string doesn't appear to be a valid PNG image");
}
}
} catch (e) {
print("Error capturing widget: $e");
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin printer example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Number of printers: $_numberOfPrinters \nHas permission: $_hasPermission'),
RepaintBoundary(
key: _repaintBoundaryKey,
child: Container(
color: Colors.white,
padding: EdgeInsets.all(16),
width: 300,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'INVOICE',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
Text(
'#INV-001',
style: TextStyle(
fontSize: 16,
),
),
],
),
SizedBox(height: 20),
Text(
'Date: ${DateTime.now().toString().split(' ')[0]}',
style: TextStyle(fontSize: 14),
),
SizedBox(height: 20),
Table(
border: TableBorder.all(),
children: [
TableRow(
children: [
Padding(
padding: EdgeInsets.all(8),
child: Text('Item',
style:
TextStyle(fontWeight: FontWeight.bold)),
),
Padding(
padding: EdgeInsets.all(8),
child: Text('Qty',
style:
TextStyle(fontWeight: FontWeight.bold)),
),
Padding(
padding: EdgeInsets.all(8),
child: Text('Price',
style:
TextStyle(fontWeight: FontWeight.bold)),
),
],
),
TableRow(
children: [
Padding(
padding: EdgeInsets.all(8),
child: Text('Item 1'),
),
Padding(
padding: EdgeInsets.all(8),
child: Text('2'),
),
Padding(
padding: EdgeInsets.all(8),
child: Text('\$10.00'),
),
],
),
],
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
'Total: \$20.00',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
],
),
],
),
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.greenAccent,
),
onPressed: _captureWidget,
child: Text("Print"),
),
// if (_capturedImageBase64 != null)
// Image.memory(
// base64Decode(_capturedImageBase64!),
// height: 200,
// ),
],
),
),
),
);
}
}