import 'package:cssd/models/item_model.dart'; import 'package:cssd/providers/items.dart'; import 'package:cssd/widgets/ItemScreens/Add/item_save_button.dart'; import 'package:cssd/widgets/ItemScreens/Add/upload_image.dart'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:provider/provider.dart'; import 'package:random_string/random_string.dart'; import '../../config/routes.dart'; import '../../providers/connectivity.dart'; import '../../widgets/Global/internet_error.dart'; class ItemAddScreen extends StatefulWidget { @override _ItemAddScreenState createState() => _ItemAddScreenState(); } class _ItemAddScreenState extends State { final _itemSizeFocusNode = FocusNode(); final _itemSpecificFocusNode = FocusNode(); final _itemUdiFocusNode = FocusNode(); final _descriptionFocusNode = FocusNode(); TextEditingController descriptionCTRL = new TextEditingController(); TextEditingController udiCTRL = new TextEditingController(); TextEditingController nameCTRL = new TextEditingController(); TextEditingController specificCTRL = new TextEditingController(); TextEditingController sizeCTRL = new TextEditingController(); final _form = GlobalKey(); bool isInit = true; bool isLoading = false; ItemFilterObject? filters; final batchId = randomString(10); int curLabelVal = 1; @override void initState() { descriptionCTRL.text = ''; udiCTRL.text = ''; nameCTRL.text = ''; sizeCTRL.text = ''; specificCTRL.text = ''; super.initState(); } void _saveForm(String name, String size, String specific, String udi, String description, ItemProvider itemProvider) async { itemProvider.setLoadingButton(); setState(() { isLoading = false; }); if (!_form.currentState!.validate()) { return; } await Provider.of(context, listen: false) .createItemResult( name, size, specific, udi, description, batchId, ) .then( (result) { setState(() { isLoading = false; }); if (result == 'done') { Navigator.of(context).popAndPushNamed(Routes.itemResultScreen); Fluttertoast.showToast( msg: "Item added.", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, ); } else { Fluttertoast.showToast( msg: result, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, ); } itemProvider.resetLoadingButton(); }, ); } @override void didChangeDependencies() async { if (isInit) { setState(() { isLoading = true; }); var itemProviderObject = Provider.of(context); itemProviderObject.catchImages.clear(); itemProviderObject.getItemResults().then( (result) { setState( () { isLoading = false; }, ); }, ); } isInit = false; super.didChangeDependencies(); } @override Widget build(BuildContext context) { final _media = MediaQuery.of(context); final double _devicewidth = _media.size.width; final double _deviceBlockSize = _devicewidth / 100; final itemProviderObject = Provider.of(context); return WillPopScope( onWillPop: () async { Navigator.pushReplacementNamed( context, Routes.itemResultScreen, ); return true; }, child: SafeArea( child: GestureDetector( onTap: () { FocusManager.instance.primaryFocus?.unfocus(); }, child: Scaffold( appBar: AppBar( titleSpacing: -10, title: Text( 'Items', style: TextStyle( color: Colors.white, ), ), backgroundColor: Colors.black, leading: IconButton( icon: Icon( Icons.keyboard_arrow_left, color: Colors.white, size: 35, ), onPressed: () => Navigator.of(context) .pushReplacementNamed(Routes.itemResultScreen), ), ), body: Consumer( builder: (ct, connection, _) { return connection.conStatus != 'offline' ? isLoading ? Center( child: CircularProgressIndicator( color: Colors.black, ), ) : Form( key: _form, child: Padding( padding: EdgeInsets.all(20), child: ListView( children: [ Text( 'Add an Item', style: TextStyle( fontWeight: FontWeight.w700, fontSize: 6 * _deviceBlockSize, letterSpacing: 3, ), ), SizedBox( height: 20, ), TextFormField( validator: (val) { if (val!.isEmpty) { return 'Name required!'; } return null; }, controller: nameCTRL, decoration: InputDecoration( icon: Icon( FontAwesomeIcons.alignLeft, size: 7 * _deviceBlockSize, ), labelStyle: TextStyle( fontWeight: FontWeight.w100, fontSize: 4 * _deviceBlockSize, ), isDense: true, labelText: 'Item Name', contentPadding: EdgeInsets.symmetric( vertical: 2, horizontal: 3, ), ), style: TextStyle( fontWeight: FontWeight.w700, fontSize: 5 * _deviceBlockSize, ), keyboardType: TextInputType.text, textInputAction: TextInputAction.next, onFieldSubmitted: (_) { FocusScope.of(context) .requestFocus(_itemSizeFocusNode); }, ), SizedBox( height: 30, ), TextFormField( validator: (val) { if (val!.isEmpty) { return 'Size required!'; } return null; }, controller: sizeCTRL, decoration: InputDecoration( icon: Icon( FontAwesomeIcons.sortNumericDown, size: 7 * _deviceBlockSize, ), labelStyle: TextStyle( fontWeight: FontWeight.w100, fontSize: 4 * _deviceBlockSize, ), isDense: true, labelText: 'Size', contentPadding: EdgeInsets.symmetric( vertical: 2, horizontal: 3, ), ), style: TextStyle( fontWeight: FontWeight.w700, fontSize: 5 * _deviceBlockSize, ), focusNode: _itemSizeFocusNode, keyboardType: TextInputType.number, textInputAction: TextInputAction.next, onFieldSubmitted: (_) { FocusScope.of(context) .requestFocus(_itemSpecificFocusNode); }, ), SizedBox( height: 30, ), TextFormField( validator: (val) { if (val!.isEmpty) { return 'Specific required!'; } return null; }, controller: specificCTRL, decoration: InputDecoration( icon: Icon( FontAwesomeIcons.alignLeft, size: 7 * _deviceBlockSize, ), labelStyle: TextStyle( fontWeight: FontWeight.w100, fontSize: 4 * _deviceBlockSize, ), isDense: true, labelText: 'Specific', contentPadding: EdgeInsets.symmetric( vertical: 2, horizontal: 3, ), ), style: TextStyle( fontWeight: FontWeight.w700, fontSize: 5 * _deviceBlockSize, ), focusNode: _itemSpecificFocusNode, keyboardType: TextInputType.visiblePassword, textInputAction: TextInputAction.next, onFieldSubmitted: (_) { FocusScope.of(context) .requestFocus(_itemUdiFocusNode); }, ), SizedBox( height: 30, ), TextFormField( validator: (val) { if (val!.isEmpty) { return 'UDI required!'; } return null; }, controller: udiCTRL, decoration: InputDecoration( icon: Icon( FontAwesomeIcons.alignLeft, size: 7 * _deviceBlockSize, ), labelStyle: TextStyle( fontWeight: FontWeight.w100, fontSize: 4 * _deviceBlockSize, ), isDense: true, labelText: 'UDI', contentPadding: EdgeInsets.symmetric( vertical: 2, horizontal: 3, ), ), style: TextStyle( fontWeight: FontWeight.w700, fontSize: 5 * _deviceBlockSize, ), focusNode: _itemUdiFocusNode, keyboardType: TextInputType.visiblePassword, textInputAction: TextInputAction.next, onFieldSubmitted: (_) { FocusScope.of(context) .requestFocus(_descriptionFocusNode); }, ), SizedBox( height: 30, ), TextFormField( controller: descriptionCTRL, decoration: InputDecoration( icon: Icon( FontAwesomeIcons.alignLeft, size: 7 * _deviceBlockSize, ), labelStyle: TextStyle( fontWeight: FontWeight.w100, fontSize: 4 * _deviceBlockSize, ), isDense: true, labelText: 'Description', contentPadding: EdgeInsets.symmetric( vertical: 2, horizontal: 3, ), ), style: TextStyle( fontWeight: FontWeight.w700, fontSize: 5 * _deviceBlockSize, ), keyboardType: TextInputType.multiline, focusNode: _descriptionFocusNode, minLines: 1, maxLines: 5, ), SizedBox( height: 30.0, ), UploaderImage( deviceBlockSize: _deviceBlockSize, providerObject: itemProviderObject, ), SizedBox( height: 70, ), ItemAddSaveButton( onTap: () { _saveForm( nameCTRL.text, sizeCTRL.text, specificCTRL.text, udiCTRL.text, descriptionCTRL.text, itemProviderObject); }, deviceBlockSize: _deviceBlockSize, ), SizedBox( height: 100, ), ], ), ), ) : ConnectionErrorBox(); }, ), ), ), ), ); } }