import 'package:cssd/config/routes.dart'; import 'package:cssd/providers/connectivity.dart'; import 'package:cssd/widgets/Global/internet_error.dart'; import 'package:cssd/widgets/TestResultScreens/Add/selector_box.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:barcode_scan/barcode_scan.dart'; import 'package:cssd/providers/test_results.dart'; // import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart'; import 'package:cssd/pages/test_form_page.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:provider/provider.dart'; class TestResultAddScreen extends StatefulWidget { // static const route = '/TestResultAddScreen'; @override State createState() { return _TestResultAddScreenState(); } } class _TestResultAddScreenState extends State { String testCode = ""; TextEditingController testTypeCTRL = new TextEditingController(); final List testTypeVals = []; TextEditingController testIdCTRL = new TextEditingController(); bool isInit = true; @override void didChangeDependencies() { if (isInit) { var testResultProviderObject = Provider.of(context, listen: false); testResultProviderObject.getFilterItems().then((result) { for (var testType in result[1].keys) { testTypeVals.add(testType); } testTypeCTRL.text = testTypeVals[0]; 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 WillPopScope( onWillPop: () async { Navigator.pushReplacementNamed( context, Routes.testResultScreen, ); return true; }, child: Scaffold( appBar: AppBar( titleSpacing: -10, title: Text('Test Results'), leading: IconButton( icon: Icon( Icons.keyboard_arrow_left, 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(), ) : 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 { Navigator.pushReplacement( context, MaterialPageRoute( builder: (context) { return FormPage( uniqueCode: result, ); }, ), ); } }, ); }, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: [ Column( crossAxisAlignment: CrossAxisAlignment.start, 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: testIdCTRL, 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.number, ), ), ], ) ], ), SizedBox( width: 5 * _deviceBlockSize, ), Expanded( child: Container( height: 100, margin: EdgeInsets.only(bottom: 50), child: FlatButton( padding: EdgeInsets.all(0), color: Color(0xFFEB4A64), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), ), child: Text( 'GO', textAlign: TextAlign.center, style: TextStyle( color: Colors.white, fontSize: 6 * _deviceBlockSize, fontWeight: FontWeight.w700), ), onPressed: () { if (testIdCTRL.text != '') { Navigator.push( context, MaterialPageRoute( builder: (context) { return FormPage( uniqueCode: testIdCTRL.text); }, ), ); } else if (testTypeCTRL.text != '') { Navigator.pushReplacement( context, MaterialPageRoute( builder: (context) { return FormPage( name: testTypeCTRL.text, ); }, ), ); } // print(testCode); }, ), ), ) ], ), ], ) : 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'; } } }