From 76e995acbfb45166a5365a143d26cc944b8f7d7c Mon Sep 17 00:00:00 2001 From: Rafael <1024481@stud.hs-mannheim.de> Date: Fri, 10 May 2024 00:21:23 +0200 Subject: [PATCH] location_dialog --- lib/components/location_dialog.dart | 50 ++++++++++++++ lib/components/location_selector.dart | 55 ++++++---------- lib/pages/user_data_page.dart | 95 ++++++++++++++++++++++----- 3 files changed, 148 insertions(+), 52 deletions(-) create mode 100644 lib/components/location_dialog.dart diff --git a/lib/components/location_dialog.dart b/lib/components/location_dialog.dart new file mode 100644 index 0000000..381c207 --- /dev/null +++ b/lib/components/location_dialog.dart @@ -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 createState() => _LocationDialogState(); +} + +class _LocationDialogState extends State { + 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'), + ), + ], + ); + } +} diff --git a/lib/components/location_selector.dart b/lib/components/location_selector.dart index 2069424..02feda0 100644 --- a/lib/components/location_selector.dart +++ b/lib/components/location_selector.dart @@ -62,20 +62,10 @@ class LocationSelectorState extends State { } 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,33 +111,28 @@ class LocationSelectorState extends State { }); } } 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) { - setState(() { - errorText = 'Error searching location $locationQuery: $e'; - _street = ''; - _country = ''; - _city = ''; - _subLocality = null; - _postalCode = null; - _administrativeArea = null; - _latitude = null; - _longitude = null; - }); + _resetLocationData('Error searching location $locationQuery: $e'); } } + /// 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 void _getCurrentLocation() async { bool serviceEnabled; diff --git a/lib/pages/user_data_page.dart b/lib/pages/user_data_page.dart index c41acc8..5a04307 100644 --- a/lib/pages/user_data_page.dart +++ b/lib/pages/user_data_page.dart @@ -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 { - MyLocation? _selectedLocation; + MyLocation? _primaryLocation; + MyLocation? _secondaryLocation; + List languagesList = []; final List _selectedLanguages = []; @@ -208,6 +210,33 @@ class _UserDataPageState extends State { }); } + // 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 { '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),