Profile
Profile
dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import '../services/auth_service.dart';
import 'upload_id_screen.dart';
@override
_ProfileCompletionScreenState createState() => _ProfileCompletionScreenState();
}
try {
User? user = FirebaseAuth.instance.currentUser;
if (user != null) {
String? profileImageUrl;
if (_profileImage != null) {
profileImageUrl = await _uploadImageToStorage(
_profileImage,
'users/${user.uid}/profile_image.jpg',
);
}
String? frontIDImageUrl;
String? backIDImageUrl;
if (_idImages['frontID'] != null) {
frontIDImageUrl = await _uploadImageToStorage(
_idImages['frontID'],
'users/${user.uid}/front_id_image.jpg',
);
}
if (_idImages['backID'] != null) {
backIDImageUrl = await _uploadImageToStorage(
_idImages['backID'],
'users/${user.uid}/back_id_image.jpg',
);
}
await FirebaseFirestore.instance.collection('users').doc(user.uid).set({
'name': _nameController.text,
'phone': _phoneController.text,
'propertyType': _selectedPropertyType,
'city': _selectedCity,
'area': _selectedArea,
'preferredLivingType': _preferredLivingType,
'budget': _budgetController.text,
'profileImage': profileImageUrl,
'frontIDImage': frontIDImageUrl,
'backIDImage': backIDImageUrl,
});
if (mounted) {
Navigator.pushReplacementNamed(context, '/suggest-action');
}
} else {
setState(() {
_isLoading = false;
_errorMessage = 'User not authenticated';
});
}
} catch (e) {
setState(() {
_isLoading = false;
_errorMessage = 'Failed to save profile data: $e';
});
}
}
void _skipAndContinue() {
if (mounted) {
Navigator.pushReplacementNamed(context, '/suggest-action');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Card(
elevation: 8,
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Complete Your Profile',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.black,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 20),
const Text(
'Help us personalize your experience by answering a few quick
questions.',
style: TextStyle(
fontSize: 16,
color: Colors.grey,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 30),
// Profile Image
GestureDetector(
onTap: _pickProfileImage,
child: CircleAvatar(
radius: 50,
backgroundImage: _profileImage != null ?
FileImage(_profileImage!) : null,
child: _profileImage == null ? const Icon(Icons.camera_alt,
size: 40) : null,
),
),
const SizedBox(height: 20),
// Name
TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: 'Your Name',
prefixIcon: const Icon(Icons.person),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
),
),
const SizedBox(height: 20),
// Phone Number
TextField(
controller: _phoneController,
keyboardType: TextInputType.phone,
decoration: InputDecoration(
labelText: 'Phone Number',
prefixIcon: const Icon(Icons.phone),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
),
),
const SizedBox(height: 20),
// Property Type
DropdownButtonFormField<String>(
decoration: InputDecoration(
labelText: 'Preferred Property Type',
prefixIcon: const Icon(Icons.home_outlined),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
),
value: _selectedPropertyType,
hint: const Text('Select property type'),
items: _propertyTypes.map((String type) {
return DropdownMenuItem<String>(
value: type,
child: Text(type),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
_selectedPropertyType = newValue;
});
},
),
const SizedBox(height: 20),
// City
DropdownButtonFormField<String>(
decoration: InputDecoration(
labelText: 'City',
prefixIcon: const Icon(Icons.location_city),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
),
value: _selectedCity,
hint: const Text('Select city'),
items: _cities.map((String city) {
return DropdownMenuItem<String>(
value: city,
child: Text(city),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
_selectedCity = newValue;
_selectedArea = null; // Reset area when city changes
});
},
),
const SizedBox(height: 20),
// Area
DropdownButtonFormField<String>(
decoration: InputDecoration(
labelText: 'Area',
prefixIcon: const Icon(Icons.place),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
),
value: _selectedArea,
hint: const Text('Select area'),
items: _selectedCity != null
? _availableAreas.map((String area) {
return DropdownMenuItem<String>(
value: area,
child: Text(area),
);
}).toList()
: [],
onChanged: _selectedCity != null
? (String? newValue) {
setState(() {
_selectedArea = newValue;
});
}
: null,
),
const SizedBox(height: 20),
// Preferred Living Type
DropdownButtonFormField<String>(
decoration: InputDecoration(
labelText: 'Preferred Living Type',
prefixIcon: const Icon(Icons.home),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
),
value: _preferredLivingType,
hint: const Text('Select living type'),
items: ['Investment', 'Family'].map((String type) {
return DropdownMenuItem<String>(
value: type,
child: Text(type),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
_preferredLivingType = newValue;
});
},
),
const SizedBox(height: 20),
// Budget
TextField(
controller: _budgetController,
keyboardType: const TextInputType.numberWithOptions(decimal:
true),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d*'))
],
decoration: InputDecoration(
labelText: 'Your Budget (EGP)',
prefixIcon: const Icon(Icons.attach_money),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
),
),
const SizedBox(height: 30),
if (_errorMessage.isNotEmpty)
Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: Text(
_errorMessage,
style: const TextStyle(color: Colors.red, fontSize: 14),
textAlign: TextAlign.center,
),
),
// Save and Continue Button ()وظيفته دلوقتي الحفظ بس
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _isLoading ? null : _saveProfileAndContinue,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: _isLoading
? const CircularProgressIndicator(
color: Colors.white,
)
: const Text(
'Save and Continue',
style: TextStyle(
fontSize: 18,
color: Colors.white,
),
),
),
),
const SizedBox(height: 15),
// Skip for Now Link
GestureDetector(
onTap: _isLoading ? null : _skipAndContinue,
child: const Text(
'Skip for Now',
style: TextStyle(
fontSize: 16,
color: Colors.blue,
fontWeight: FontWeight.w500,
),
),
),
],
),
),
),
),
),
);
}
@override
void dispose() {
_budgetController.dispose();
_nameController.dispose();
_phoneController.dispose();
super.dispose();
}
}