Merge branch 'bug_fix_mirza' into 'development'
Check-In NFC See merge request Cloud_Solution/diplomatic-quarter!326merge-requests/328/merge
commit
487d63ec7f
Binary file not shown.
After Width: | Height: | Size: 1.2 MiB |
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
@ -0,0 +1,196 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nfc_in_flutter/nfc_in_flutter.dart';
|
||||
|
||||
void showNfcReader(BuildContext context, {Function onNcfScan}) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
enableDrag: false,
|
||||
isDismissible: false,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(12), topRight: Radius.circular(12)),
|
||||
),
|
||||
backgroundColor: Colors.white,
|
||||
builder: (context) {
|
||||
return NfcLayout(
|
||||
onNcfScan: onNcfScan,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
class NfcLayout extends StatefulWidget {
|
||||
Function onNcfScan;
|
||||
|
||||
NfcLayout({this.onNcfScan});
|
||||
|
||||
@override
|
||||
_NfcLayoutState createState() => _NfcLayoutState();
|
||||
}
|
||||
|
||||
class _NfcLayoutState extends State<NfcLayout> {
|
||||
StreamSubscription<NDEFMessage> _stream;
|
||||
bool _reading = false;
|
||||
Widget mainWidget;
|
||||
String nfcId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
setState(() {
|
||||
// _reading = true;
|
||||
// Start reading using NFC.readNDEF()
|
||||
_stream = NFC
|
||||
.readNDEF(
|
||||
once: false,
|
||||
throwOnUserCancel: false,
|
||||
readerMode: NFCDispatchReaderMode())
|
||||
.listen((NDEFMessage message) {
|
||||
setState(() {
|
||||
_reading = true;
|
||||
mainWidget = doneNfc();
|
||||
});
|
||||
Future.delayed(const Duration(milliseconds: 500), () {
|
||||
_stream?.cancel();
|
||||
widget.onNcfScan(nfcId);
|
||||
Navigator.pop(context);
|
||||
});
|
||||
print("read NDEF id: ${message.id}");
|
||||
// widget.onNcfScan(message.id);
|
||||
nfcId = message.id;
|
||||
}, onError: (e) {
|
||||
// Check error handling guide below
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
(mainWidget == null && !_reading)
|
||||
? mainWidget = scanNfc()
|
||||
: mainWidget = doneNfc();
|
||||
return AnimatedSwitcher(
|
||||
duration: Duration(milliseconds: 500), child: mainWidget);
|
||||
}
|
||||
|
||||
Widget scanNfc() {
|
||||
return Container(
|
||||
key: ValueKey(1),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Text(
|
||||
"Ready To Scan",
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 24,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Image.asset(
|
||||
"assets/images/nfc/ic_nfc.png",
|
||||
height: MediaQuery.of(context).size.width / 3,
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Text(
|
||||
"Approach an NFC Tag",
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
ButtonTheme(
|
||||
minWidth: MediaQuery.of(context).size.width / 1.2,
|
||||
height: 45.0,
|
||||
buttonColor: Colors.grey[300],
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: RaisedButton(
|
||||
onPressed: () {
|
||||
_stream?.cancel();
|
||||
Navigator.pop(context);
|
||||
},
|
||||
child: Text("CANCEL"),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget doneNfc() {
|
||||
return Container(
|
||||
key: ValueKey(2),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Text(
|
||||
"Successfully Scanned",
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 24,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Image.asset(
|
||||
"assets/images/nfc/ic_done.png",
|
||||
height: MediaQuery.of(context).size.width / 3,
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Text(
|
||||
"Approach an NFC Tag",
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
ButtonTheme(
|
||||
minWidth: MediaQuery.of(context).size.width / 1.2,
|
||||
height: 45.0,
|
||||
buttonColor: Colors.grey[300],
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: RaisedButton(
|
||||
// onPressed: () {
|
||||
// _stream?.cancel();
|
||||
// widget.onNcfScan(nfcId);
|
||||
// Navigator.pop(context);
|
||||
// },
|
||||
onPressed: null,
|
||||
|
||||
child: Text("DONE"),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue