123 lines
4.4 KiB
Dart
123 lines
4.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:dart_core_sdk/handlingunit.pb.dart';
|
|
import 'package:dart_core_sdk/handlingunitQuery.pbgrpc.dart';
|
|
import 'package:dart_core_sdk/shared.pb.dart';
|
|
import 'package:sampleapp/widgets/components/reflex_button.dart';
|
|
import 'package:barcode_scan2/barcode_scan2.dart';
|
|
import 'package:sampleapp/widgets/components/reflex_circular_progress.dart';
|
|
import 'package:sampleapp/widgets/components/reflex_text_form_field.dart';
|
|
import '../../locator.dart';
|
|
import '../components/reflex_alert.dart';
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
class ScanBarcode extends StatefulWidget {
|
|
final String projectID;
|
|
|
|
final void Function(String refID, Handlingunit? handlingUnitResult) onBarcodeScanned;
|
|
|
|
const ScanBarcode(
|
|
{super.key, required this.projectID, required this.onBarcodeScanned});
|
|
|
|
@override
|
|
_ScanBarcodeState createState() => _ScanBarcodeState();
|
|
}
|
|
|
|
class _ScanBarcodeState extends State<ScanBarcode> {
|
|
final _handlingUnitIDController = TextEditingController();
|
|
|
|
bool _isButtonDisabled() {
|
|
return _handlingUnitIDController.text.isEmpty;
|
|
}
|
|
|
|
void _onHandlingUnitLookup() async {
|
|
showDialog(
|
|
barrierDismissible: false,
|
|
context: context,
|
|
builder: (context) => const Center(child: ReflexCircularProgress()));
|
|
await locator
|
|
.get<HandlingunitQueryClient>()
|
|
.getByIds(HandlingunitByIdQuery(
|
|
header: QueryProjectHeader(
|
|
projectID: widget.projectID,
|
|
),
|
|
iDs: [EntityID(
|
|
refID: _handlingUnitIDController.text,
|
|
)],
|
|
))
|
|
.then((res) => {
|
|
widget.onBarcodeScanned(_handlingUnitIDController.text, res.objects.isNotEmpty ? res.objects.first : null),
|
|
Navigator.of(context, rootNavigator: true).pop()
|
|
})
|
|
.catchError((e) => {
|
|
Navigator.of(context, rootNavigator: true).pop(),
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: Text(AppLocalizations.of(context)!.error),
|
|
content: Text(e.toString()),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text(AppLocalizations.of(context)!.ok))
|
|
],
|
|
))
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(15.0),
|
|
child: Form(
|
|
child: Column(
|
|
children: [
|
|
ReflexTextFormField(
|
|
controller: _handlingUnitIDController,
|
|
label: AppLocalizations.of(context)!.handlingUnitIdentifier,
|
|
onChanged: (v) {
|
|
setState(() {});
|
|
},
|
|
),
|
|
// Separator
|
|
const SizedBox(height: 20),
|
|
Text(
|
|
AppLocalizations.of(context)!.or,
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
// Scan button
|
|
ReflexButton(
|
|
text: AppLocalizations.of(context)!.scanBarcode,
|
|
isFullWidth: true,
|
|
onPressed: () async {
|
|
var result = await BarcodeScanner.scan();
|
|
if (result.rawContent.isNotEmpty) {
|
|
setState(() {
|
|
_handlingUnitIDController.text = result.rawContent.trim();
|
|
});
|
|
_onHandlingUnitLookup();
|
|
}
|
|
}),
|
|
ReflexAlert(
|
|
text:
|
|
AppLocalizations.of(context)!.selectHandlingUnit,
|
|
icon: Icons.info_outline_rounded,
|
|
),
|
|
const SizedBox(height: 20),
|
|
Align(
|
|
alignment: Alignment.centerRight,
|
|
child: ReflexButton(
|
|
text: AppLocalizations.of(context)!.search,
|
|
onPressed:
|
|
_isButtonDisabled() ? null : _onHandlingUnitLookup,
|
|
))
|
|
],
|
|
))));
|
|
}
|
|
}
|