150 lines
4.6 KiB
Dart
150 lines
4.6 KiB
Dart
import 'package:dart_core_sdk/handlingunit.pb.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:grpc/grpc.dart';
|
|
import 'package:sampleapp/services/grpc_service.dart';
|
|
import 'package:sampleapp/widgets/components/reflex_app_bar.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:sampleapp/widgets/components/reflex_circular_progress.dart';
|
|
|
|
import '../globals.dart';
|
|
import '../locator.dart';
|
|
import '../services/auth/auth.dart';
|
|
import '../services/auth/auth_view_model.dart';
|
|
import 'scan_flow/delivery_confirmed.dart' as sf;
|
|
import 'scan_flow/select_context_screen.dart' as sf;
|
|
import 'scan_flow/scan_barcode_screen.dart' as sf;
|
|
import 'scan_flow/scan_result_screen.dart' as sf;
|
|
|
|
const routePrefixHome = '/home/';
|
|
const routeHome = '/home/$routeSelectContext';
|
|
const routeSelectContext = 'select_context';
|
|
const routeScanBarCode = 'scan_barcode';
|
|
const routeScanResult = 'scan_result';
|
|
const routeDeliveryConfirmed = 'delivery_confirmed';
|
|
|
|
class HomeScreen extends StatefulWidget {
|
|
final VoidCallback onLogout;
|
|
|
|
const HomeScreen({super.key, required this.onLogout});
|
|
|
|
@override
|
|
State<StatefulWidget> createState() {
|
|
return HomeScreenState();
|
|
}
|
|
}
|
|
|
|
class HomeScreenState extends State<HomeScreen> {
|
|
final _navigatorKey = GlobalKey<NavigatorState>();
|
|
String? _projectID;
|
|
String? _handlingUnitID;
|
|
Handlingunit? _handlingUnit;
|
|
bool? _isDeliveryValid;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
}
|
|
|
|
Future<void> _logout(AuthViewModel avm) async {
|
|
showDialog(
|
|
barrierDismissible: false,
|
|
context: context,
|
|
builder: (context) => const Center(child: ReflexCircularProgress()));
|
|
await avm.logout().then((result) => {
|
|
Navigator.of(context, rootNavigator: true).pop(),
|
|
if (result) widget.onLogout()
|
|
});
|
|
}
|
|
|
|
void _onContextSelected(String projectID) {
|
|
_projectID = projectID;
|
|
_navigatorKey.currentState!.pushNamed(routeScanBarCode);
|
|
}
|
|
|
|
void _onBarCodeScanned(String handlingUnitRefId, Handlingunit? handlingUnitResult) {
|
|
_handlingUnit = handlingUnitResult;
|
|
_handlingUnitID = handlingUnitRefId;
|
|
_navigatorKey.currentState!.pushNamed(routeScanResult);
|
|
}
|
|
|
|
void _onDeliveryConfirmed(bool isDeliveryValid) {
|
|
_isDeliveryValid = isDeliveryValid;
|
|
_navigatorKey.currentState!.pushNamed(routeDeliveryConfirmed);
|
|
}
|
|
|
|
void _onNextDelivery() {
|
|
_navigatorKey.currentState!.pushNamedAndRemoveUntil(
|
|
routeScanBarCode, ModalRoute.withName(routeSelectContext));
|
|
}
|
|
|
|
Route _onGenerateRoute(RouteSettings settings) {
|
|
late Widget page;
|
|
switch (settings.name) {
|
|
case routeSelectContext:
|
|
page = sf.SelectContext(onContextSelected: _onContextSelected);
|
|
break;
|
|
case routeScanBarCode:
|
|
page = sf.ScanBarcode(
|
|
onBarcodeScanned: _onBarCodeScanned, projectID: _projectID!);
|
|
break;
|
|
case routeScanResult:
|
|
page = sf.ScanResult(
|
|
refID: _handlingUnitID!,
|
|
handlingUnit: _handlingUnit,
|
|
projectID: _projectID!,
|
|
onDeliveryConfirmed: _onDeliveryConfirmed);
|
|
break;
|
|
case routeDeliveryConfirmed:
|
|
page = sf.DeliveryConfirmed(
|
|
projectID: _projectID!,
|
|
handlingUnit: _handlingUnit!,
|
|
isDeliveryValid: _isDeliveryValid!,
|
|
onNextDelivery: _onNextDelivery,
|
|
);
|
|
break;
|
|
default:
|
|
page = Text(settings.name!);
|
|
}
|
|
|
|
return CupertinoPageRoute<dynamic>(
|
|
builder: (context) {
|
|
return page;
|
|
},
|
|
settings: settings,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final authViewModel = context.watch<AuthViewModel>();
|
|
return WillPopScope(
|
|
onWillPop: () async {
|
|
if (_navigatorKey.currentState!.canPop()) {
|
|
_navigatorKey.currentState!.pop();
|
|
return false;
|
|
}
|
|
return true;
|
|
},
|
|
child: Scaffold(
|
|
appBar: ReflexAppBar(
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.logout,
|
|
color: Colors.white,
|
|
),
|
|
onPressed: () => _logout(authViewModel)
|
|
)
|
|
],
|
|
onBackCallback: () => {
|
|
if (_navigatorKey.currentState!.canPop())
|
|
{_navigatorKey.currentState!.pop()}
|
|
}),
|
|
body: Navigator(
|
|
key: _navigatorKey,
|
|
initialRoute: routeSelectContext,
|
|
onGenerateRoute: _onGenerateRoute)));
|
|
}
|
|
}
|