import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:responsive_builder/responsive_builder.dart'; import '../../widgets/custom_text_field.dart'; import '../../widgets/custom_text_itnerary_sub.dart'; class MiscellaneousScreen extends StatefulWidget { final Map? apiData; final Function(bool) onClose; final Function(Map) onSaveMiscellaneous; final Map? selectedItem; final int? selectedIndex; final String? loginUser; MiscellaneousScreen({ required this.onClose, required this.apiData, required this.onSaveMiscellaneous, this.selectedItem, this.selectedIndex,required this.loginUser}); @override _MiscellaneousScreenState createState() => _MiscellaneousScreenState(); } class _MiscellaneousScreenState extends State { final GlobalKey _formKey = GlobalKey(); Map selectedValues = {}; final FocusNode _tripTypeFocusNode = FocusNode(); final FocusNode _hotelNameFocusNode = FocusNode(); final FocusNode _commentsFocusNode = FocusNode(); late Map _controllers; late TextEditingController _tripTypeController = TextEditingController(); late TextEditingController _commentsController = TextEditingController(); bool _isHotelNameFocused = false; bool _commentsFocus = false; String? selectedSpecialType; Map errorMessages = {}; Map get miscellaneousData { Map data = { "special_request": selectedSpecialType, "comments": _commentsController.text, "created_by": widget.loginUser, "updated_by": widget.loginUser, }; if (widget.selectedItem != null) { if (widget.selectedItem?["indx"] != null && widget.selectedItem?["indx"] != 0) { data["indx"] = widget.selectedItem!["indx"]; } else if (widget.selectedItem?["miscellaneous_id"] != null && widget.selectedItem?["miscellaneous_id"] != 0) { data["miscellaneous_id"] = widget.selectedItem!["miscellaneous_id"]; } } return data; } @override void initState() { super.initState(); if (widget.selectedItem == null) { _commentsController.clear(); } _hotelNameFocusNode.addListener(() { setState(() { _isHotelNameFocused = _hotelNameFocusNode.hasFocus; }); }); _commentsFocusNode.addListener(() { setState(() { _commentsFocus = _commentsFocusNode.hasFocus; }); }); _commentsController = TextEditingController(text: widget.selectedItem?["comments"] ?? ""); // Set the selected value if available if (widget.selectedItem != null && widget.selectedItem!["special_request"] != null) { selectedSpecialType = widget.selectedItem!["special_request"].toString(); } } @override void dispose() { _tripTypeFocusNode.dispose(); _tripTypeController.dispose(); _commentsController.dispose(); super.dispose(); } bool isValidData(Map data) { errorMessages.clear(); // Reset errors // Required fields that must not be empty List requiredFields = ["special_request", "comments"]; // Check validation for each field for (String field in requiredFields) { if (data[field] == null || data[field].toString().trim().isEmpty) { errorMessages[field] = "This field is required"; } } return errorMessages.isEmpty; // Valid if there are no errors } void handleSave(){ print( "Handle Save miscellaneousData $miscellaneousData"); Map data = miscellaneousData; if (!isValidData(data)) { print("Validation Failed: Required fields are missing."); setState(() {}); return; // Stop execution if validation fails }else { widget.onSaveMiscellaneous(miscellaneousData); // Send object to parent } widget.onClose(false);// Close screen after saving // Clear only if this is a new entry // if (widget.selectedItem == null) { // _commentsController.clear(); // } } @override Widget build(BuildContext context) { return ResponsiveBuilder(builder: (context, sizingInfo) { bool isMobile = sizingInfo.isMobile; bool isDesktop = sizingInfo.deviceScreenType == DeviceScreenType.desktop; return Container( color: Color(0xFFF4F4FB), child: Form( key: _formKey, child: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ Align( alignment: Alignment.centerRight, child: InkWell( onTap: () { _commentsController.clear(); widget.onClose(false); }, child: Icon( Icons.close, size: 18, color: Color(0xFF575A74), ), ), ), Text("Miscellaneous Booking List", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold,color: Color(0xFF575A74))), SizedBox( height: 6, ), Padding( padding: const EdgeInsets.all(28.0), child: Center( child: Column(children: _buildAccomadtionForm(isDesktop)), ), ) ], ), ), ), ); }); } List _buildAccomadtionForm (bool isDesktop) { List buildResponsiveRow(List children) { return [ isDesktop ? Row(children: children) : Column(children: children), SizedBox(height: 10), ]; } return [ ...buildResponsiveRow(_buildFirstRow(isDesktop)), ...buildResponsiveRow(_buildThirdRow(isDesktop)), // Actions row remains a Row Row( mainAxisAlignment: MainAxisAlignment.end, children: _handleAction(isDesktop), ), ]; } List _buildFirstRow(isDesktop) { return [ Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "Special Type", style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xFF575A74)), ), SizedBox(height: 5), isDesktop ? Row(children: _buildTripType(isDesktop) ) : Column( children: _buildTripType(isDesktop) ) ], ), if (isDesktop) Spacer() else SizedBox( height: 8, ), ]; } List _buildTripType(bool isDesktop){ List purposeList = widget.apiData?['miscellaneous_special_request'] ?? []; // selectedInsuranceType = purposeList.isNotEmpty ? purposeList.first['dropdown_value'] : null; List> dropdownItems = purposeList .map((item)=>DropdownMenuItem( value: item['dropdown_key'], child: Text(item['dropdown_value']), )).toList(); if (dropdownItems.isEmpty) { dropdownItems.add( DropdownMenuItem( value: null, child: Text("No options available", style: TextStyle(color: Colors.grey)), ), ); } // Default selected value selectedSpecialType ??= dropdownItems.isNotEmpty ? dropdownItems.first.value : "No options"; return [ CustomTextFieldWrapper( isFocused: _isHotelNameFocused, isDesktop: isDesktop, child: SizedBox( height: 40, child: DropdownButtonFormField( focusNode: _tripTypeFocusNode, // Assign the correct focus node value: selectedSpecialType, style: TextStyle(fontSize: 12), decoration: InputDecoration( border: InputBorder.none, contentPadding: EdgeInsets.symmetric( horizontal: 10), // Proper padding ), onChanged: purposeList.isNotEmpty ? (newValue) { setState(() { selectedSpecialType = newValue; }); print(selectedSpecialType); } : null, items: dropdownItems, ), ), ), if (errorMessages["special_request"] != null) ...[ SizedBox(height: 5), // Space before error message Text( "Select Type", style: TextStyle(color: Colors.red, fontSize: 12), ), ], ]; } List _buildThirdRow(bool isDesktop) { return [ Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "Comments", style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xFF575A74)), ), SizedBox(height: 5), CustomTextFieldWrapper( isFocused: _commentsFocus, // Dropdown doesn't use focus isDesktop: isDesktop, width: isDesktop ? MediaQuery.of(context).size.width * 0.4 : MediaQuery.of(context).size.width * 0.66, child: TextField( focusNode: _commentsFocusNode, controller: _commentsController, maxLines: 6, keyboardType: TextInputType.multiline, style: TextStyle(fontSize: 12), decoration: InputDecoration( labelText: "Description", labelStyle: TextStyle(fontSize: 12, color: Colors.grey), floatingLabelBehavior: FloatingLabelBehavior.never, border: InputBorder.none, contentPadding: EdgeInsets.symmetric(vertical: 4), ), ), ), if (errorMessages["comments"] != null) ...[ SizedBox(height: 5), // Space before error message Text( "Required", style: TextStyle(color: Colors.red, fontSize: 12), ), ], ], ) ]; } List _handleAction(bool isDesktop) { return [ // Close Button ElevatedButton( onPressed: () { _commentsController.clear(); widget.onClose(false);// Close the dialog or screen }, style: ElevatedButton.styleFrom( backgroundColor: Colors.grey[400], // Light grey color shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12), ), child: Text( "Close", style: TextStyle(color: Colors.white, fontSize: 14), ), ), SizedBox(width: 10), // Space between buttons // Save Changes Button ElevatedButton( onPressed: () { handleSave(); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.blue, // Primary color for save shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12), ), child: Text( "Save Changes", style: TextStyle(color: Colors.white, fontSize: 14), ), ), ]; } }