flutter-freaccess-hub/lib/pages/qr_code_page/qr_code_page_model.dart

144 lines
3.8 KiB
Dart

import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:hub/flutter_flow/flutter_flow_model.dart';
import 'package:hub/pages/qr_code_page/qr_code_page_widget.dart';
import 'package:hub/shared/helpers/db_helper.dart';
import 'package:qr_flutter/qr_flutter.dart';
class QrCodePageModel extends FlutterFlowModel<QrCodePageWidget> {
/// Local state fields for this page.
bool isAccess = false;
String? key = null;
DateTime? time;
final DatabaseHelper db = DatabaseHelper();
late final bool isFingerprint;
late final String userDevUUID;
/// State fields for stateful widgets in this page.
final unfocusNode = FocusNode();
@override
void initState(BuildContext context) {
initVariable();
}
Future<void> initVariable() async {
isFingerprint = await db
.get(key: 'fingerprint', field: 'value')
.then((value) => value.toString() == 'true');
userDevUUID = await db
.get(key: 'userDevUUID', field: 'value')
.then((value) => value.toString());
}
@override
void dispose() {
unfocusNode.dispose();
}
Uint8List assembleQRPacket(
int direction, String identifier, String password) {
List<int> packet = [direction];
String paddedBadge = identifier.padLeft(30, '0');
for (var i = 0; i < paddedBadge.length; i += 2) {
packet.add(int.parse(paddedBadge.substring(i, i + 2), radix: 16));
}
DateTime now = DateTime.now();
int year = now.year % 100;
int month = now.month;
int day = now.day;
int hour = now.hour;
int minute = now.minute;
int sum = year + month + day + hour + minute;
if (sum == int.parse('0D', radix: 16) ||
sum == int.parse('0A', radix: 16)) {
packet.add(int.parse('FF', radix: 16));
} else {
packet.add(sum);
}
String paddedPassword = password.length != 4 ? 'FFFF' : password;
for (var i = 0; i < paddedPassword.length; i += 2) {
packet.add(int.parse(paddedPassword.substring(i, i + 2), radix: 16));
}
int check = 0x00;
for (var b in packet) {
check ^= b;
}
if (check == int.parse('0D', radix: 16) ||
check == int.parse('0A', radix: 16)) {
packet.add(int.parse('FF', radix: 16));
} else {
packet.add(check);
}
var bytes = packet
.map((byte) => byte.toRadixString(16).padLeft(2, '0'))
.join((' '));
return Uint8List.fromList(packet);
}
Uint8List hexStringToByteArray(String s) {
int len = s.length;
Uint8List data = Uint8List(len ~/ 2);
for (int i = 0; i < len; i += 2) {
data[i ~/ 2] =
((int.parse(s[i], radix: 16) << 4) + int.parse(s[i + 1], radix: 16));
}
return data;
}
String byteToHexa(Uint8List pDados) {
return pDados
.map((byte) => byte.toRadixString(16).padLeft(2, '0').toUpperCase())
.join();
}
Future<String> byteToString(Uint8List bytes) async {
return String.fromCharCodes(bytes);
}
Widget buildQrCode(
{required int errorCorrectLevel,
required double dimension,
required String identifier,
required String pass,
required int direction}) {
try {
const Color backgroundColor = Colors.white;
const Color foregroundColor = Colors.black;
return QrImageView.withQr(
qr: QrCode.fromUint8List(
data: assembleQRPacket(direction, identifier, pass),
errorCorrectLevel: errorCorrectLevel),
size: dimension,
padding: const EdgeInsets.all(10),
backgroundColor: backgroundColor,
foregroundColor: foregroundColor);
} catch (e) {
return Text("Erro ao gerar QR Code: ${e.toString()}");
}
}
/// Action blocks.
Future qrCodeEncoder(
BuildContext context, {
required String? key,
}) async {}
}