flutter_pinelabs 1.0.0+2 flutter_pinelabs: ^1.0.0+2 copied to clipboard
Flutter plugin to support pinelab device.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_pinelabs/flutter_pinelabs_module.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _flutterPinelabsPlugin = const FlutterPinelabs(
header: HeaderModel(
applicationId: 'your pine lab id',
methodId: '1001',
versionNo: '1.0',
),
);
late TextEditingController _controller;
TransactionType _transactionType = TransactionType.cash;
String _responseMessage = '';
@override
void initState() {
super.initState();
_controller = TextEditingController();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _doTransaction(double amount) async {
/// do transaction for pinelabs device.
/// calls the pinelab device with the header provided in the constructor.
/// one can override the contructor using [overrideHeader] parameter.
final response = await _flutterPinelabsPlugin.doTransaction(
transactionType: _transactionType,
billingRefNo: '12345',
paymentAmount: amount,
mobileNumberForEChargeSlip: '1234567890',
);
/// provides ResponseModel in return which contains the response from the pinelabs device.
setState(() {
_responseMessage = response?.responseMsg ?? '';
});
}
String _organizeName(String name) {
final words = name.split(" ");
StringBuffer line = StringBuffer();
final result = StringBuffer();
for (var i = 0; i < words.length; i++) {
final word = words[i];
if (line.length + word.length > 16) {
//add line to result if it full
result
..write(line)
..write('\n');
line = StringBuffer(); //reset line is empty
}
line.write(word);
if (i == (words.length - 1)) {
line.write(" ".padRight(line.length >= 16 ? 0 : (16 - words.length)));
} else {
line.write(" ");
}
}
result.write(line);
return result.toString();
}
Future<void> _printData() async {
final rowData = [
{'name': 'test product', 'qty': '2', 'price': '\$200'},
{'name': 'dosa', 'qty': '15', 'price': '\$10'},
{
'name': 'SOME BRAND awesome product that keeps your mind cool',
'qty': '2',
'price': '\$200'
},
{'name': 'test product', 'qty': '100', 'price': '\$10000'},
];
final List<PrintRowModel> printRows = rowData.map((e) {
final name = e['name']!;
final qty = e['qty']!;
final price = e['price']!;
e['name'] = name.length > 12 ? _organizeName(name) : name.padRight(16);
e['qty'] = qty.padRight(7);
e['price'] = price.padRight(9);
return PrintRowModel(
printDataType: '0',
printerWidth: 32,
dataToPrint: '${e['name']}${e['qty']}${e['price']}',
imageData: '0',
imagePath: '0',
isCenterAligned: false,
);
}).toList();
final request = PrintModel(
printRefNo: '1234',
savePrintData: true,
data: [
const PrintRowModel(
printDataType: '0',
printerWidth: 24,
isCenterAligned: true,
dataToPrint: 'Test Bill',
imageData: '0',
imagePath: '0',
),
const PrintRowModel(
printDataType: '0',
printerWidth: 48,
dataToPrint: '------------------------------------------------',
imageData: '0',
imagePath: '0',
),
const PrintRowModel(
printDataType: '0',
printerWidth: 24,
dataToPrint: 'Items Qty Cost',
imageData: '0',
imagePath: '0',
),
const PrintRowModel(
printDataType: '0',
printerWidth: 48,
dataToPrint: '------------------------------------------------',
imageData: '0',
imagePath: '0',
),
...printRows,
const PrintRowModel(
printDataType: '0',
printerWidth: 48,
dataToPrint: '------------------------------------------------',
imageData: '0',
imagePath: '0',
),
const PrintRowModel(
printDataType: '0',
printerWidth: 24,
dataToPrint: 'Total: \$5000',
imageData: '',
imagePath: '0',
),
const PrintRowModel(
printDataType: '0',
printerWidth: 48,
dataToPrint: '------------------------------------------------',
imageData: '0',
imagePath: '0',
),
const PrintRowModel(
printDataType: '0',
printerWidth: 32,
dataToPrint: 'Thank You For Shopping With Us',
imageData: '',
imagePath: '0',
),
const PrintRowModel(
printDataType: '0',
printerWidth: 24,
dataToPrint: '\n',
imageData: '',
imagePath: '0',
),
],
);
final response = await _flutterPinelabsPlugin.printData(
overrideHeader: const HeaderModel(
applicationId: 'your pinelab app id',
methodId: '1002',
versionNo: '1.0',
),
printRequest: request,
);
setState(() {
_responseMessage = response?.responseMsg ?? '';
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: [
TextField(
controller: _controller,
decoration: const InputDecoration(hintText: 'Enter amount'),
),
const SizedBox(height: 20),
DropdownButtonFormField<TransactionType>(
value: _transactionType,
items: TransactionType.values
.map(
(e) => DropdownMenuItem<TransactionType>(
value: e,
child: Text(e.name),
),
)
.toList(),
onChanged: (e) {
if (e == null) return;
setState(() {
_transactionType = e;
});
},
),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('Do transaction'),
onPressed: () async {
double amount = double.tryParse(_controller.text) ?? 0;
_doTransaction(amount);
},
),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('Print Data'),
onPressed: () async {
_printData();
},
),
const SizedBox(height: 20),
Text('Running on: $_responseMessage\n'),
],
),
),
);
}
}