import 'package:barcode_scan2/barcode_scan2.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:provider/provider.dart'; import '../../config/routes.dart'; import '../../providers/connectivity.dart'; import '../../providers/test_results.dart'; import '../../widgets/Global/internet_error.dart'; import '../../widgets/TestResultScreens/Add/selector_box.dart'; class TestResultAddScreen extends StatefulWidget { @override State createState() { return _TestResultAddScreenState(); } } class _TestResultAddScreenState extends State { TextEditingController testTypeCTRL = new TextEditingController(); List> testTypeMap = []; List testTypeVals = []; TextEditingController testCodeCTRL = new TextEditingController(); bool isInit = true; @override void didChangeDependencies() { if (isInit) { var testResultProviderObject = Provider.of(context, listen: false); testResultProviderObject.resetTests(); testResultProviderObject.getFilterItems().then((result) { // testTypeVals = result[1] as List>; // testTypeCTRL.text = testTypeVals[0].keys as String; testTypeMap = Provider.of( context, listen: false, ).testTypeMap; for (var testType in testTypeMap[1].keys) { testTypeVals.add(testType); } setState(() { isInit = false; }); }); } super.didChangeDependencies(); } @override Widget build(BuildContext context) { final _media = MediaQuery.of(context); // final double _pixelRatio = (_media.size.height / _media.size.width * 7 / 5); final double _devicewidth = _media.size.width; final double _deviceBlockSize = _devicewidth / 100; return SafeArea( child: Scaffold( appBar: AppBar( titleSpacing: -10, title: Text( 'Test Results', style: TextStyle( color: Colors.white, ), ), backgroundColor: Colors.black, leading: IconButton( icon: Icon( Icons.keyboard_arrow_left, color: Colors.white, size: 35, ), // padding: EdgeInsets.all(0), onPressed: () => Navigator.of(context).popAndPushNamed(Routes.testResultScreen), ), ), body: Consumer( builder: (ct, connection, _) { return connection.conStatus != 'offline' ? isInit ? Center( child: CircularProgressIndicator( color: Colors.black, ), ) : ListView( padding: EdgeInsets.symmetric(horizontal: 20, vertical: 20), children: [ Container( padding: EdgeInsets.only( top: 10, ), child: Text( 'Add a Test Result', style: TextStyle( color: Colors.black, fontSize: 5 * _deviceBlockSize, letterSpacing: 3, fontWeight: FontWeight.w700, ), ), ), GestureDetector( child: Container( margin: EdgeInsets.only(top: 5, bottom: 30), padding: EdgeInsets.all(10), height: 200, decoration: BoxDecoration(border: Border.all()), child: Center( child: Text( 'PLease Scan The Test Barcode', textAlign: TextAlign.center, style: TextStyle( color: Colors.grey, fontSize: 5 * _deviceBlockSize, fontWeight: FontWeight.w100, letterSpacing: 3, ), ), ), ), onTap: () { scan().then( (String result) { if (result == 'cancel') { print('cancel'); } else if (result == 'error') { print('error'); } else if (result == 'access') { print('access'); } else { Provider.of( context, listen: false, ).testTypeId = result; Provider.of( context, listen: false, ).isFromTestID = false; Navigator.pushNamed( context, Routes.formTestResultScreen, arguments: result, ); } }, ); }, ), Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: [ Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( width: 70 * _deviceBlockSize, child: TestResultAddSelectorBox( deviceBlockSize: _deviceBlockSize, dataCTRL: testTypeCTRL, dataVals: testTypeVals, label: 'Choose a Test Type', ), ), Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ Text( 'OR', style: TextStyle( fontWeight: FontWeight.w700, fontSize: 7 * _deviceBlockSize, ), ), SizedBox( width: 8 * _deviceBlockSize, ), Container( width: 50 * _deviceBlockSize, child: TextFormField( onChanged: (val) { if (val != '') { setState( () { testTypeCTRL.text = ''; }, ); } else { setState( () { testTypeCTRL.text = testTypeVals[0]; }, ); } }, controller: testCodeCTRL, decoration: InputDecoration( isDense: true, icon: Icon( FontAwesomeIcons.sortNumericDown, ), labelText: 'Enter a Test Code', labelStyle: TextStyle( fontSize: 3 * _deviceBlockSize, ), contentPadding: EdgeInsets.only( bottom: 5, top: 5, ), ), keyboardType: TextInputType.visiblePassword, ), ), ], ), SizedBox( height: 40.0, ), Container( padding: EdgeInsets.symmetric( horizontal: 5 * _deviceBlockSize, vertical: 10, ), width: 50 * _deviceBlockSize, decoration: BoxDecoration( color: Colors.black, borderRadius: BorderRadius.circular(7), ), child: Align( alignment: Alignment.center, child: TextButton( child: Text( 'Select', style: TextStyle( color: Colors.white, fontSize: 5 * _deviceBlockSize, ), ), onPressed: () async { print( "testCodeCTRL.text : ${testCodeCTRL.text}"); print( "testTypeCTRL.text : ${testTypeCTRL.text}"); if (testCodeCTRL.text != '') { Provider.of( context, listen: false, ).testTypeId = testCodeCTRL.text; Provider.of( context, listen: false, ).isFromTestID = false; Navigator.pushNamed( context, Routes.formTestResultScreen, arguments: testCodeCTRL.text, ); } else if (testTypeCTRL.text != '') { String? testTypeId = ''; for (var map in testTypeMap) { if (map.containsKey( testTypeCTRL.text)) { testTypeId = map[testTypeCTRL.text]; break; } } Provider.of( context, listen: false, ).testTypeId = testTypeId!; Navigator.pushReplacementNamed( context, Routes.formTestResultScreen, arguments: testTypeId, ); } // print(testCode); }, ), ), ), ], ), SizedBox( width: 5 * _deviceBlockSize, ), ], ), ], ) : ConnectionErrorBox(); }, ), ), ); } Future scan() async { try { var barcode = await BarcodeScanner.scan(); if (barcode.type.name == 'Cancelled') { return 'cancel'; } else if (barcode.type.name == 'Failed') { return 'error'; } return barcode.rawContent; } on PlatformException catch (_) { return 'access'; } catch (_) { return 'error'; } } }