location_dialog

master
Rafael 2024-05-10 00:21:23 +02:00
parent ea81c2ea97
commit 76e995acbf
3 changed files with 148 additions and 52 deletions

View File

@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import '../models/location.dart';
import 'location_selector.dart';
/// Custom dialog widget for LocationSelector
class LocationDialog extends StatefulWidget {
final Function(MyLocation) onLocationSelected;
const LocationDialog({super.key, required this.onLocationSelected});
@override
State<LocationDialog> createState() => _LocationDialogState();
}
class _LocationDialogState extends State<LocationDialog> {
MyLocation? _selectedLocation;
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Select Location'),
content: LocationSelector(
onLocationChanged: (location) {
// Update selectedLocation when location changes
setState(() {
_selectedLocation = location;
});
},
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(); // Close dialog on cancel
},
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: _selectedLocation != null
? () {
// Pass the selected location to caller and close dialog
widget.onLocationSelected(_selectedLocation!);
Navigator.of(context).pop();
}
: null, // Disable button when _selectedLocation is null
child: const Text('Apply'),
),
],
);
}
}

View File

@ -62,20 +62,10 @@ class LocationSelectorState extends State<LocationSelector> {
} }
void _searchLocation() async { void _searchLocation() async {
String locationQuery = _locationController.text; String locationQuery = _locationController.text.trim();
if (locationQuery.trim().isEmpty) { if (locationQuery.isEmpty) {
setState(() { _resetLocationData('Specify an address for the search');
errorText = 'Specify an address for the search';
_street = '';
_country = '';
_city = '';
_subLocality = null;
_postalCode = null;
_administrativeArea = null;
_latitude = null;
_longitude = null;
});
return; return;
} }
@ -121,33 +111,28 @@ class LocationSelectorState extends State<LocationSelector> {
}); });
} }
} else { } else {
setState(() { _resetLocationData('Location $locationQuery not found');
errorText = 'Location $locationQuery not found';
_street = '';
_country = '';
_city = '';
_subLocality = null;
_postalCode = null;
_administrativeArea = null;
_latitude = null;
_longitude = null;
});
} }
} catch (e) { } catch (e) {
setState(() { _resetLocationData('Error searching location $locationQuery: $e');
errorText = 'Error searching location $locationQuery: $e';
_street = '';
_country = '';
_city = '';
_subLocality = null;
_postalCode = null;
_administrativeArea = null;
_latitude = null;
_longitude = null;
});
} }
} }
/// Resets the location data and calls setState()
void _resetLocationData(String errorMessage) {
setState(() {
errorText = errorMessage;
_street = '';
_country = '';
_city = '';
_subLocality = null;
_postalCode = null;
_administrativeArea = null;
_latitude = null;
_longitude = null;
});
}
/// Determine users position using geolocator package /// Determine users position using geolocator package
void _getCurrentLocation() async { void _getCurrentLocation() async {
bool serviceEnabled; bool serviceEnabled;

View File

@ -4,7 +4,7 @@ import 'package:cofounderella/components/my_button.dart';
import 'package:cofounderella/constants.dart'; import 'package:cofounderella/constants.dart';
import 'package:cofounderella/models/language.dart'; import 'package:cofounderella/models/language.dart';
import 'package:cofounderella/models/language_setting.dart'; import 'package:cofounderella/models/language_setting.dart';
import 'package:cofounderella/components/location_selector.dart'; import '../components/location_dialog.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
@ -21,7 +21,9 @@ class UserDataPage extends StatefulWidget {
enum Gender { none, male, female, divers } enum Gender { none, male, female, divers }
class _UserDataPageState extends State<UserDataPage> { class _UserDataPageState extends State<UserDataPage> {
MyLocation? _selectedLocation; MyLocation? _primaryLocation;
MyLocation? _secondaryLocation;
List<LanguageSetting> languagesList = []; List<LanguageSetting> languagesList = [];
final List<Language> _selectedLanguages = []; final List<Language> _selectedLanguages = [];
@ -208,6 +210,33 @@ class _UserDataPageState extends State<UserDataPage> {
}); });
} }
// Function to show location dialog
void _showLocationDialog(bool isPrimary) {
showDialog(
context: context,
builder: (BuildContext context) {
return LocationDialog(
onLocationSelected: (location) {
setState(() {
if (isPrimary) {
_primaryLocation = location;
} else {
_secondaryLocation = location;
}
});
},
);
},
);
}
/// Method to remove secondary location
void _removeSecondaryLocation() {
setState(() {
_secondaryLocation = null;
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (languagesList.isEmpty) { if (languagesList.isEmpty) {
@ -224,23 +253,55 @@ class _UserDataPageState extends State<UserDataPage> {
'Location', 'Location',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
), ),
Padding( const SizedBox(height: 20),
padding: const EdgeInsets.all(16.0), // Display selected primary location
child: LocationSelector( const Text(
onLocationChanged: (location) { 'Primary Location:',
setState(() { style: TextStyle(fontWeight: FontWeight.bold),
_selectedLocation = location;
});
},
),
), ),
// TODO Show and handle selected location if (_primaryLocation != null) ...[
if (_selectedLocation != null) ...[ Text(_primaryLocation!.toString()),
Text(style: const TextStyle(backgroundColor: Colors.yellow), const SizedBox(height: 10),
'Selected Location: ${_selectedLocation!.toString()}'), ] else ...[
Text(style: const TextStyle(backgroundColor: Colors.yellowAccent), const Text('n/a'),
'Coordinates: ${_selectedLocation!.toStringDegree()}'), const SizedBox(height: 10),
], ],
// Button to set primary location
ElevatedButton(
onPressed: () {
_showLocationDialog(true);
},
child: const Text('Set Primary Location'),
),
const SizedBox(height: 20),
// Display selected secondary location
if (_secondaryLocation != null) ...[
const Text(
'Secondary Location:',
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(_secondaryLocation!.toString()),
const SizedBox(height: 10),
],
if (_primaryLocation != null) ...[
// Button to set secondary location
ElevatedButton(
onPressed: () {
_showLocationDialog(false);
},
child: Text(_secondaryLocation != null
? 'Set Secondary Location'
: 'Add Secondary Location'),
),
],
// Display selected secondary location or remove button
if (_secondaryLocation != null) ...[
ElevatedButton(
onPressed: _removeSecondaryLocation,
child: const Text('Remove Secondary Location'),
),
],
const SizedBox(height: 20),
const Text( const Text(
'Age', 'Age',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),