import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:socialbuddy_mobile/core/constants/colors.dart'; import 'package:socialbuddy_mobile/features/posts/screens/posts_screen.dart'; import 'package:socialbuddy_mobile/features/home/widgets/feature_card.dart'; import 'package:socialbuddy_mobile/features/connect/screens/social_connect_details_screen.dart'; import 'package:socialbuddy_mobile/features/automation/screens/automation_screen.dart'; import 'package:socialbuddy_mobile/features/connect/screens/channels_screen.dart'; import 'package:socialbuddy_mobile/features/profile/screens/profile_screen.dart'; import 'package:socialbuddy_mobile/features/profile/screens/pricing_screen.dart'; class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State { int _selectedIndex = 0; static final List _widgetOptions = [ const DashboardTab(), const SocialMediaConnectScreen(), const ChannelsScreen(), const PostsScreen(), const AutomationScreen(), const ProfileScreen(), ]; void _onItemTapped(int index) { setState(() { _selectedIndex = index; }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: AppColors.darkBg, appBar: AppBar( backgroundColor: AppColors.darkBg, elevation: 0, leading: Padding( padding: const EdgeInsets.all(12.0), child: Image.asset('assets/images/logo_sb.png', errorBuilder: (c, e, s) => const Icon(Icons.hub, color: Colors.blue)), ), actions: [ IconButton( icon: Container( padding: const EdgeInsets.all(6), decoration: BoxDecoration( color: Colors.white.withOpacity(0.05), shape: BoxShape.circle, ), child: const Icon(Icons.person_outline, color: Colors.white70, size: 20), ), onPressed: () => _onItemTapped(5), // Navigate to Profile (index 5) ), const SizedBox(width: 8), ], ), body: _widgetOptions.elementAt(_selectedIndex), bottomNavigationBar: Container( decoration: BoxDecoration( border: Border(top: BorderSide(color: Colors.white.withOpacity(0.05))), ), child: BottomNavigationBar( items: const [ BottomNavigationBarItem( icon: Icon(Icons.grid_view_outlined), activeIcon: Icon(Icons.grid_view), label: 'Home', ), BottomNavigationBarItem( icon: Icon(Icons.link), activeIcon: Icon(Icons.link), label: 'Connect', ), BottomNavigationBarItem( icon: Icon(Icons.account_circle_outlined), activeIcon: Icon(Icons.account_circle), label: 'Channels', ), BottomNavigationBarItem( icon: Icon(Icons.post_add_outlined), activeIcon: Icon(Icons.post_add), label: 'Posts', ), BottomNavigationBarItem( icon: Icon(Icons.comment_outlined), activeIcon: Icon(Icons.comment), label: 'Automation', ), BottomNavigationBarItem( icon: Icon(Icons.person_outline), activeIcon: Icon(Icons.person), label: 'Profile', ), ], currentIndex: _selectedIndex, selectedItemColor: Colors.blueAccent, unselectedItemColor: Colors.white38, showUnselectedLabels: true, type: BottomNavigationBarType.fixed, backgroundColor: AppColors.darkBg, selectedLabelStyle: GoogleFonts.nunito(fontWeight: FontWeight.bold, fontSize: 10), unselectedLabelStyle: GoogleFonts.nunito(fontSize: 10), onTap: _onItemTapped, ), ), ); } } class DashboardTab extends StatelessWidget { const DashboardTab({super.key}); Future _checkAndNavigate(BuildContext context, Widget screen) async { final prefs = await SharedPreferences.getInstance(); final userJson = prefs.getString('user_details'); if (userJson != null) { final user = jsonDecode(userJson); final role = user['role']; if (role == 'customer') { final session = prefs.getString('payment_session'); if (session == null) { Navigator.push( context, MaterialPageRoute(builder: (context) => const PricingScreen()), ); return; } } } if (context.mounted) { Navigator.push( context, MaterialPageRoute(builder: (context) => screen), ); } } @override Widget build(BuildContext context) { // Get the parent state to change tabs final homeState = context.findAncestorStateOfType<_HomeScreenState>(); return SingleChildScrollView( padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( border: Border.all(color: Colors.cyanAccent.withOpacity(0.5)), borderRadius: BorderRadius.circular(8), ), child: const Icon(Icons.add, color: Colors.cyanAccent, size: 24), ), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "Social Buddy", style: GoogleFonts.nunito( color: Colors.white, fontSize: 28, fontWeight: FontWeight.w800, ), ), Text( "Dashboard", style: GoogleFonts.nunito( color: Colors.white, fontSize: 28, fontWeight: FontWeight.w800, height: 1, ), ), ], ), ), ], ), const SizedBox(height: 24), Text( "Explore all tools and features below.", style: GoogleFonts.nunito( color: Colors.white.withOpacity(0.5), fontSize: 16, ), ), const SizedBox(height: 32), FeatureCard( title: "Auto Reply", description: "Automatically reply to comments/messages using smart automation.", icon: Icons.chat_bubble_outline_rounded, iconColor: Colors.blueAccent, onTap: () => _checkAndNavigate(context, const AutomationScreen()), ), FeatureCard( title: "Instagram Channels", description: "Select and connect specific Instagram business accounts to manage.", icon: Icons.account_tree_outlined, iconColor: Colors.pinkAccent, onTap: () => _checkAndNavigate(context, const ChannelsScreen()), ), FeatureCard( title: "Instagram Media", description: "View all Instagram media from your connected accounts with insights.", icon: Icons.camera_alt_outlined, iconColor: Colors.cyanAccent, onTap: () => homeState?._onItemTapped(1), ), FeatureCard( title: "Social Media Posts", description: "Create, schedule, and publish posts for multiple platforms.", icon: Icons.send_rounded, iconColor: Colors.greenAccent, onTap: () => homeState?._onItemTapped(1), ), FeatureCard( title: "Automation Logs", description: "Track auto reply logs and automation actions with timestamps.", icon: Icons.history_rounded, iconColor: Colors.orangeAccent, onTap: () { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text("Automation Logs coming soon!")), ); }, ), FeatureCard( title: "Users", description: "Manage users, roles, and access permissions.", icon: Icons.person_search_outlined, iconColor: Colors.purpleAccent, onTap: () { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text("User Management coming soon!")), ); }, ), FeatureCard( title: "Reports", description: "Analyze insights, performance charts, and engagement metrics.", icon: Icons.auto_graph_rounded, iconColor: Colors.cyanAccent, onTap: () { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text("Reports and Analytics coming soon!")), ); }, ), const SizedBox(height: 40), Center( child: Column( children: [ Text( "© 2025. SocialBuddy All rights reserved.", style: GoogleFonts.nunito(color: Colors.white38, fontSize: 12), ), const SizedBox(height: 4), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "Powered By ", style: GoogleFonts.nunito(color: Colors.white38, fontSize: 12), ), Text( "MetatronCube", style: GoogleFonts.nunito( color: Colors.blueAccent.withOpacity(0.7), fontSize: 12, decoration: TextDecoration.underline, ), ), ], ), ], ), ), const SizedBox(height: 20), ], ), ); } }