import 'package:wanisclient/config/custom_text.dart'; import 'package:wanisclient/models/userAddress_model.dart'; import 'package:flutter/material.dart'; import 'package:flutter_lucide/flutter_lucide.dart'; import 'package:latlong2/latlong.dart'; // Changed from google_maps_flutter to latlong2 import 'package:provider/provider.dart'; import '../config/AppSnackbar.dart'; import '../config/language/language_enum.dart'; import '../providers/language_provider.dart'; import '../providers/myAddress_provider.dart'; import '../providers/ride_providers.dart'; import '../utils/colors.dart'; import '../widgets/address_widget.dart'; import '../widgets/inpultAddress_widget.dart'; import '../widgets/map_screen.dart'; import '../widgets/waveAppBarWidget.dart'; class MyAdresses extends StatefulWidget { MyAdresses({super.key}); @override State createState() => _MyAdressesState(); } class _MyAdressesState extends State { final TextEditingController _homeController = TextEditingController(); final TextEditingController _workController = TextEditingController(); String posLocationHome = ''; String posLocationWork = ''; // Add a boolean state variable to manage the loading state bool _isLoading = false; void clear() { _homeController.text = ""; _workController.text = ""; posLocationHome = ""; posLocationWork = ""; } @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { context.read().fetchAddressesHomeWork(); }); } @override void dispose() { _homeController.dispose(); _workController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final addressProvider = Provider.of(context); final languageProvider = context.watch(); bool isArabic = languageProvider.currentLangCode == Language.ar; return SafeArea( child: Scaffold( backgroundColor: Colors.white, body: Stack( // Use a Stack for the full-screen loading overlay children: [ SingleChildScrollView( child: Column( children: [ Padding( padding: const EdgeInsets.symmetric( horizontal: 16, vertical: 16), child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ GestureDetector( onTap: () { Navigator.pop(context); }, child: Container( width: 32, height: 32, margin: const EdgeInsets.symmetric(vertical: 12), decoration: BoxDecoration( color: AppNewColors.lightgray, borderRadius: BorderRadius.circular(16), boxShadow: [ BoxShadow( color: AppNewColors.black.withOpacity(.1), blurRadius: 1, offset: const Offset(2, 1), ), ], ), child: const Icon( Icons.arrow_back, color: AppNewColors.black, size: 16, ), ), ), Expanded( child: Center( child: Txt( text: 'My Addresses', style: TextStyle( color: AppNewColors.titleTextColor, fontSize: 18, fontFamily: isArabic ? "Readex_Pro" : 'Inter', fontWeight: FontWeight.w600, height: 1.23, ), ), ), ), ], ), ), // Main content Padding( padding: const EdgeInsets.only(top: 30.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ /// Updated Field with onTap - Using Mapbox map_screen IconInputField( controller: _homeController, label: "Set home address", height: 60, icon: LucideIcons.house, onTap: () { Navigator.push( context, MaterialPageRoute( builder: (_) => map_screen( on_location_selected: (LatLng? position) async { if (position != null) { print( 'Selected: ${position.latitude}, ${position.longitude}'); _homeController.text = await Provider.of(context, listen: false).getPlaceNameByGoogle(position.latitude, position.longitude); setState(() { posLocationHome = "${position.latitude}, ${position.longitude}"; }); } else { AppSnackbar.showInfo("Info", 'No location selected'); } }, ), ), ); }, ), IconInputField( controller: _workController, label: "Set the workplace address", height: 60, icon: LucideIcons.briefcase_business, onTap: () { Navigator.push( context, MaterialPageRoute( builder: (_) => map_screen( on_location_selected: (LatLng? position) async { if (position != null) { print( 'Selected: ${position.latitude}, ${position.longitude}'); _workController.text = await Provider.of( context, listen: false) .getPlaceNameByGoogle( position.latitude, position.longitude); setState(() { posLocationWork = "${position.latitude}, ${position.longitude}"; }); } else { AppSnackbar.showInfo("Info", 'No location selected'); } }, ), ), ); }, ), Padding( padding: const EdgeInsets.symmetric( horizontal: 38.0, vertical: 10), child: InkWell( // Disable the button while loading onTap: _isLoading ? null : () async { final toSave = [ if (posLocationHome.isNotEmpty) UserAddress( name: _homeController.text, position: posLocationHome, tag: 'HOME'), // Corrected tag if (posLocationWork.isNotEmpty) UserAddress( name: _workController.text, position: posLocationWork, tag: 'WORK'), ]; if (toSave.isEmpty) { AppSnackbar.showInfo("Info", 'Nothing to save'); return; } // Show the loader setState(() { _isLoading = true; }); try { await addressProvider.addAddress(toSave); clear(); } finally { // Hide the loader setState(() { _isLoading = false; }); } }, child: Row( children: [ const Icon( LucideIcons.circle_plus, color: AppNewColors.black, ), const SizedBox(width: 12), // Conditionally show loader or text _isLoading ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator( strokeWidth: 2), ) : const Txt( text: "Add new place", style: TextStyle( fontSize: 16, color: AppNewColors.black,), ) ], ), ), ), const Padding( padding: EdgeInsets.symmetric( horizontal: 28.0, vertical: 10), child: Divider(thickness: 1, color: Colors.grey), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 28.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Txt( text: "My Addresses", style: TextStyle( color: Colors.grey, fontWeight: FontWeight.w800, ), ), // Display Home addresses if the list is NOT empty if (addressProvider.addressesHome.isNotEmpty) ListView.builder( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), itemCount: addressProvider.addressesHome.length > 3 ? 3 : addressProvider .addressesHome.length, itemBuilder: (context, index) { final address = addressProvider.addressesHome[index]; return addressCard( true, address.name, address.position, address.id, address.tag, addressProvider); }, ), // Display Work addresses if the list is NOT empty if (addressProvider.addressesWork.isNotEmpty) ListView.builder( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), itemCount: addressProvider.addressesWork.length > 3 ? 3 : addressProvider .addressesWork.length, itemBuilder: (context, index) { final address = addressProvider.addressesWork[index]; return addressCard( true, address.name, address.position, address.id, address.tag, addressProvider); }, ), // Display a message if both lists are empty if (addressProvider.addressesHome.isEmpty && addressProvider.addressesWork.isEmpty) const Center( child: Padding( padding: EdgeInsets.symmetric(vertical: 20.0), child: Txt(text: "No saved addresses found.",style: TextStyle(),), ), ), ], ), ) ], ), ), ], ), ), // Full-screen loading overlay ], ), ), ); } }