Files
Siro/siro_driver/lib/views/transit/qr_scanner_page.dart
T

160 lines
4.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:audioplayers/audioplayers.dart';
import '../../constant/colors.dart';
import '../../constant/style.dart';
import '../../controller/transit/transit_driver_controller.dart';
import '../widgets/elevated_btn.dart';
import 'manual_boarding_page.dart';
class QRScannerPage extends StatefulWidget {
final int stopId;
final String stopName;
const QRScannerPage({super.key, required this.stopId, required this.stopName});
@override
State<QRScannerPage> createState() => _QRScannerPageState();
}
class _QRScannerPageState extends State<QRScannerPage> {
final MobileScannerController _scannerController = MobileScannerController();
final AudioPlayer _audioPlayer = AudioPlayer();
bool _isProcessing = false;
String _successMessage = '';
@override
void dispose() {
_scannerController.dispose();
_audioPlayer.dispose();
super.dispose();
}
void _onDetect(BarcodeCapture capture) async {
if (_isProcessing) return;
final List<Barcode> barcodes = capture.barcodes;
if (barcodes.isEmpty) return;
final String? code = barcodes.first.rawValue;
if (code == null || !code.startsWith('transit_board:')) return;
setState(() {
_isProcessing = true;
});
final passengerId = code.replaceAll('transit_board:', '');
// Play beep sound
try {
// Assuming you might add a beep sound asset later, or just visual feedback for now.
// await _audioPlayer.play(AssetSource('sounds/beep.mp3'));
} catch (_) {}
// Send API Request
final c = Get.find<TransitDriverController>();
final success = await c.boardPassenger(widget.stopId, passengerId);
if (success) {
setState(() {
_successMessage = 'تم صعود الراكب بنجاح ✅'.tr;
});
// Clear message after 1.5s
Future.delayed(const Duration(milliseconds: 1500), () {
if (mounted) {
setState(() {
_successMessage = '';
_isProcessing = false;
});
}
});
} else {
setState(() {
_successMessage = 'فشل التحقق ❌'.tr;
});
Future.delayed(const Duration(milliseconds: 2000), () {
if (mounted) {
setState(() {
_successMessage = '';
_isProcessing = false;
});
}
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: Text('${'Scan Passengers'.tr} - ${widget.stopName}', style: AppStyle.headTitle2.copyWith(color: Colors.white)),
backgroundColor: Colors.transparent,
elevation: 0,
iconTheme: const IconThemeData(color: Colors.white),
),
body: Stack(
children: [
MobileScanner(
controller: _scannerController,
onDetect: _onDetect,
),
// Overlay for scanner target
Center(
child: Container(
width: 250,
height: 250,
decoration: BoxDecoration(
border: Border.all(color: AppColor.accentColor, width: 3),
borderRadius: BorderRadius.circular(20),
),
),
),
// Processing Overlay
if (_isProcessing && _successMessage.isEmpty)
Container(
color: Colors.black54,
child: const Center(
child: CircularProgressIndicator(color: AppColor.accentColor),
),
),
// Success / Error Message Overlay
if (_successMessage.isNotEmpty)
Container(
color: _successMessage.contains('✅') ? Colors.green.withValues(alpha: 0.8) : Colors.red.withValues(alpha: 0.8),
child: Center(
child: Text(
_successMessage,
style: AppStyle.headTitle2.copyWith(color: Colors.white),
textAlign: TextAlign.center,
),
),
),
// Fallback manual list button
Positioned(
bottom: 40,
left: 20,
right: 20,
child: SizedBox(
height: 55,
child: MyElevatedButton(
onPressed: () {
Get.to(() => ManualBoardingPage(stopId: widget.stopId, stopName: widget.stopName));
},
kolor: AppColor.cardColor,
title: 'Manual Passenger List'.tr,
),
),
),
],
),
);
}
}