pos_printer 0.2.6
pos_printer: ^0.2.6 copied to clipboard
A android plugin that print bill using thermal printer via bluetooth
example/lib/main.dart
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pos_printer/bill_generator.dart';
import 'package:pos_printer/device_data.dart';
import 'package:pos_printer/pos_printer.dart';
import 'package:screenshot/screenshot.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 BillGenerator _billGenerator = BillGenerator();
final GlobalKey globalKey = GlobalKey();
var showBill = false;
ScreenshotController screenshotController = ScreenshotController();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: RepaintBoundary(
key: globalKey,
child: Padding(
padding:
const EdgeInsets.only(top: 8, bottom: 8, left: 16, right: 16),
child: Column(
children: [
showBill
? Expanded(
child: _billGenerator.build(bill()),
)
: Expanded(
child: ListView(
children: [
StreamBuilder<List<DeviceData>>(
initialData: const [],
stream: _billGenerator.posPrinter.getDevices(),
builder: (_, snapshot) {
final data = snapshot.data;
if (data == null || data.isEmpty) {
return const SizedBox();
}
return Column(
children: [
for (final device in data) ...[
deviceCard(device),
const SizedBox(height: 16),
],
],
);
},
),
],
),
),
const SizedBox(
height: 16,
),
ButtonBar(
children: [
ElevatedButton(
onPressed: () async {
await _billGenerator.posPrinter.startDiscovery();
},
child: const Text('Search Devices'),
),
ElevatedButton(
onPressed: () async {
await _billGenerator.posPrinter.disconnect();
},
child: const Text('Disconnect'),
),
if (showBill)
ElevatedButton(
onPressed: () async {
try {
await _billGenerator.printBill();
} on Exception catch (e) {
print(e.toString());
}
},
child: const Text('Print'),
),
ElevatedButton(
onPressed: () async {
setState(() {
showBill = !showBill;
});
},
child: const Text('View Bill'),
),
],
),
],
),
),
),
),
);
}
Widget deviceCard(DeviceData device) {
return InkWell(
onTap: () async {
await _billGenerator.posPrinter.connect(device);
},
child: Card(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(Icons.bluetooth,
color: device.isBle ? Colors.blue : Colors.black),
const SizedBox(
width: 16,
),
Text(
device.name,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const Spacer(),
const Icon(Icons.forward)
],
),
),
),
);
}
Widget bill() {
const color = Colors.white;
return Container(
width: 400,
color: color,
child: Padding(
padding: EdgeInsets.only(left: 8, right: 8, top: 16, bottom: 16),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image(
image: AssetImage('assets/logo.png'),
width: 150,
),
Text(
'Wow Mart',
style: TextStyle(
color: Colors.black,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 20),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'Receipt No. - ',
style: TextStyle(
fontSize: 14,
color: Colors.black,
),
),
SizedBox(
width: 8,
),
Text(
'KAR/21JUN/1001',
style: TextStyle(
fontSize: 14,
color: Colors.black,
),
),
],
),
SizedBox(
height: 8,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'Date & Time. - ',
style: TextStyle(
fontSize: 14,
color: Colors.black,
),
),
SizedBox(
width: 8,
),
Text(
'21st June, 15:21',
style: TextStyle(
fontSize: 14,
color: Colors.black,
),
),
],
),
SizedBox(
height: 8,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'Customer ID - ',
style: TextStyle(
fontSize: 14,
color: Colors.black,
),
),
SizedBox(
width: 8,
),
Text(
'TKAR1002',
style: TextStyle(
fontSize: 14,
color: Colors.black,
),
),
],
),
SizedBox(
height: 16,
),
Divider(
color: Colors.black,
thickness: 3,
),
Row(
children: [
Expanded(
flex: 4,
child: Padding(
padding: EdgeInsets.only(left: 4),
child: Text(
'Product',
textAlign: TextAlign.start,
),
),
),
SizedBox(
width: 16,
),
Expanded(
flex: 3,
child: Padding(
padding: EdgeInsets.only(left: 0),
child: Text(
'Qty',
textAlign: TextAlign.start,
),
),
),
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.only(left: 0),
child: Text(
'Rate',
textAlign: TextAlign.start,
),
),
),
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.only(right: 4),
child: Text(
'Amount',
textAlign: TextAlign.end,
),
),
),
],
),
Divider(
color: Colors.black,
thickness: 3,
),
SizedBox(
height: 8,
),
for (var i = 0; i < 15; i++) ...[
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
flex: 4,
child: Padding(
padding: EdgeInsets.only(left: 4),
child: Text(
'பச்சை பட்டாணி',
textAlign: TextAlign.start,
maxLines: 3,
),
),
),
SizedBox(
width: 16,
),
Expanded(
flex: 3,
child: Padding(
padding: EdgeInsets.only(left: 0),
child: Text(
'2 கே.ஜி',
textAlign: TextAlign.start,
maxLines: 3,
),
),
),
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.only(left: 0),
child: Text(
'₹20',
textAlign: TextAlign.start,
),
),
),
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.only(right: 4),
child: Text(
'₹40',
textAlign: TextAlign.end,
),
),
),
],
),
SizedBox(
height: 8,
),
],
Divider(
color: Colors.black,
thickness: 3,
),
SizedBox(
height: 8,
),
SizedBox(
width: double.infinity,
child: Text(
'Items: 15 | Qty: 4 Kg',
style: TextStyle(
fontSize: 14,
),
textAlign: TextAlign.start,
),
),
SizedBox(
height: 16,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Offer Discounts',
style: TextStyle(
fontSize: 14,
),
),
Text(
'₹5',
style: TextStyle(
fontSize: 14,
),
),
],
),
SizedBox(
height: 16,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Redeemable Points',
style: TextStyle(
fontSize: 14,
),
),
Text(
'0',
style: TextStyle(
fontSize: 14,
),
),
],
),
SizedBox(
height: 16,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Total',
style: TextStyle(
fontSize: 14,
),
),
Text(
'₹380',
style: TextStyle(
fontSize: 14,
),
),
],
),
SizedBox(
height: 16,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Payment Mode',
style: TextStyle(
fontSize: 14,
),
),
Text(
'Cash',
style: TextStyle(
fontSize: 14,
),
),
],
),
SizedBox(
height: 16,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Collected',
style: TextStyle(
fontSize: 14,
),
),
Text(
'₹380',
style: TextStyle(
fontSize: 14,
),
),
],
),
SizedBox(
height: 16,
),
Divider(
color: Colors.black,
thickness: 3,
),
SizedBox(
height: 20,
),
DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.black, width: 2)),
child: Padding(
padding: EdgeInsets.all(16),
child: SizedBox(
width: double.infinity,
child: Text(
'Offer Details - One Kg Free Tomato @ ₹100 cart value',
textAlign: TextAlign.start,
style: TextStyle(
fontSize: 14,
),
),
),
),
),
SizedBox(
height: 16,
),
DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.black, width: 2)),
child: Padding(
padding: EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Loyalty Points',
textAlign: TextAlign.start,
style: TextStyle(fontSize: 14),
),
Padding(
padding: EdgeInsets.only(left: 8, right: 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'- Claimed in this offer',
style: TextStyle(fontSize: 12),
),
Text(
'0',
style: TextStyle(fontSize: 12),
),
],
),
),
Padding(
padding: EdgeInsets.only(left: 8, right: 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'- Total Remaining Points',
style: TextStyle(fontSize: 12),
),
Text(
'20',
style: TextStyle(fontSize: 12),
),
],
),
),
],
)),
),
SizedBox(
height: 20,
),
Divider(
color: Colors.black,
thickness: 3,
),
SizedBox(
height: 40,
),
],
),
),
);
}
}