insurance_card_scanner 0.1.2 insurance_card_scanner: ^0.1.2 copied to clipboard
A library that makes it easy to add health insurance card scanning to any flutter application.
import 'package:flutter/material.dart';
import 'package:insurance_card_scanner/insurance_card_scanner.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
// set a valid session token here
const token = 'XXX';
//camera permissions request -- must happen before CardScanModal is created.
Future<void> requestCameraPermission(BuildContext context) async {
var status = await Permission.camera.status;
debugPrint("Camera Status - $status");
status = await Permission.camera.request();
if (status.isDenied) {
showMessage(context, 'Camera Access Denied');
} else {
debugPrint("Camera Status request - $status");
}
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Card Scanner Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MainScreen(),
);
}
}
class MainScreen extends StatelessWidget {
const MainScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
requestCameraPermission(context);
return Scaffold(
appBar: AppBar(
title: const Text('Card Scanner Example'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
MaterialButton(
onPressed: () => showWidget(context),
child: const Text('Show widget'),
color: Colors.blue,
textColor: Colors.white,
),
const SizedBox(height: 40),
MaterialButton(
onPressed: () => showModal(context),
child: const Text('Show modal'),
color: Colors.green,
textColor: Colors.white,
)
],
),
),
);
}
void showWidget(BuildContext context) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (ctx) => const ScannerWidgetScreen(),
),
);
}
void showModal(BuildContext context) {
Navigator.of(context).push(
CardScanModal(
sessionToken: token,
onCardScanSuccess: (card) {
debugPrint("Scanned card: $card");
showMessage(context, 'Scan success');
},
onCardScanError: (message) {
showMessage(context, message ?? 'Scan failed');
},
onWebViewLoadError: () {
showMessage(context, 'WebView failed to load');
},
onCardScanCancel: () {
Navigator.of(context).pop();
},
),
);
}
}
class ScannerWidgetScreen extends StatelessWidget {
const ScannerWidgetScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Scanner widget'),
),
body: CardScanner(
sessionToken: token,
onCardScanSuccess: (card) {
debugPrint("Scanned card: $card");
showMessage(context, 'Scan success');
},
onCardScanError: (message) {
showMessage(context, message ?? 'Scan failed');
},
onWebViewLoadError: () {
showMessage(context, 'WebView failed to load');
},
onCardScanCancel: () {
Navigator.of(context).pop();
},
),
);
}
}
void showMessage(BuildContext context, String message) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text(message),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("OK"),
)
],
),
);
}