ml_card_scanner 0.2.3
ml_card_scanner: ^0.2.3 copied to clipboard
Package for scanning bank credit cards.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:ml_card_scanner/ml_card_scanner.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MaterialApp(home: InitialScreen()));
}
class InitialScreen extends StatefulWidget {
const InitialScreen({super.key});
@override
State<InitialScreen> createState() => _InitialScreenState();
}
class _InitialScreenState extends State<InitialScreen> {
CardInfo? _cardInfo;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Card Scanner Demo'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
(_cardInfo != null)
? Expanded(
child: Center(
child: Container(
padding: const EdgeInsets.all(16),
width: MediaQuery.of(context).size.width - 32,
height: MediaQuery.of(context).size.width / 2,
decoration: const BoxDecoration(
color: Colors.blueGrey,
borderRadius: BorderRadius.all(
Radius.circular(16),
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
_cardInfo?.numberFormatted() ?? '',
style: const TextStyle(
color: Colors.white,
fontSize: 24,
letterSpacing: 2,
),
),
const SizedBox(height: 32),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: Text(
_cardInfo?.expiry ?? '',
style: const TextStyle(
color: Colors.white,
fontSize: 16,
letterSpacing: 2,
),
),
),
Text(
_cardInfo?.type ?? '',
style: const TextStyle(
color: Colors.white,
fontSize: 16,
letterSpacing: 2,
),
),
],
),
],
),
),
),
)
: const SizedBox.shrink(),
const SizedBox(height: 16),
Center(
child: ElevatedButton(
onPressed: _scanCard,
child: const Text('Tap to Scan Card'),
),
),
const SizedBox(height: 16),
],
),
);
}
void _scanCard() async {
setState(() {
_cardInfo = null;
});
final result = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return const MainScreen();
},
),
) as CardInfo?;
if (result != null) {
setState(() {
_cardInfo = result;
});
}
}
}
class MainScreen extends StatefulWidget {
const MainScreen({super.key});
@override
State<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
final ScannerWidgetController _controller = ScannerWidgetController();
@override
void initState() {
super.initState();
_controller
..setCardListener(_onListenCard)
..setErrorListener(_onError);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ScannerWidget(
controller: _controller,
overlayOrientation: CardOrientation.landscape,
cameraResolution: CameraResolution.max,
oneShotScanning: true,
),
);
}
@override
void dispose() {
_controller
..removeCardListeners(_onListenCard)
..removeErrorListener(_onError)
..dispose();
super.dispose();
}
void _onListenCard(CardInfo? value) {
if (value != null) {
Navigator.of(context).pop(value);
}
}
void _onError(ScannerException exception) {
if (kDebugMode) {
print('Error: ${exception.message}');
}
}
}