import 'package:flutter/material.dart'; import 'package:medora/controllers/doctor_controller.dart'; import '../../../route/route_names.dart'; class DoctorAddressScreen extends StatefulWidget { final DoctorController? controller; const DoctorAddressScreen({ super.key, required this.controller, }); @override State createState() => _DoctorAddressScreenState(); } class _DoctorAddressScreenState extends State { late DoctorController _controller; late TextEditingController _floorBuildingController; late TextEditingController _streetController; late TextEditingController _cityController; late TextEditingController _stateController; late TextEditingController _countryController; late TextEditingController _postalCodeController; late TextEditingController _addressTypeController; final _formKey = GlobalKey(); String? selectedAddressType; bool showCustomTypeField = false; final List addressTypes = ['Home', 'Office', 'Others']; @override void initState() { super.initState(); _controller = widget.controller ?? DoctorController(); _loadSavedData(); } @override void dispose() { _floorBuildingController.dispose(); _streetController.dispose(); _cityController.dispose(); _stateController.dispose(); _countryController.dispose(); _postalCodeController.dispose(); _addressTypeController.dispose(); super.dispose(); } void _loadSavedData() { final doctor = _controller.model; _floorBuildingController = TextEditingController(text: doctor.floorBuilding ?? ''); _streetController = TextEditingController(text: doctor.street ?? ''); _cityController = TextEditingController(text: doctor.city ?? ''); _stateController = TextEditingController(text: doctor.state ?? ''); _countryController = TextEditingController(text: doctor.country ?? ''); _postalCodeController = TextEditingController(text: doctor.postalCode ?? ''); _addressTypeController = TextEditingController(text: doctor.addressType ?? ''); selectedAddressType = widget.controller?.model.addressType; if (selectedAddressType != null && !addressTypes.contains(selectedAddressType)) { showCustomTypeField = true; } } // bool _validateAndProceed() {if (_formKey.currentState!.validate()) { // // Update the address model // _controller.updateFloorBuilding(_floorBuildingController.text); // _controller.updateStreet(_streetController.text); // _controller.updateCity(_cityController.text); // _controller.updateState(_stateController.text); // _controller.updateCountry(_countryController.text); // _controller.updatePostalCode(_postalCodeController.text); // // Validate the address fields // if (_areFieldsValid()) { // return true; // } // ScaffoldMessenger.of(context).showSnackBar( // const SnackBar(content: Text('Please fill in all required fields')), // ); // return false; // } bool _validateAndProceed() { // if (!_formKey.currentState!.validate()) return false; // if (selectedAddressType == null) { // ScaffoldMessenger.of(context).showSnackBar(const SnackBar( // content: Text('Please select an address type'), // backgroundColor: Colors.red, // )); // } if (_formKey.currentState!.validate()) { _controller.updateFloorBuilding(_floorBuildingController.text); _controller.updateStreet(_streetController.text); _controller.updateCity(_cityController.text); _controller.updateState(_stateController.text); _controller.updateCountry(_countryController.text); _controller.updatePostalCode(_postalCodeController.text); _controller.updateAddressType(_addressTypeController.text); return true; } return false; } bool _areFieldsValid() { return _floorBuildingController.text.isNotEmpty && _streetController.text.isNotEmpty && _cityController.text.isNotEmpty && _stateController.text.isNotEmpty && _countryController.text.isNotEmpty && _postalCodeController.text.isNotEmpty && _addressTypeController.text.isNotEmpty; } // bool _validateAndProceed() { // if (!_formKey.currentState!.validate()) return false; // if (selectedAddressType == null) { // ScaffoldMessenger.of(context).showSnackBar( // const SnackBar( // content: Text('Please select an address type'), // backgroundColor: Colors.red, // ), // ); // return false; // } // return true; // } void _handleAddressTypeSelection(String type) { setState(() { if (type == 'Others') { showCustomTypeField = !showCustomTypeField; if (!showCustomTypeField) { _addressTypeController.clear(); selectedAddressType = null; } } else { showCustomTypeField = false; selectedAddressType = type; widget.controller?.updateAddressType(type); } }); } Widget _buildAddressTypeChips() { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( 'Address Type', style: TextStyle( fontSize: 16, fontWeight: FontWeight.w500, color: Colors.black87, ), ), const SizedBox(height: 8), LayoutBuilder( builder: (context, constraints) { // Use the width of the layout to manage chip sizes final chipMaxWidth = constraints.maxWidth; return Wrap( spacing: 6, // Adjusted spacing to help fit in a single row runSpacing: 6, children: addressTypes.map((type) { final isSelected = !showCustomTypeField && selectedAddressType == type || (type == 'Others' && showCustomTypeField); IconData icon; switch (type) { case 'Home': icon = Icons.home; break; case 'Office': icon = Icons.work; break; case 'Others': icon = Icons.more_horiz; break; default: icon = Icons.location_on; } return Material( color: Colors.transparent, child: InkWell( onTap: () => _handleAddressTypeSelection(type), borderRadius: BorderRadius.circular(20), child: Container( constraints: BoxConstraints( maxWidth: (chipMaxWidth - (addressTypes.length - 1) * 6) / addressTypes.length, ), // Calculate width to fit all chips within row padding: const EdgeInsets.symmetric( horizontal: 12, // Reduced horizontal padding vertical: 8, // Reduced vertical padding ), decoration: BoxDecoration( color: isSelected ? Colors.blue.withOpacity(0.2) : Colors.grey.withOpacity(0.1), borderRadius: BorderRadius.circular(20), border: Border.all( color: isSelected ? Colors.blue : Colors.transparent, ), ), child: Row( mainAxisSize: MainAxisSize.min, children: [ Icon(icon, color: Colors.blue, size: 16), // Smaller icon const SizedBox( width: 4), // Spacing between icon and text Text( type, style: TextStyle( fontSize: 14, // Reduced font size color: isSelected ? Colors.blue : Colors.black87, fontWeight: FontWeight.w500, ), ), ], ), ), ), ); }).toList(), ); }, ), if (showCustomTypeField) ...[ const SizedBox(height: 16), TextFormField( controller: _addressTypeController, decoration: InputDecoration( labelText: 'Custom Address Type', prefixIcon: const Icon(Icons.edit_location_alt, color: Colors.blue), // filled: true, // fillColor: Colors.grey[100], contentPadding: const EdgeInsets.symmetric(vertical: 16, horizontal: 12), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: BorderSide(color: Colors.grey.shade300, width: 1), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: Colors.blue, width: 1.5), ), ), validator: (value) { if (showCustomTypeField && (value == null || value.isEmpty)) { return 'Please enter address type'; } return null; }, onChanged: (value) { _controller.updateAddressType(value); }), ], ], ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( actions: [ IconButton( onPressed: () { if (_validateAndProceed()) { Navigator.pushNamed( context, RouteNames.profileDescriptionScreen, arguments: _controller, ); } }, icon: const Icon(Icons.arrow_forward), ), ], title: const Text('Address Details'), ), body: Form( key: _formKey, child: SingleChildScrollView( padding: const EdgeInsets.all(16.0), child: Container( padding: const EdgeInsets.all(16.0), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), blurRadius: 10, offset: const Offset(0, 4), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildTextField( label: 'Floor, Building', controller: _floorBuildingController, onChanged: (value) => widget.controller!.updateFloorBuilding(value), icon: Icons.apartment, isMandatory: true, ), _buildTextField( label: 'Street or Road', controller: _streetController, onChanged: (value) => widget.controller!.updateStreet(value), icon: Icons.streetview, isMandatory: true, ), _buildTextField( label: 'City', controller: _cityController, onChanged: (value) => widget.controller!.updateCity(value), icon: Icons.location_city, isMandatory: true, ), _buildTextField( label: 'State', controller: _stateController, onChanged: (value) => widget.controller!.updateState(value), icon: Icons.map, isMandatory: true, ), _buildTextField( label: 'Country', controller: _countryController, onChanged: (value) => widget.controller!.updateCountry(value), icon: Icons.flag, isMandatory: true, ), _buildPostalCodeField( label: 'Postal Code', controller: _postalCodeController, onChanged: (value) => widget.controller!.updatePostalCode(value), icon: Icons.mail, isMandatory: true, ), const SizedBox(height: 16), _buildAddressTypeChips(), ], ), ), ), ), ); } Widget _buildTextField({ required String label, required TextEditingController controller, required Function(String) onChanged, required IconData icon, bool isMandatory = false, }) { return Padding( padding: const EdgeInsets.only(bottom: 16.0), child: TextFormField( controller: controller, onChanged: onChanged, validator: (value) { if (isMandatory && (value == null || value.isEmpty)) { return '$label is required'; } if (value != null && value.isNotEmpty && !RegExp(r"^[a-zA-Z0-9\s]+$").hasMatch(value)) { return 'Please enter valid text'; } return null; }, decoration: InputDecoration( labelText: label, prefixIcon: Icon(icon, color: Colors.blue), // filled: true, // fillColor: Colors.grey[100], contentPadding: const EdgeInsets.symmetric(vertical: 20, horizontal: 12), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: BorderSide(color: Colors.grey.shade300, width: 1), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: Colors.blue, width: 1.5), ), errorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: Colors.red, width: 1.5), ), focusedErrorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: Colors.red, width: 1.5), ), ), ), ); } Widget _buildPostalCodeField({ required String label, required TextEditingController controller, required Function(String) onChanged, required IconData icon, bool isMandatory = false, }) { return Padding( padding: const EdgeInsets.only(bottom: 16.0), child: TextFormField( controller: controller, onChanged: onChanged, keyboardType: TextInputType.number, maxLength: 6, validator: (value) { if (isMandatory && (value == null || value.isEmpty)) { return '$label is required'; } if (value != null && !RegExp(r'^[0-9]+$').hasMatch(value)) { return 'Please enter numbers only'; } return null; }, decoration: InputDecoration( labelText: label, prefixIcon: Icon(icon, color: Colors.blue), // filled: true, // fillColor: Colors.grey[100], contentPadding: const EdgeInsets.symmetric(vertical: 20, horizontal: 12), counterText: '', enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: BorderSide(color: Colors.grey.shade300, width: 1), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: Colors.blue, width: 1.5), ), errorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: Colors.red, width: 1.5), ), focusedErrorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8), borderSide: const BorderSide(color: Colors.red, width: 1.5), ), ), ), ); } }