kalapa_nfc_reader_sdk 1.0.2
kalapa_nfc_reader_sdk: ^1.0.2 copied to clipboard
A Flutter plugin designed to integrate with Kalapa's NFC Reader SDK, enabling seamless NFC functionality within Flutter applications.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:kalapa_nfc_reader_sdk/kalapa_nfc_reader_sdk.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 _kalapaNfcReaderSdk = KalapaNfcReaderSdk();
String _sdkVersion = 'Unknown';
bool _isNFCSupported = false;
bool _isNFCEnabled = false;
String _result = '';
final TextEditingController _mrzController = TextEditingController();
@override
void initState() {
super.initState();
initPlatformState();
configureSDK();
}
Future<void> initPlatformState() async {
String sdkVersion;
bool nfcSupported;
bool nfcEnabled;
try {
sdkVersion = await _kalapaNfcReaderSdk.getSDKVersion();
nfcSupported = await _kalapaNfcReaderSdk.isNFCSupported();
nfcEnabled = await _kalapaNfcReaderSdk.isNFCEnabled();
} on PlatformException {
sdkVersion = 'Failed to get SDK version.';
nfcSupported = false;
nfcEnabled = false;
}
if (!mounted) return;
setState(() {
_sdkVersion = sdkVersion;
_isNFCSupported = nfcSupported;
_isNFCEnabled = nfcEnabled;
});
}
Future<void> configureSDK() async {
try {
await _kalapaNfcReaderSdk.configureSDK(
backgroundColor: "#FFFFFF",
mainColor: "#3270EA",
textColor: "#000000",
btnTextColor: "#FFFFFF",
language: "vi",
);
} on PlatformException catch (e) {
setState(() {
_result = 'Failed to configure SDK: ${e.message}';
});
}
}
Future<void> startWithUI() async {
FocusManager.instance.primaryFocus?.unfocus();
try {
final result = await _kalapaNfcReaderSdk.startWithUI();
setState(() {
_result = 'Success: $result';
});
} on PlatformException catch (e) {
setState(() {
_result = 'Error: ${e.message}';
});
}
}
Future<void> startWithoutUI() async {
FocusManager.instance.primaryFocus?.unfocus();
final mrz = _mrzController.text;
if (mrz.isEmpty) {
setState(() {
_result = 'Please enter MRZ data';
});
return;
}
try {
final result = await _kalapaNfcReaderSdk.startWithoutUI(mrz);
setState(() {
_result = 'Success: $result';
});
} on PlatformException catch (e) {
setState(() {
_result = 'Error: ${e.message}';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Kalapa NFC Reader SDK'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('SDK Version: $_sdkVersion'),
const SizedBox(height: 8),
Text('NFC Supported: ${_isNFCSupported ? 'Yes' : 'No'}'),
const SizedBox(height: 8),
Text('NFC Enabled: ${_isNFCEnabled ? 'Yes' : 'No'}'),
],
),
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _isNFCSupported ? startWithUI : null,
child: const Text('Start NFC with UI'),
),
const SizedBox(height: 20),
TextField(
controller: _mrzController,
decoration: const InputDecoration(
labelText: 'MRZ Data',
border: OutlineInputBorder(),
hintText: 'Enter MRZ lines',
),
maxLines: 3,
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: _isNFCSupported ? startWithoutUI : null,
child: const Text('Start NFC without UI'),
),
const SizedBox(height: 20),
if (_result.isNotEmpty)
Card(
color: _result.startsWith('Error') ? Colors.red[50] : Colors.green[50],
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Result:',
style: TextStyle(
fontWeight: FontWeight.bold,
color: _result.startsWith('Error') ? Colors.red : Colors.green,
),
),
const SizedBox(height: 8),
Text(_result),
],
),
),
),
],
),
),
),
);
}
@override
void dispose() {
_mrzController.dispose();
super.dispose();
}
}