import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:provider/provider.dart'; import '../../../../api/user_api_client.dart'; import '../../../../controllers/http_status_manger/http_status_manger.dart'; import '../../../../controllers/localization/localization.dart'; import '../../../../controllers/providers/api/gas_refill_provider.dart'; import '../../../../models/enums/user_types.dart'; import '../../../../models/gas_refill/gas_refill_model.dart'; import '../../../../models/subtitle.dart'; import '../../../app_style/colors.dart'; import '../../../app_style/sizing.dart'; import '../../../widgets/buttons/app_back_button.dart'; import '../../../widgets/buttons/app_button.dart'; import '../../../widgets/buttons/app_icon_button.dart'; import '../../../widgets/gas_refill/gas_refill_update_details_item.dart'; import '../../../widgets/loaders/loading_manager.dart'; import '../../../widgets/requests/info_row.dart'; import '../../../widgets/requests/request_status.dart'; import '../../../widgets/status/gas_refill/gas_status.dart'; import '../../../widgets/titles/app_sub_title.dart'; class GasRefillDetails extends StatefulWidget { final GasRefillModel? model; const GasRefillDetails({Key? key, this.model}) : super(key: key); @override State createState() => _GasRefillDetailsState(); } class _GasRefillDetailsState extends State { final GasRefillModel _model = GasRefillModel(); bool _enableEdit = false; bool _validate = false; GasRefillProvider? _gasRefillProvider; bool _isLoading = false; Subtitle? _subtitle; final GlobalKey _formKey = GlobalKey(); final GlobalKey _scaffoldKey = GlobalKey(); _update() async { _validate = true; if (!(_formKey.currentState?.validate() ?? false)) { setState(() {}); return false; } _formKey.currentState?.save(); _isLoading = true; setState(() {}); int? status = await _gasRefillProvider?.updateModel(newModel: _model, oldModel: widget.model); _isLoading = false; setState(() {}); if (status != null && status >= 200 && status < 300) { Fluttertoast.showToast( msg: _subtitle?.requestCompleteSuccessfully ?? "", ); _enableEdit = false; _validate = false; //Navigator.of(context).pop(); } else { String errorMessage = HttpStatusManger.getStatusMessage(status: status, subtitle: _subtitle); ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text(errorMessage), )); } } @override void initState() { _model.fromGasRefillModel(widget.model ?? GasRefillModel()); super.initState(); } @override Widget build(BuildContext context) { _subtitle = AppLocalization.of(context)?.subtitle; _gasRefillProvider = Provider.of(context); return Scaffold( key: _scaffoldKey, body: SafeArea( child: Form( key: _formKey, child: LoadingManager( isLoading: _isLoading, isFailedLoading: false, stateCode: 200, onRefresh: () async {}, child: Column( children: [ Container( color: Theme.of(context).colorScheme.primary, padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 4), child: Row( children: [ ABackButton(), Expanded( child: Center( child: Text( _subtitle?.details ?? "", style: Theme.of(context).textTheme.headline6?.copyWith(color: AColors.white, fontStyle: FontStyle.italic), ), ), ), if (UserApiClient().user?.roles == UsersTypes.engineer.name) AIconButton( iconData: _enableEdit ? Icons.cancel : Icons.edit, color: Theme.of(context).colorScheme.onPrimary, buttonSize: 42, backgroundColor: AColors.green, onPressed: () async { _enableEdit = !_enableEdit; _model.fromGasRefillModel(widget.model ?? GasRefillModel()); setState(() {}); }, ), const SizedBox( width: 16, ) ], ), ), Expanded( child: SingleChildScrollView( padding: EdgeInsets.all(16 * AppStyle.getScaleFactor(context)), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ RequestInfoRow( title: _subtitle?.title ?? "", info: _model.title ?? "", ), RequestInfoRow( title: _subtitle?.hospital ?? "", info: _model.clientName ?? "", ), _enableEdit ? Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const SizedBox( height: 8, ), ASubTitle(_subtitle?.status ?? ""), if (_validate && _model.status == null) ASubTitle( _subtitle?.requiredWord ?? "", color: Colors.red, ), const SizedBox( height: 4, ), GasStatusMenu( initialValue: _model.status, onSelect: (status) { _model.status = status; }, ) ], ) : Row( children: [ Expanded( child: Text( "${_subtitle?.status} : ", style: Theme.of(context).textTheme.subtitle2, textScaleFactor: AppStyle.getScaleFactor(context), ), ), StatusLabel(label: _model.status?.label.toString() ?? "", color: AColors.getGasStatusColor(_model.status?.id ?? 0)), ], ), const SizedBox( height: 8, ), const ASubTitle("Gas Requests"), const SizedBox( height: 8, ), ListView.builder( shrinkWrap: true, physics: const ClampingScrollPhysics(), itemCount: _model.details?.length, itemBuilder: (context, index) { final details = _model.details![index]; return GasRefillUpdateDetailsItem( details: details, validate: _validate, enableEdit: _enableEdit, ); }), if (_enableEdit) Column( children: [ const SizedBox( height: 16, ), AButton( text: _subtitle?.update ?? "", onPressed: _update, ), ], ), ], ), ), ), ], ), ), ), ), ); } }