-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathnfc_scanner_widget.dart
More file actions
101 lines (87 loc) · 2.41 KB
/
Copy pathnfc_scanner_widget.dart
File metadata and controls
101 lines (87 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import 'dart:async';
import 'package:bb_mobile/core/themes/app_theme.dart';
import 'package:bb_mobile/core/utils/build_context_x.dart';
import 'package:bb_mobile/core/widgets/buttons/button.dart';
import 'package:bb_mobile/generated/flutter_gen/assets.gen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_nfc_kit/flutter_nfc_kit.dart';
import 'package:gif/gif.dart';
class NfcScannerWidget extends StatefulWidget {
final FutureOr<void> Function(NFCTag tag) onScanned;
final void Function(Object error)? onError;
final bool scanOnInit;
final Widget? loadingWidget;
const NfcScannerWidget({
super.key,
required this.onScanned,
this.onError,
this.scanOnInit = true,
this.loadingWidget,
});
@override
State<NfcScannerWidget> createState() => _NfcPageState();
}
class _NfcPageState extends State<NfcScannerWidget> {
NFCTag? _tag;
Future<void> _scan() async {
if (mounted) setState(() => _tag = null);
final NFCTag tag;
try {
// Coldcard uses NFC-V / ISO-15693.
tag = await FlutterNfcKit.poll(readIso15693: true, readIso18092: false);
} catch (e) {
widget.onError?.call(e);
if (!mounted) return;
setState(() => _tag = null);
return;
}
if (!mounted) return;
await widget.onScanned(tag);
if (!mounted) return;
setState(() => _tag = tag);
}
@override
void initState() {
super.initState();
if (widget.scanOnInit) unawaited(_scan());
}
@override
void dispose() {
unawaited(_finishNfcSession());
super.dispose();
}
@override
Widget build(BuildContext context) {
final loadingWidget =
widget.loadingWidget ??
Center(
child: SizedBox(
width: 250,
height: 250,
child: Gif(
image: AssetImage(Assets.animations.nfcPoll.path),
autostart: Autostart.loop,
),
),
);
return Column(
mainAxisAlignment: .center,
children: [
if (_tag == null) loadingWidget,
if (_tag != null)
BBButton.big(
label: context.loc.scanNfcButton,
onPressed: _scan,
bgColor: context.appColors.surface,
textColor: context.appColors.secondary,
iconData: Icons.nfc,
),
],
);
}
Future<void> _finishNfcSession() async {
try {
await FlutterNfcKit.finish();
} catch (_) {}
}
}