import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'package:provider/provider.dart'; import '../providers/auth_provider.dart'; import '../providers/user_profile_provider.dart'; import 'edit_profile_screen.dart'; import 'login_screen.dart'; class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) async { final auth = Provider.of(context, listen: false); while (!auth.initialized) { await Future.delayed(const Duration(milliseconds: 100)); } final profile = Provider.of(context, listen: false); profile.init(context); await profile.loadProfile(context); }); } Future _pickImage() async { try { final picked = await ImagePicker().pickImage( source: ImageSource.gallery, imageQuality: 80, ); if (picked != null) { final file = File(picked.path); final profile = Provider.of(context, listen: false); profile.init(context); final ok = await profile.updateProfileImage(context, file); if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(ok ? "Profile updated" : "Failed to update")), ); } } catch (e) { if (!mounted) return; ScaffoldMessenger.of(context) .showSnackBar(SnackBar(content: Text("Error: $e"))); } } Future _logout() async { final auth = Provider.of(context, listen: false); final confirm = await showDialog( context: context, builder: (_) => AlertDialog( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)), title: const Text("Logout"), content: const Text("Are you sure you want to logout?"), actions: [ TextButton(onPressed: () => Navigator.pop(context, false), child: const Text("Cancel")), TextButton( onPressed: () => Navigator.pop(context, true), child: const Text("Logout", style: TextStyle(color: Colors.red))), ], ), ); if (confirm == true) { await auth.logout(context); if (!mounted) return; Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (_) => const LoginScreen()), (r) => false, ); } } // ------------------------- REUSABLE FIELD ROW ------------------------- Widget _fieldRow(IconData icon, String label, String value, double scale) { return Padding( padding: EdgeInsets.symmetric(vertical: 12 * scale), child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [ Icon(icon, size: 26 * scale, color: Colors.blueGrey.shade700), SizedBox(width: 14 * scale), Expanded( child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(label, style: TextStyle( fontSize: 14 * scale, color: Colors.grey[700], fontWeight: FontWeight.w700)), SizedBox(height: 4 * scale), Text(value, style: TextStyle( fontSize: 16 * scale, fontWeight: FontWeight.bold)), ]), ) ]), ); } // ------------------------- INFO TILE ------------------------- Widget _infoTile(IconData icon, String title, String value, double scale) { return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Icon(icon, size: 26 * scale, color: Colors.orange.shade800), SizedBox(width: 14 * scale), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(title, style: TextStyle( fontSize: 13 * scale, fontWeight: FontWeight.w600, color: Colors.black54)), SizedBox(height: 4 * scale), Text(value, style: TextStyle( fontSize: 17 * scale, fontWeight: FontWeight.bold)) ]), ) ], ); } @override Widget build(BuildContext context) { final profile = Provider.of(context); if (profile.loading || profile.profile == null) { return const Scaffold(body: Center(child: CircularProgressIndicator())); } // ----------- RESPONSIVE SCALE ----------- final width = MediaQuery.of(context).size.width; final scale = (width / 390).clamp(0.80, 1.25); final p = profile.profile!; final img = p.profileImage; final name = p.customerName ?? "Unknown"; final email = p.email ?? "Not provided"; final status = p.status ?? "Active"; final cid = p.customerId ?? "—"; final company = p.companyName ?? "—"; final type = p.customerType ?? "—"; final mobile = p.mobile ?? "—"; final address = p.address ?? "Not provided"; final pincode = p.pincode ?? "—"; final isPartner = type.toLowerCase().contains("partner"); return Scaffold( backgroundColor: const Color(0xFFE9F2FF), body: SafeArea( child: SingleChildScrollView( padding: EdgeInsets.all(18 * scale), child: Column( children: [ // -------------------- PROFILE SECTION -------------------- Center( child: Column( children: [ GestureDetector( onTap: _pickImage, child: Stack( clipBehavior: Clip.none, children: [ CircleAvatar( radius: 64 * scale, backgroundColor: Colors.grey[200], backgroundImage: img != null ? NetworkImage(img) : null, child: img == null ? Icon(Icons.person, size: 70 * scale, color: Colors.grey[600]) : null, ), // ------------------ FIXED STATUS BADGE ------------------ Positioned( bottom: 8 * scale, right: 8 * scale, child: Container( padding: EdgeInsets.symmetric( horizontal: 12 * scale, vertical: 6 * scale), decoration: BoxDecoration( color: status.toLowerCase() == 'active' ? Colors.green : Colors.orange, borderRadius: BorderRadius.circular(20 * scale), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.2), blurRadius: 8 * scale) ], ), child: Text( status, style: TextStyle( color: Colors.white, fontSize: 13 * scale, fontWeight: FontWeight.bold, ), ), ), ) ], ), ), SizedBox(height: 14 * scale), Text(name, style: TextStyle( fontSize: 20 * scale, fontWeight: FontWeight.bold)), SizedBox(height: 6 * scale), Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.email, size: 18 * scale, color: Colors.blueGrey), SizedBox(width: 8 * scale), Text(email, style: TextStyle( fontSize: 14 * scale, color: Colors.grey[700])), ], ), ], ), ), SizedBox(height: 26 * scale), // ---------------------- YELLOW SUMMARY CARD ---------------------- Container( padding: EdgeInsets.all(18 * scale), decoration: BoxDecoration( gradient: const LinearGradient( colors: [Color(0xFFFFF8A3), Color(0xFFFFE275)], ), borderRadius: BorderRadius.circular(16 * scale), boxShadow: [ BoxShadow( color: Colors.orange.withOpacity(0.25), blurRadius: 14 * scale, offset: Offset(0, 8 * scale)) ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _infoTile(Icons.badge, "Customer ID", cid, scale), Divider(height: 30 * scale), _infoTile(Icons.business, "Company Name", company, scale), Divider(height: 30 * scale), _infoTile(Icons.category, "Customer Type", type, scale), SizedBox(height: 20 * scale), if (isPartner) Container( padding: EdgeInsets.all(14 * scale), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(14 * scale), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.08), blurRadius: 10 * scale) ], ), child: Row(children: [ Icon(Icons.workspace_premium, size: 32 * scale, color: Colors.amber[800]), SizedBox(width: 12 * scale), Text("Partner", style: TextStyle( fontSize: 16 * scale, fontWeight: FontWeight.bold)), ]), ), ]), ), SizedBox(height: 24 * scale), // ---------------------- DETAILS CARD ---------------------- Container( padding: EdgeInsets.all(16 * scale), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(14 * scale), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.06), blurRadius: 12 * scale) ]), child: Column( children: [ _fieldRow(Icons.phone_android, "Mobile", mobile, scale), const Divider(), _fieldRow(Icons.location_on, "Address", address, scale), const Divider(), _fieldRow(Icons.local_post_office, "Pincode", pincode, scale), SizedBox(height: 20 * scale), Row(children: [ Expanded( child: ElevatedButton.icon( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (_) => const EditProfileScreen())); }, icon: Icon(Icons.edit, color: Colors.white, size: 18 * scale), label: Text("Edit Profile", style: TextStyle( color: Colors.white, fontSize: 14 * scale)), style: ElevatedButton.styleFrom( backgroundColor: Colors.blue[700], padding: EdgeInsets.symmetric(vertical: 14 * scale), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12 * scale)), ), )), SizedBox(width: 12 * scale), Expanded( child: ElevatedButton.icon( onPressed: _logout, icon: Icon(Icons.logout, size: 18 * scale, color: Colors.white), label: Text("Logout", style: TextStyle( color: Colors.white, fontSize: 14 * scale)), style: ElevatedButton.styleFrom( backgroundColor: Colors.red[600], padding: EdgeInsets.symmetric(vertical: 14 * scale), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12 * scale)), ), )), ]) ], ), ), SizedBox(height: 30 * scale), ], ), ), ), ); } }