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

View File

@ -4,7 +4,7 @@ import 'package:cofounderella/components/my_button.dart';
import 'package:cofounderella/constants.dart';
import 'package:cofounderella/models/language.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/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
@ -21,7 +21,9 @@ class UserDataPage extends StatefulWidget {
enum Gender { none, male, female, divers }
class _UserDataPageState extends State<UserDataPage> {
MyLocation? _selectedLocation;
MyLocation? _primaryLocation;
MyLocation? _secondaryLocation;
List<LanguageSetting> languagesList = [];
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
Widget build(BuildContext context) {
if (languagesList.isEmpty) {
@ -224,23 +253,55 @@ class _UserDataPageState extends State<UserDataPage> {
'Location',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: LocationSelector(
onLocationChanged: (location) {
setState(() {
_selectedLocation = location;
});
},
const SizedBox(height: 20),
// Display selected primary location
const Text(
'Primary Location:',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
// TODO Show and handle selected location
if (_selectedLocation != null) ...[
Text(style: const TextStyle(backgroundColor: Colors.yellow),
'Selected Location: ${_selectedLocation!.toString()}'),
Text(style: const TextStyle(backgroundColor: Colors.yellowAccent),
'Coordinates: ${_selectedLocation!.toStringDegree()}'),
if (_primaryLocation != null) ...[
Text(_primaryLocation!.toString()),
const SizedBox(height: 10),
] else ...[
const Text('n/a'),
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(
'Age',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),