You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
5.1 KiB
Dart
152 lines
5.1 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:qr_code_scanner_plus/qr_code_scanner_plus.dart';
|
|
import 'package:test_sa/extensions/context_extension.dart';
|
|
import 'package:test_sa/extensions/int_extensions.dart';
|
|
import 'package:test_sa/extensions/text_extensions.dart';
|
|
import 'package:test_sa/extensions/widget_extensions.dart';
|
|
import 'package:test_sa/models/device/asset.dart';
|
|
import 'package:test_sa/modules/cm_module/views/components/action_button/footer_action_button.dart';
|
|
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
|
|
|
|
import '../../../controllers/providers/api/devices_provider.dart';
|
|
import '../../../models/device/asset_search.dart';
|
|
import '../../../new_views/common_widgets/app_filled_button.dart';
|
|
import '../../../new_views/common_widgets/custom_app_bar.dart';
|
|
import '../../pages/device_transfer/search_device_page.dart';
|
|
|
|
class AssetScanQr extends StatefulWidget {
|
|
static const String id = "/asset-scan-qr";
|
|
|
|
const AssetScanQr({Key? key, required this.title, this.multiSelection = false,this.enablePickManually = true}) : super(key: key);
|
|
final String title;
|
|
final bool multiSelection;
|
|
final bool enablePickManually;
|
|
|
|
@override
|
|
_AssetScanQrState createState() => _AssetScanQrState();
|
|
}
|
|
|
|
class _AssetScanQrState extends State<AssetScanQr> {
|
|
// Barcode result;
|
|
QRViewController? _controller;
|
|
bool _scanDone = false;
|
|
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR_scanner');
|
|
late AssetProvider _devicesProvider;
|
|
|
|
// In order to get hot reload to work we need to pause the camera if the platform
|
|
// is android, or resume the camera if the platform is iOS.
|
|
@override
|
|
void reassemble() {
|
|
super.reassemble();
|
|
if (Platform.isAndroid) {
|
|
_controller?.pauseCamera();
|
|
} else if (Platform.isIOS) {
|
|
_controller?.resumeCamera();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
_controller?.dispose();
|
|
_devicesProvider.searchReset();
|
|
}
|
|
|
|
_pickManually() async {
|
|
_controller?.pauseCamera();
|
|
await Navigator.push(context, MaterialPageRoute(builder: (context) => SearchDevicePage(multiSelection: widget.multiSelection))).then((value) => _controller?.resumeCamera());
|
|
}
|
|
|
|
_getDevice(String result, {bool isQr = false}) async {
|
|
_devicesProvider.reset();
|
|
await _devicesProvider.getAssets(
|
|
search: DeviceSearch(assetNo: result, assetSerialNumber: ""),
|
|
isQr: isQr,
|
|
);
|
|
return _devicesProvider.devices;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
_devicesProvider = Provider.of<AssetProvider>(context);
|
|
return Scaffold(
|
|
appBar: DefaultAppBar(title: widget.title),
|
|
body: Column(
|
|
children: [
|
|
Stack(
|
|
children: [
|
|
QRView(
|
|
key: qrKey,
|
|
onQRViewCreated: (QRViewController controller) {
|
|
setState(() {
|
|
_controller = controller;
|
|
});
|
|
controller.scannedDataStream.listen((scanData) async {
|
|
if (!_scanDone) {
|
|
_scanDone = true;
|
|
if(!widget.enablePickManually){
|
|
if(scanData.code!=null){
|
|
Navigator.of(context).pop(Asset(assetNumber: scanData.code!));
|
|
}else{
|
|
_scanDone = false;
|
|
}
|
|
}else{
|
|
final result = await _getDevice(scanData.code!, isQr: true);
|
|
if (result.isNotEmpty) {
|
|
if (widget.multiSelection) {
|
|
Navigator.of(context).pop(<Asset>[result[0]]);
|
|
} else {
|
|
Navigator.of(context).pop(result[0]);
|
|
}
|
|
} else {
|
|
_scanDone = false;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
},
|
|
),
|
|
Center(
|
|
child: 'scan'.toSvgAsset(
|
|
height: 283.toScreenHeight.toInt(),
|
|
width: 283.toScreenWidth.toInt(),
|
|
fit: BoxFit.fitHeight,
|
|
),
|
|
),
|
|
],
|
|
).expanded,
|
|
if(widget.enablePickManually)
|
|
FooterActionButton.footerContainer(
|
|
context: context,
|
|
child: AppFilledButton(
|
|
label: context.translation.pickManually,
|
|
onPressed: _pickManually,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
// bottomSheet:
|
|
// 16.height,
|
|
|
|
// Container(
|
|
// height: 82.toScreenHeight,
|
|
// color: Colors.white,
|
|
// width: MediaQuery.of(context).size.width,
|
|
// child: Center(
|
|
// child: SizedBox(
|
|
// height: 50.toScreenHeight,
|
|
// width: 358.toScreenWidth,
|
|
// child: AppFilledButton(
|
|
// label: context.translation.pickManually,
|
|
// onPressed: _pickManually,
|
|
// ),
|
|
// ),
|
|
// ),
|
|
// )
|
|
);
|
|
}
|
|
}
|