dtb_link 0.0.14+21
dtb_link: ^0.0.14+21 copied to clipboard
DTB Link - utility app enabling applications to connect and integrate with NFC card readers over Bluetooth
example/lib/main.dart
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:dtb_link/dtb_link.dart';
enum EasyKey { sKey, echo, saleTran, voidTran }
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';
DtbLink dtbLink = DtbLink();
DataInitRet? initData;
List<BluetoothDevice> deviceList = [];
TextEditingController controller = TextEditingController();
ScrollController scrollController = ScrollController();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
dtbLink.terminalInitData().then((data) {
debugPrint("initData: ${data?.toJson()}");
initData = data;
});
dtbLink.onEvent((onData) {
debugPrint("onData: $onData");
Map<String, dynamic> retData = json.decode(onData);
if (retData["command"] == "bluetooth.device_found") {
String? deviceId = retData["ret"]?["device_id"];
String? deviceName = retData["ret"]?["device_name"];
if (deviceId != null && deviceName != null) {
final device = BluetoothDevice(deviceId: deviceId, deviceName: deviceName);
if (!deviceList.contains(device)) {
setState(() {
deviceList.add(device);
addMessage("${device.toJson()}");
});
}
}
} else if (retData["command"] == "bluetooth.download_file.pregress") {
int? current = retData["ret"]?["current"];
int? total = retData["ret"]?["total"];
debugPrint("current: $current, total: $total");
}
});
initPlatformState();
});
}
Future<void> initPlatformState() async {
String platformVersion;
try {
debugPrint("platformVersion before");
platformVersion = await dtbLink.getPlatformVersion() ?? 'Unknown platform version';
debugPrint("platformVersion: $platformVersion");
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("DTB Link Flutter Demo $_platformVersion")),
body: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Wrap(
runSpacing: 6,
children: [
const Text("Bluetooth"),
ElevatedButton(
onPressed: () {
deviceList.clear();
addMessage("=>search device");
dtbLink.bluetoothStartSearch();
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth Search"),
),
ElevatedButton(
onPressed: () {
addMessage("=>isConnected");
dtbLink.bluetoothIsConnected().then((result) {
addMessage("<=$result");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth is connected"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Contactless upload");
dtbLink.bluetoothContactlessFileUpload().then((result) {
addMessage("<=$result");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth Contactless upload"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Contactless upload");
dtbLink.bluetoothEmvFileUpload().then((result) {
addMessage("<=$result");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth EMV upload"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Device data");
dtbLink.bluetoothDeviceData().then((device) {
addMessage("<=${device.toJson()}");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth Device Data"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Reconnect Device");
dtbLink.bluetoothReconnectDevice().then((device) {
addMessage("<=${device?.toJson()}");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth Reconnect Device"),
),
ElevatedButton(
onPressed: () {
addMessage("=>card read");
dtbLink.traceNo.then((traceNo) {
dtbLink
.bluetoothCardRead(amount: "500.00", tranType: 1, traceNo: traceNo)
.then((ret) {
addMessage("<=$ret");
});
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth card read"),
),
],
),
),
),
Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Wrap(
runSpacing: 6,
children: [
const Text("Terminal"),
ElevatedButton(
onPressed: () {
addMessage("=>Echo test");
dtbLink.terminalEchoTest().then((ret) {
addMessage("<=$ret");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Echo Test"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Init Data");
dtbLink.terminalInitData().then((ret) {
addMessage("<=${ret?.toJson()}");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Init Data"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Init terminal");
dtbLink.terminalDoInit(terminalId: "", trc: "").then((ret) {
addMessage("<=${ret.toJson()}");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Init Terminal"),
),
ElevatedButton(
onPressed: () {
addMessage("=>do sale");
dtbLink.doSale(amount: 500).then((ret) {
addMessage("<=$ret");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Do Sale"),
),
],
),
),
)
],
),
),
),
// Text
Container(
constraints: const BoxConstraints(maxHeight: 200),
child: SingleChildScrollView(
controller: scrollController,
child: TextField(
maxLines: null,
enabled: false,
controller: controller,
),
),
),
],
),
),
),
);
}
addMessage(String msg) {
controller.text = "${controller.text}\n$msg\n";
Future.delayed(const Duration(milliseconds: 200), () {
scrollController.animateTo(scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 200), curve: Curves.easeInOut);
});
}
// Widget _buildConfig() {
// return Container(
// decoration: const BoxDecoration(
// color: color02,
// borderRadius: BorderRadius.only(
// bottomLeft: Radius.circular(20), bottomRight: Radius.circular(20)),
// image: DecorationImage(
// image: AssetImage("assets/home_section_bg.png"),
// fit: BoxFit.fitWidth,
// ),
// ),
// child: Padding(
// padding: const EdgeInsets.all(16),
// child: Row(
// children: [
// Expanded(
// child: Column(
// children: [
// buildDeviceInfo(),
// // _buildTerminalInfo(
// // context
// // .watch<DemoProvider>()
// // .terminalConfigPercent,
// // context
// // .watch<DemoProvider>()
// // .terminalConfigTitle,
// // ),
// const SizedBox(height: 16),
// ElevatedButton(
// onPressed: () => Get.to(const ConfigPage()),
// style: ElevatedButton.styleFrom(
// shape: const StadiumBorder(),
// backgroundColor: color20,
// side: const BorderSide(color: color03, width: 3),
// ),
// child: const Row(
// mainAxisAlignment: MainAxisAlignment.center,
// mainAxisSize: MainAxisSize.max,
// children: [
// Text('Тохиргоо'),
// Icon(Icons.chevron_right),
// ],
// ),
// ),
// ],
// ),
// ),
// const SizedBox(width: 16),
// CircularPercentIndicator(
// radius: 80,
// lineWidth: 6,
// animation: true,
// percent: 0.755,
// startAngle: 180,
// backgroundColor: color03,
// animationDuration: 2000,
// center: Container(
// height: 138,
// width: 138,
// decoration:
// const BoxDecoration(shape: BoxShape.circle, color: color01),
// child: Center(
// child: Container(
// height: 126,
// width: 126,
// decoration: const BoxDecoration(
// shape: BoxShape.circle, color: color04),
// child: const Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// Text("Тохиргоо"),
// Text(
// "42.31%",
// style: TextStyle(fontWeight: FontWeight.bold),
// ),
// Text(
// "-42,aa",
// style: TextStyle(color: color11),
// ),
// ],
// ),
// ),
// ),
// ),
// circularStrokeCap: CircularStrokeCap.round,
// progressColor: color20,
// ),
// ],
// ),
// ),
// );
// }
//
// Widget _buildEasyTransaction() {
// return Obx(() {
// final initData = service.initData.value;
// final Map<String, dynamic> lastTran = service.lastTran;
// debugPrint("lastTran: $lastTran");
// return Container(
// margin: const EdgeInsets.all(10),
// padding:
// const EdgeInsets.only(left: 10, right: 10, top: 20, bottom: 20),
// decoration: const BoxDecoration(
// color: color02,
// borderRadius: BorderRadius.all(Radius.circular(20)),
// ),
// child: Column(
// children: [
// const Row(
// mainAxisSize: MainAxisSize.max,
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// children: [
// Text("Хялбар Гүйлгээ"),
// Icon(Icons.edit_note, color: color05),
// ],
// ),
// const SizedBox(height: 20),
// LayoutBuilder(builder: (context, constraint) {
// double width = constraint.maxWidth / 4 - 6;
// Size size = Size(width, width);
// return Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// children: [
// EasyButton(
// size: size,
// onPressed: initData.netEncKey != null
// ? () => _onAddLog(EasyKey.sKey, initData: initData)
// : null,
// child: const Text("SKey"),
// ),
// EasyButton(
// size: size,
// onPressed: initData.netEncKey != null
// ? () => _onAddLog(EasyKey.echo, initData: initData)
// : null,
// child: const Text("Echo"),
// ),
// EasyButton(
// size: size,
// onPressed: initData.netEncKey != null
// ? () => _onAddLog(EasyKey.saleTran, initData: initData)
// : null,
// child: const Text("Sale"),
// ),
// EasyButton(
// size: size,
// onPressed: lastTran.isEmpty
// ? null
// : () => _onAddLog(EasyKey.voidTran, initData: initData),
// child: const Text("Void"),
// ),
// ],
// );
// })
// ],
// ),
// );
// });
// }
//
// _onAddLog(EasyKey key, {required DataInitRet initData}) {
// service.addLog(key.name.toUpperCase()).then((_) => _scrollDown());
// if (key == EasyKey.sKey) {
// service.syncKey().then((keyRet) {
// service.addLog("${keyRet.toJson()}").then((_) => _scrollDown());
// });
// } else if (key == EasyKey.echo) {
// service.terminalEchoTest().then((ret) {
// service.addLog("$ret").then((_) => _scrollDown());
// });
// } else if (key == EasyKey.saleTran) {
// service.transactionSale().then((ret) {
// service.addLog("$ret").then((_) => _scrollDown());
// });
// } else if (key == EasyKey.voidTran) {
// service.transactionVoid().then((ret) {
// service.addLog("$ret").then((_) => _scrollDown());
// });
// }
// }
//
// Widget _buildTranHistory() {
// return Expanded(
// child: Container(
// margin: const EdgeInsets.only(left: 10, right: 10, bottom: 20),
// padding:
// const EdgeInsets.only(left: 10, right: 10, top: 20, bottom: 20),
// decoration: const BoxDecoration(
// color: color02,
// borderRadius: BorderRadius.all(Radius.circular(20))),
// child: Column(
// children: [
// const Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// children: [Text("Комманд лог"), Icon(Icons.history)],
// ),
// Expanded(
// child: SizedBox(
// width: MediaQuery.of(context).size.width - 20,
// child: Scrollbar(
// thumbVisibility: true,
// controller: _scrollController,
// child: SingleChildScrollView(
// controller: _scrollController,
// child: _buildLog(),
// ),
// ),
// ),
// )
// ],
// ),
// ),
// );
// }
//
// Widget _buildLog() {
// return Obx(() {
// final logData = service.logData;
// List<Widget> list = [];
// for (String log in logData) {
// list.add(Text(log));
// }
// return Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// mainAxisSize: MainAxisSize.min,
// children: list,
// );
// });
// }
//
// void _scrollDown() {
// _scrollController.animateTo(
// _scrollController.position.maxScrollExtent,
// duration: const Duration(seconds: 1),
// curve: Curves.fastOutSlowIn,
// );
// }
}