fajfield_print_flutter 0.1.0
fajfield_print_flutter: ^0.1.0 copied to clipboard
Flutter add-on for fajfield_print. Prints text in any language with the phone's fonts, plus a print preview and a debug panel.
import 'package:fajfield_print_flutter/fajfield_print_flutter.dart';
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Demo()));
class Demo extends StatefulWidget {
const Demo({super.key});
@override
State<Demo> createState() => _DemoState();
}
class _DemoState extends State<Demo> {
final log = PrintLogger.console(LogLevel.debug);
late final printer = FajPrinter.network('192.168.1.50', paper: Paper.mm110, text: FlutterTextRenderer(), logger: log);
PrintJobResult? job;
String status = '';
final template = PrintTemplate.receipt(Paper.mm110)
..centered('{{shop}}', y: 0, size: 36, bold: true)
..centered('നന്ദി! Thank you', y: 44, size: 28)
..line(y: 84)
..table(rows: 'items', y: 94, columns: [
{'title': 'Item', 'field': 'name', 'wrap': true},
{'title': 'Qty', 'field': 'qty', 'width': 100, 'align': 'R'},
{'title': 'Amount', 'field': 'amount', 'width': 180, 'align': 'R'},
])
..line(y: 170)
..text('Total ₹{{total}}', x: 0, y: 180, size: 32, bold: true)
..qr('upi://pay?pa={{upi}}&am={{total}}', x: 600, y: 180, size: 5);
final data = {
'shop': 'Fresh Mart',
'upi': 'freshmart@upi',
'total': '310.00',
'items': [
{'name': 'ചായപ്പൊടി 250g', 'qty': 2, 'amount': '180.00'},
{'name': 'Coconut oil 1L', 'qty': 1, 'amount': '130.00'},
],
};
Future<void> run(Future<String?> Function() f) async {
setState(() => status = 'Working…');
try {
final msg = await f();
setState(() => status = msg ?? '');
} on PrintException catch (e) {
setState(() => status = '${e.message}\n→ ${e.hint}');
}
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Fajfield print demo')),
body: Column(children: [
Wrap(spacing: 8, children: [
FilledButton(onPressed: () => run(() async => (await discoverPrinters(logger: log)).join('\n')), child: const Text('Find printers')),
FilledButton(onPressed: () => run(() => printer.diagnose()), child: const Text('Check printer')),
FilledButton(onPressed: () => run(() async { job = await printer.print(printer.testPage()); return 'Test page sent'; }), child: const Text('Test page')),
FilledButton(onPressed: () => run(() async { job = await printer.print(template, data: data); return job!.hasWarnings ? 'Printed with warnings' : 'Printed'; }), child: const Text('Print receipt')),
]),
if (status.isNotEmpty) Padding(padding: const EdgeInsets.all(8), child: Text(status)),
Expanded(child: job == null ? const Center(child: Text('Print something to see the debug panel')) : FajDebugPanel(job: job!)),
]),
);
}