fl_droid_connection 0.0.4
fl_droid_connection: ^0.0.4 copied to clipboard
DTB Android Connection Flutter plugin
example/lib/main.dart
import 'dart:convert';
import 'package:fl_droid_connection_example/classes/fl_droid_connection_request.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:fl_droid_connection/fl_droid_connection.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _dynamicResult = 'Unknown';
final _flDroidConnectionPlugin = FlDroidConnection();
final TextEditingController _amountController = TextEditingController();
final TextEditingController _amountPassMpmController =
TextEditingController();
final TextEditingController _amountPassCpmController =
TextEditingController();
final TextEditingController _paymentTokenController = TextEditingController();
late BuildContext _buildContext;
final List<Map<String, dynamic>> _transactions = [];
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await _flDroidConnectionPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// 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(() {
_platformVersion = platformVersion;
});
}
Future<void> ping() async {
String pingResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
pingResult =
await _flDroidConnectionPlugin.ping() ?? 'Unkown ping result';
} on PlatformException {
pingResult = 'Failed to ping.';
}
// 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;
debugPrint(pingResult);
setState(() {
_dynamicResult = pingResult;
});
}
Future<void> lastTransaction() async {
String lastTransactionResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
lastTransactionResult =
await _flDroidConnectionPlugin.lastTransaction() ??
'Unkown lastTransaction result';
} on PlatformException {
lastTransactionResult = 'Failed to lastTransaction.';
}
// 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;
debugPrint(lastTransactionResult);
setState(() {
_dynamicResult = lastTransactionResult;
});
}
Future<void> settlement() async {
String settlementResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
settlementResult = await _flDroidConnectionPlugin.settlement() ??
'Unkown settlement result';
} on PlatformException {
settlementResult = 'Failed to settlement.';
}
// 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;
debugPrint(settlementResult);
setState(() {
_dynamicResult = settlementResult;
});
}
Future<void> passMpm() async {
String passMpmResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
passMpmResult = await _flDroidConnectionPlugin.passMpm(
FlDroidConnectionRequest(amount: _amountPassMpmController.text)
.toRawJson()) ??
'Unkown passMpm result';
} on PlatformException {
passMpmResult = 'Failed to passMpm.';
}
// 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;
debugPrint(passMpmResult);
setState(() {
_dynamicResult = passMpmResult;
});
}
Future<void> passCpm() async {
String passCpmResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
passCpmResult = await _flDroidConnectionPlugin.passCpm(
FlDroidConnectionRequest(
amount: _amountPassCpmController.text,
paymentToken: _paymentTokenController.text)
.toRawJson()) ??
'Unkown passCpm result';
} on PlatformException {
passCpmResult = 'Failed to passCpm.';
}
// 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;
debugPrint(passCpmResult);
setState(() {
_dynamicResult = passCpmResult;
});
}
Future<void> sale() async {
String saleResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
saleResult = await _flDroidConnectionPlugin.sale(
FlDroidConnectionRequest(amount: _amountController.text)
.toRawJson()) ??
'Unkown sale result';
} on PlatformException {
saleResult = 'Failed to sale.';
}
// 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;
Navigator.of(_buildContext, rootNavigator: true).pop();
Map<String, dynamic> jsonResult = jsonDecode(saleResult);
debugPrint(jsonResult.toString());
debugPrint(saleResult);
setState(() {
_dynamicResult = saleResult;
_transactions.add(jsonResult);
});
}
Future<void> voidOperation(String traceNo) async {
String voidResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
voidResult = await _flDroidConnectionPlugin.voidOperation(
FlDroidConnectionRequest(traceNo: traceNo).toRawJson()) ??
'Unkown void result';
} on PlatformException {
voidResult = 'Failed to void.';
}
// 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;
Map<String, dynamic> jsonResult = jsonDecode(voidResult);
debugPrint(jsonResult.toString());
debugPrint(voidResult);
setState(() {
_dynamicResult = voidResult;
});
}
Future<void> tblpInit() async {
String tblpInitResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
Map<String, dynamic> requestJson = {
"merchant_id": "ba2891",
"terminal_id": "70010222",
"user_id": "12333",
"total_amount": "20000",
"items": [
{
"name": "test",
"barcode": "123123123",
"measure_unit": "unit",
"qty": "1",
"unit_price": "20000",
"total_amount": "20000"
}
]
};
try {
tblpInitResult =
await _flDroidConnectionPlugin.tblpInit(json.encode(requestJson)) ??
'Unkown void result';
} on PlatformException {
tblpInitResult = 'Failed to void.';
}
// 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;
Map<String, dynamic> jsonResult = jsonDecode(tblpInitResult);
debugPrint(jsonResult.toString());
debugPrint(tblpInitResult);
setState(() {
_dynamicResult = tblpInitResult;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: const [
DefaultMaterialLocalizations.delegate,
DefaultWidgetsLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', ''), // English, no country code
Locale('he', ''), // Hebrew, no country code
Locale.fromSubtags(languageCode: 'zh')
],
home: Builder(builder: (context) {
_buildContext = context;
return Scaffold(
appBar: AppBar(
title: const Text('Fl Droid Connection package example'),
),
body: Row(
children: [
Expanded(
flex: 2,
child: Wrap(
spacing: 20,
alignment: WrapAlignment.start,
runAlignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.start,
direction: Axis.vertical,
children: [
ElevatedButton(
onPressed: () {
ping();
},
child: const Text('Ping')),
ElevatedButton(
onPressed: () {
lastTransaction();
},
child: const Text('Last Transaction')),
ElevatedButton(
onPressed: () {
settlement();
},
child: const Text('Settlement')),
ElevatedButton(
onPressed: () {
tblpInit();
},
child: const Text('TBLP INIT')),
ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: const Text('Enter amount'),
content: TextFormField(
controller: _amountController,
),
actions: [
ElevatedButton(
onPressed: () {
if (_amountController
.text.isEmpty ||
_amountController.text == "0") {
return;
}
sale();
},
child: const Text('Sale')),
],
);
});
},
child: const Text('Sale')),
ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: const Text('Enter amount'),
content: TextFormField(
controller: _amountPassMpmController,
),
actions: [
ElevatedButton(
onPressed: () {
if (_amountPassMpmController
.text.isEmpty ||
_amountPassMpmController.text ==
"0") {
return;
}
passMpm();
},
child: const Text('Sale')),
],
);
});
},
child: const Text('Pass Mpm Sale')),
ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: const Text(
'Enter amount & payment token'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
controller: _amountPassCpmController,
decoration: const InputDecoration(
hintText: "Amount"),
),
TextFormField(
controller: _paymentTokenController,
decoration: const InputDecoration(
hintText: "Payment token"),
),
],
),
actions: [
ElevatedButton(
onPressed: () {
if (_amountPassCpmController
.text.isEmpty ||
_amountPassCpmController.text ==
"0") {
return;
}
if (_paymentTokenController
.text.isEmpty) {
return;
}
passCpm();
},
child: const Text('Sale')),
],
);
});
},
child: const Text('Pass Cpm Sale')),
],
)),
Expanded(
flex: 3,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.grey),
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(10),
child: Column(
children: [
const Text(
"TRANSACTIONS",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 24),
),
Expanded(
child: Visibility(
visible: _transactions.isNotEmpty,
replacement: const Text("No transaction"),
child: ListView(
scrollDirection: Axis.vertical,
children: [
..._transactions.map((e) {
return ListTile(
title: Text((e["status_code"] ?? "Failed") +
" : " +
(e["ret"]?["response_code"] ?? "None")),
subtitle:
Text(e["ret"]?["amount"] ?? "None"),
leading:
Text(e["ret"]?["operation"] ?? "None"),
trailing: Visibility(
visible: e["status_code"] == "ok" &&
e["ret"]?["response_code"] == "000",
child: ElevatedButton(
onPressed: () {
voidOperation(
e["ret"]?["trace_no"] ?? "");
},
child: const Text("VOID")),
),
);
})
],
),
),
)
],
),
)),
Expanded(
flex: 3,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.grey),
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(10),
child: Column(
children: [
const Text(
"RESPONSE",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 24),
),
Text(_dynamicResult)
],
),
))
],
),
bottomNavigationBar: Text('Running on: $_platformVersion\n'),
);
}),
);
}
}