diff --git a/lib/view_models/service_view_model.dart b/lib/view_models/service_view_model.dart index 959f02a..836f739 100644 --- a/lib/view_models/service_view_model.dart +++ b/lib/view_models/service_view_model.dart @@ -275,11 +275,7 @@ class ServiceVM extends BaseVM { context, allowMultiple: false, ); - if (files != null && files.any((element) => - element.path - .split('.') - .last - .toLowerCase() != 'pdf')) { + if (files != null && files.any((element) => element.path.split('.').last.toLowerCase() != 'pdf')) { Utils.showToast("Only PDF Files are allowed"); return; } @@ -531,10 +527,10 @@ class ServiceVM extends BaseVM { DropValue( element.id ?? 0, ((element.categoryName!.isEmpty - ? "N/A" - : countryCode == "SA" - ? element.categoryNameN - : element.categoryName) ?? + ? "N/A" + : countryCode == "SA" + ? element.categoryNameN + : element.categoryName) ?? "N/A"), "", ), @@ -841,9 +837,7 @@ class ServiceVM extends BaseVM { File file = File(imageModel.filePath!); List imageBytes = await file.readAsBytes(); String image = base64Encode(imageBytes); - String fileName = file.path - .split('/') - .last; + String fileName = file.path.split('/').last; branchPostingImages = BranchPostingImages( imageName: fileName, imageStr: image, diff --git a/lib/views/location_views/pick_location_page.dart b/lib/views/location_views/pick_location_page.dart index db0dcef..5a28c57 100644 --- a/lib/views/location_views/pick_location_page.dart +++ b/lib/views/location_views/pick_location_page.dart @@ -37,6 +37,8 @@ class _PickLocationPageState extends State { late LatLng currentPosition; Completer mapController = Completer(); final ValueNotifier _counter = ValueNotifier(""); + final ValueNotifier _isLoadingAddress = ValueNotifier(false); + String actualAddress = ""; // Store the actual address to return @override void initState() { @@ -74,7 +76,7 @@ class _PickLocationPageState extends State { Expanded( child: Stack( children: [ - if (appMap != null) appMap, + appMap, ValueListenableBuilder( builder: (BuildContext context, String value, Widget? child) { // This builder will only get called when the _counter @@ -86,13 +88,42 @@ class _PickLocationPageState extends State { child: Card( child: Padding( padding: const EdgeInsets.all(12.0), - child: Text(value ?? ""), + child: Text(value), )), ) : Container(); }, valueListenable: _counter, ), + ValueListenableBuilder( + builder: (BuildContext context, bool isLoading, Widget? child) { + return isLoading + ? Container( + width: double.infinity, + margin: const EdgeInsets.only(top: 70), + alignment: Alignment.topCenter, + child: const Card( + child: Padding( + padding: EdgeInsets.all(12.0), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + SizedBox( + width: 20, + height: 20, + child: CircularProgressIndicator(strokeWidth: 2), + ), + SizedBox(width: 12), + Text('Loading address...'), + ], + ), + ), + ), + ) + : const SizedBox.shrink(); + }, + valueListenable: _isLoadingAddress, + ), const Align( alignment: Alignment.center, child: Icon( @@ -108,12 +139,20 @@ class _PickLocationPageState extends State { width: double.infinity, child: Padding( padding: const EdgeInsets.all(12.0), - child: ShowFillButton( - maxHeight: 55, - title: LocaleKeys.pickAddress.tr(), - onPressed: () { - widget.onPickAddress(latitude, longitude, _counter.value); - pop(context); + child: ValueListenableBuilder( + valueListenable: _isLoadingAddress, + builder: (context, isLoading, child) { + return ShowFillButton( + maxHeight: 55, + title: isLoading ? 'Loading Address...' : LocaleKeys.pickAddress.tr(), + isDisabled: isLoading || _counter.value.isEmpty, + onPressed: () { + if (!isLoading && _counter.value.isNotEmpty) { + widget.onPickAddress(latitude, longitude, actualAddress); + pop(context); + } + }, + ); }, ), ), @@ -157,8 +196,33 @@ class _PickLocationPageState extends State { } updateAddress(double latitude, double longitude) async { - List placemarks = await placemarkFromCoordinates(latitude, longitude); - _counter.value = '${placemarks.first.street}, ${placemarks.first.subLocality}, ${placemarks.first.locality}, ${placemarks.first.postalCode}, ${placemarks.first.country}'; + _isLoadingAddress.value = true; + + try { + List placemarks = await placemarkFromCoordinates(latitude, longitude).timeout( + const Duration(seconds: 5), + onTimeout: () { + throw TimeoutException('Geocoding request timed out'); + }, + ); + + if (placemarks.isNotEmpty) { + final placemark = placemarks.first; + // Restore original format - building address string from individual fields + actualAddress = '${placemark.street}, ${placemark.subLocality}, ${placemark.locality}, ${placemark.postalCode}, ${placemark.country}'; + _counter.value = actualAddress; + } else { + actualAddress = ""; + _counter.value = ""; + } + } catch (e) { + log('Error getting address: $e'); + // On error, leave address empty - user can try again by moving the map + actualAddress = ""; + _counter.value = ""; + } finally { + _isLoadingAddress.value = false; + } } setMap() {