2023-11-13 17:49:10 +03:30
|
|
|
import 'package:dartx/dartx.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2023-11-13 17:49:10 +03:30
|
|
|
import 'package:hiddify/core/core_providers.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/utils/utils.dart';
|
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
|
|
|
|
|
|
|
|
class QRCodeScannerScreen extends HookConsumerWidget with PresLogger {
|
|
|
|
|
const QRCodeScannerScreen({super.key});
|
|
|
|
|
|
|
|
|
|
Future<String?> open(BuildContext context) async {
|
|
|
|
|
return Navigator.of(context, rootNavigator: true).push(
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
fullscreenDialog: true,
|
|
|
|
|
builder: (context) => const QRCodeScannerScreen(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2023-11-13 17:49:10 +03:30
|
|
|
final t = ref.watch(translationsProvider);
|
|
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
final controller = useMemoized(
|
2023-11-13 17:49:10 +03:30
|
|
|
() => MobileScannerController(detectionTimeoutMs: 500),
|
2023-07-06 17:18:41 +03:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => controller.dispose, []);
|
|
|
|
|
|
2023-11-13 17:49:10 +03:30
|
|
|
final size = MediaQuery.sizeOf(context);
|
|
|
|
|
final overlaySize = (size.shortestSide - 12).coerceAtMost(248);
|
|
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
return Scaffold(
|
|
|
|
|
extendBodyBehindAppBar: true,
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
iconTheme: Theme.of(context).iconTheme.copyWith(
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
size: 32,
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: ValueListenableBuilder(
|
|
|
|
|
valueListenable: controller.torchState,
|
|
|
|
|
builder: (context, state, child) {
|
|
|
|
|
switch (state) {
|
|
|
|
|
case TorchState.off:
|
|
|
|
|
return const Icon(Icons.flash_off, color: Colors.grey);
|
|
|
|
|
case TorchState.on:
|
|
|
|
|
return const Icon(Icons.flash_on, color: Colors.yellow);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
2023-11-13 17:49:10 +03:30
|
|
|
tooltip: t.profile.add.qrScanner.torchSemanticLabel,
|
2023-07-06 17:18:41 +03:30
|
|
|
onPressed: () => controller.toggleTorch(),
|
|
|
|
|
),
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: ValueListenableBuilder(
|
|
|
|
|
valueListenable: controller.cameraFacingState,
|
|
|
|
|
builder: (context, state, child) {
|
|
|
|
|
switch (state) {
|
|
|
|
|
case CameraFacing.front:
|
|
|
|
|
return const Icon(Icons.camera_front);
|
|
|
|
|
case CameraFacing.back:
|
|
|
|
|
return const Icon(Icons.camera_rear);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
2023-11-13 17:49:10 +03:30
|
|
|
tooltip: t.profile.add.qrScanner.facingSemanticLabel,
|
2023-07-06 17:18:41 +03:30
|
|
|
onPressed: () => controller.switchCamera(),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2023-11-13 17:49:10 +03:30
|
|
|
body: Stack(
|
|
|
|
|
children: [
|
|
|
|
|
MobileScanner(
|
|
|
|
|
controller: controller,
|
|
|
|
|
onDetect: (capture) {
|
|
|
|
|
final data = capture.barcodes.first;
|
|
|
|
|
if (context.mounted && data.type == BarcodeType.url) {
|
|
|
|
|
loggy.debug('captured raw: [${data.rawValue}]');
|
|
|
|
|
loggy.debug('captured url: [${data.url?.url}]');
|
|
|
|
|
Navigator.of(context, rootNavigator: true).pop(data.url?.url);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
errorBuilder: (_, error, __) {
|
|
|
|
|
final message = switch (error.errorCode) {
|
|
|
|
|
MobileScannerErrorCode.permissionDenied =>
|
|
|
|
|
t.profile.add.qrScanner.permissionDeniedError,
|
|
|
|
|
_ => t.profile.add.qrScanner.unexpectedError,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Center(
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
const Padding(
|
|
|
|
|
padding: EdgeInsets.only(bottom: 8),
|
|
|
|
|
child: Icon(Icons.error, color: Colors.white),
|
|
|
|
|
),
|
|
|
|
|
Text(message),
|
|
|
|
|
Text(error.errorDetails?.message ?? ''),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
CustomPaint(
|
|
|
|
|
painter: ScannerOverlay(
|
|
|
|
|
Rect.fromCenter(
|
|
|
|
|
center: size.center(Offset.zero),
|
|
|
|
|
width: overlaySize,
|
|
|
|
|
height: overlaySize,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
2023-07-06 17:18:41 +03:30
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-13 17:49:10 +03:30
|
|
|
|
|
|
|
|
class ScannerOverlay extends CustomPainter {
|
|
|
|
|
ScannerOverlay(this.scanWindow);
|
|
|
|
|
|
|
|
|
|
final Rect scanWindow;
|
|
|
|
|
final double borderRadius = 12.0;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void paint(Canvas canvas, Size size) {
|
|
|
|
|
final backgroundPath = Path()..addRect(Rect.largest);
|
|
|
|
|
final cutoutPath = Path()
|
|
|
|
|
..addRRect(
|
|
|
|
|
RRect.fromRectAndCorners(
|
|
|
|
|
scanWindow,
|
|
|
|
|
topLeft: Radius.circular(borderRadius),
|
|
|
|
|
topRight: Radius.circular(borderRadius),
|
|
|
|
|
bottomLeft: Radius.circular(borderRadius),
|
|
|
|
|
bottomRight: Radius.circular(borderRadius),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final backgroundPaint = Paint()
|
|
|
|
|
..color = Colors.black.withOpacity(0.5)
|
|
|
|
|
..style = PaintingStyle.fill
|
|
|
|
|
..blendMode = BlendMode.dstOut;
|
|
|
|
|
|
|
|
|
|
final backgroundWithCutout = Path.combine(
|
|
|
|
|
PathOperation.difference,
|
|
|
|
|
backgroundPath,
|
|
|
|
|
cutoutPath,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final borderPaint = Paint()
|
|
|
|
|
..color = Colors.white
|
|
|
|
|
..style = PaintingStyle.stroke
|
|
|
|
|
..strokeWidth = 3.0;
|
|
|
|
|
|
|
|
|
|
final borderRect = RRect.fromRectAndCorners(
|
|
|
|
|
scanWindow,
|
|
|
|
|
topLeft: Radius.circular(borderRadius),
|
|
|
|
|
topRight: Radius.circular(borderRadius),
|
|
|
|
|
bottomLeft: Radius.circular(borderRadius),
|
|
|
|
|
bottomRight: Radius.circular(borderRadius),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
canvas.drawPath(backgroundWithCutout, backgroundPaint);
|
|
|
|
|
canvas.drawRRect(borderRect, borderPaint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
bool shouldRepaint(covariant CustomPainter oldDelegate) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|