import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; class ProfileScreen extends ConsumerStatefulWidget { const ProfileScreen({super.key}); @override ConsumerState createState() => _ProfileScreenState(); } class _ProfileScreenState extends ConsumerState { bool _isEditing = false; final _nameController = TextEditingController(); String? _photoUrl; bool _isLoading = true; @override void initState() { super.initState(); _loadProfile(); } Future _loadProfile() async { final user = FirebaseAuth.instance.currentUser; if (user != null) { _nameController.text = user.displayName ?? ''; _photoUrl = user.photoURL; } setState(() => _isLoading = false); } Future _saveProfile() async { final user = FirebaseAuth.instance.currentUser; if (user != null) { await user.updateDisplayName(_nameController.text); await FirebaseFirestore.instance .collection('users') .doc(user.uid) .update({ 'displayName': _nameController.text, 'updatedAt': Timestamp.now(), }); setState(() => _isEditing = false); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('✅ Профиль сохранён'), backgroundColor: Colors.green, ), ); } } } @override Widget build(BuildContext context) { if (_isLoading) { return const Scaffold( body: Center(child: CircularProgressIndicator()), ); } final user = FirebaseAuth.instance.currentUser; return Scaffold( appBar: AppBar( title: const Text('Профиль'), actions: [ IconButton( icon: Icon(_isEditing ? Icons.check : Icons.edit), onPressed: () { if (_isEditing) { _saveProfile(); } else { setState(() => _isEditing = true); } }, tooltip: _isEditing ? 'Сохранить' : 'Редактировать', ), ], ), body: ListView( padding: const EdgeInsets.all(16), children: [ // Аватар Center( child: Stack( children: [ CircleAvatar( radius: 60, backgroundColor: Colors.blue, backgroundImage: _photoUrl != null ? NetworkImage(_photoUrl!) : null, child: _photoUrl == null ? Text( user?.displayName?.substring(0, 1).toUpperCase() ?? 'U', style: const TextStyle( fontSize: 40, color: Colors.white, fontWeight: FontWeight.bold, ), ) : null, ), if (_isEditing) Positioned( bottom: 0, right: 0, child: Container( padding: const EdgeInsets.all(4), decoration: const BoxDecoration( color: Colors.blue, shape: BoxShape.circle, ), child: const Icon( Icons.camera_alt, color: Colors.white, size: 20, ), ), ), ], ), ), const SizedBox(height: 24), // Имя TextField( controller: _nameController, enabled: _isEditing, decoration: const InputDecoration( labelText: 'Имя', border: OutlineInputBorder(), prefixIcon: Icon(Icons.person), ), ), const SizedBox(height: 16), // Email ListTile( leading: const Icon(Icons.email), title: const Text('Email'), subtitle: Text(user?.email ?? 'Не указан'), enabled: false, ), // UID ListTile( leading: const Icon(Icons.fingerprint), title: const Text('User ID'), subtitle: Text(user?.uid ?? 'Неизвестно'), enabled: false, ), const Divider(height: 32), // Статистика _buildStatCard( 'Статистика', [ _buildStatRow('Задач создано', '12'), _buildStatRow('Mind-карт', '5'), _buildStatRow('Будильников', '3'), _buildStatRow('Дней в приложении', '30'), ], ), const SizedBox(height: 24), // Кнопка выхода OutlinedButton.icon( onPressed: () async { await FirebaseAuth.instance.signOut(); }, icon: const Icon(Icons.logout, color: Colors.red), label: const Text('Выйти', style: TextStyle(color: Colors.red)), ), ], ), ); } Widget _buildStatCard(String title, List children) { return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Theme.of(context).cardColor, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.05), blurRadius: 8, offset: const Offset(0, 2), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( title, style: Theme.of(context).textTheme.titleMedium?.copyWith( fontWeight: FontWeight.bold, ), ), const SizedBox(height: 16), ...children, ], ), ); } Widget _buildStatRow(String label, String value) { return Padding( padding: const EdgeInsets.symmetric(vertical: 4), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(label, style: TextStyle(color: Colors.grey[600])), Text( value, style: const TextStyle( fontWeight: FontWeight.bold, fontSize: 16, ), ), ], ), ); } }