import 'package:algea_care/algea_care.dart'; import 'package:algea_care/features/pharmacy/domain/notifiers/ other_pharmacy_input_notifier.dart'; class PharmacyAddressForm extends ConsumerStatefulWidget { const PharmacyAddressForm({ Key? key, required this.countries, required this.user, required this.formKey, required this.isPatientAddress, required this.viewDetails, }) : super(key: key); final List<Country> countries; final User user; final GlobalKey<FormState>? formKey; final bool isPatientAddress; final bool viewDetails; @override _PharmacyAddressFormState createState() => _PharmacyAddressFormState(); } class _PharmacyAddressFormState extends ConsumerState<PharmacyAddressForm> { @override Widget build(BuildContext context) { final checkValidation = ref.watch(checkValidationProvider); final ownChoice = ref.watch(pharmacyOwnChoiceProvider); final pharmacyAddressState = ref.watch(pharmacyAddressInputNotifierProvider); ref.listen<CountriesState>( pharmacyCountriesNotifierProvider, (_, state) { state.maybeWhen( orElse: () {}, data: (countries) { if (widget.isPatientAddress) { ref .read(pharmacyAddressInputNotifierProvider.notifie r) .changeField( country: ref .read(pharmacyCountriesNotifierProvider.noti fier) .getCountryByCodeWithoutState( widget.user.country, countries, ), ); } }, ); }, ); return Form( key: widget.formKey, autovalidateMode: checkValidation ? AutovalidateMode.onUserInteraction : AutovalidateMode.disabled, child: Column( children: [ if (widget.isPatientAddress) PharmacyPatientInfoRow(user: widget.user) else PharmacyNameField( viewDetails: widget.viewDetails, ), const SizedBox(height: 16), Stack( children: [ Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ PharmacyGoogleAddressSearch( user: widget.user, isPatientAddress: widget.isPatientAddress, viewDetails: widget.viewDetails, ), const SizedBox(height: 16), Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( flex: 7, child: AlgeaFieldWithBorder( child: PharmacyLocationTextField( label: S.current.city, initialText: _getCityInitialText( widget.isPatientAddress, widget.viewDetails), autoComplete: true, onChanged: (city) => widget.isPatientAddress ? ref .read( pharmacyAddressInputNotifierProvider .notifier, ) .changeField(city: city) : ref .read( otherPharmacyInputNotifierProvider .notifier, ) .changeField(city: city), validator: ref .read( inputValidatorsNotifierProvider.notifier, ) .validateCity, autocorrect: false, hint: S.current.city_hint, locationFields: ChooseLocationFields.city, verticalPadding: 4.0, backgroundColor: Colors.transparent, enabled: !widget.viewDetails, isPatientAdress: widget.isPatientAddress, ), ), ), const SizedBox(width: 24), Expanded( flex: 3, child: AlgeaFieldWithBorder( child: PharmacyLocationTextField( label: S.current.zip_code, initialText: _getZipCodeInitialText( widget.isPatientAddress, widget.viewDetails), autoComplete: true, onChanged: (zipCode) => widget.isPatientAddress ? ref .read( pharmacyAddressInputNotifierProvider .notifier, ) .changeField(zipCode: zipCode) : ref .read( otherPharmacyInputNotifierProvider .notifier, ) .changeField(zipCode: zipCode), validator: ref .read( inputValidatorsNotifierProvider.notifier, ) .validateZipCode, autocorrect: false, hint: S.current.zip_code_hint, locationFields: ChooseLocationFields.zipCode, verticalPadding: 4.0, backgroundColor: Colors.transparent, enabled: !widget.viewDetails, isPatientAdress: widget.isPatientAddress, ), ), ), ], ), ...[ const SizedBox(height: 16), ConstrainedBox( constraints: getDefaultBoxConstraints(context), child: widget.isPatientAddress ? widget.viewDetails ? AlgeaFieldWithBorder( child: PharmacyLocationTextField( label: S.current.country, initialText: _getCityInitialText( widget.isPatientAddress, widget.viewDetails, ), autoComplete: true, onChanged: (city) {}, validator: (_) { return null; }, autocorrect: false, hint: S.current.select_country, locationFields: ChooseLocationFields.country, verticalPadding: 4.0, backgroundColor: Colors.transparent, enabled: !widget.viewDetails, isPatientAdress: widget.isPatientAddress, ), ) : AlgeaFieldWithBorder( error: ref.watch( pharmacyCountryDropdownErrorProvider, ), child: CustomAlgeaDropdownWithoutForm( hide: !widget.isPatientAddress, hint: S.current.select_country, dropdownItems: widget.countries .map((country) => country.label) .toList(), value: ref .watch( pharmacyAddressInputNotifierProvider, ) .country, buttonWidth: double.infinity, horizontalPadding: 16.0, iconHeight: 12, iconWidth: 12, onChanged: (country) => ref .read( pharmacyAddressInputNotifierProvider .notifier, ) .changeField( country: country, ), backGroundColor: AlgeaColors.white, showBorder: false, error: ref.watch( pharmacyCountryDropdownErrorProvider, ), ), ) : CustomAlgeaDropdownWithoutForm( hide: !widget.isPatientAddress, hint: S.current.select_country, dropdownItems: widget.countries .map((country) => country.label) .toList(), value: ref .watch(pharmacyAddressInputNotifie rProvider) .country, buttonWidth: double.infinity, horizontalPadding: 16.0, iconHeight: 12, iconWidth: 12, onChanged: (country) => ref .read( pharmacyAddressInputNotifierProvider .notifier, ) .changeField( country: country, ), backGroundColor: AlgeaColors.white, showBorder: false, error: ref .watch(pharmacyCountryDropdownErro rProvider), ), ), ], const SizedBox(height: 16), Container( constraints: getDefaultBoxConstraints(context), decoration: const BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(8)), color: AlgeaColors.errors3, ), child: Padding( padding: const EdgeInsets.all(16), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ SvgPicture.asset( ImageAssets.infoCircleRed, color: AlgeaColors.errors1, ), const SizedBox(width: 12), Expanded( child: Text( S.current.prescription_processing_warning, style: getBodySecondaryStyle( color: AlgeaColors.errors1, ), ), ), ], ), ), ), ], ), PharmacyAutoCompleteList( color: Colors.grey.shade100, isPatientAddress: widget.isPatientAddress, ), ], ), const SizedBox(height: 80), GradientActionButton( isBusy: false, text: _getButtonTitle(), onPressed: () => _onButtonPressed(context, ref), buttonColor: widget.viewDetails ? AlgeaColors.primary3 : null, textColor: widget.viewDetails ? AlgeaColors.black : null, ), ], ), ); } void _onButtonPressed(BuildContext context, WidgetRef ref) { ref.watch(currentUserNotifierProvider).maybeWhen( orElse: () {}, data: (user, _) { if (user?.blockPharmacy != null && user?.blockPharmacy == true) { CustomAlgeaDialog( title: S.current.pharmacy_blocked, image: ImageAssets.pharmacyIllustration2, description: S.current.pharmacy_change_not_allowed, firstButtonText: S.current.cancel, //ignore: prefer-extracting-callbacks firstActionButton: () { Navigator.of(context, rootNavigator: true).pop(); context.beamToReplacementNamed(ProfilePage.routeName); }, ).show(context, barrierDismissible: false); } else { if (widget.viewDetails) { context.beamToNamed(PharmacyOptions.routeName); } else { validatePharmacyLocationPage( context, ref, isPatientAddress: widget.isPatientAddress, ); } } }, ); } String _getButtonTitle() { if (widget.viewDetails) return S.current.change_pharmacy; if (widget.isPatientAddress) { return S.current.send_prescription_to_desired_address; } return S.current.send_prescriptions_as_specified; } String? _getCityInitialText(bool isPatientAddress, bool viewDetails) { final pharmacyAddressState = ref.watch(pharmacyAddressInputNotifierProvider); final otherAddressState = ref.watch(otherPharmacyInputNotifierProvider); final ownChoice = ref.watch(pharmacyOwnChoiceProvider); final String? city = isPatientAddress ? pharmacyAddressState.city : viewDetails ? ownChoice?.city : null; if (city != null) return city; if (widget.user.city != null && isPatientAddress) { return widget.user.city ?? ''; } if (!isPatientAddress && !viewDetails) { return ''; } return ownChoice?.city; } String? _getZipCodeInitialText(bool isPatientAddress, bool viewDetails) { final pharmacyAddressState = ref.watch(pharmacyAddressInputNotifierProvider); final otherAddressState = ref.watch(otherPharmacyInputNotifierProvider); final ownChoice = ref.watch(pharmacyOwnChoiceProvider); final String? zipCode = isPatientAddress ? pharmacyAddressState.zipCode : viewDetails ? ownChoice?.zipCode : null; if (zipCode != null) return zipCode; if (widget.user.zip != null && isPatientAddress) { return widget.user.zip ?? ''; } if (!isPatientAddress && !viewDetails) { return ''; } return ownChoice?.zipCode; } }