import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'models/responsive_policy.dart'; class ScaffoldWithNavBar extends StatefulWidget { const ScaffoldWithNavBar({super.key, required this.child}); final Widget child; @override State createState() => _ScaffoldWithNavBarState(); } class _ScaffoldWithNavBarState extends State { bool _isLoggedIn = false; late String initials; @override void initState() { super.initState(); init(); } Future init() async { var prefs = await SharedPreferences.getInstance(); if (prefs.getString("token") != null) { setState(() { _isLoggedIn = true; initials = prefs.getString("initials")!; }); } } @override Widget build(BuildContext context) { return LayoutBuilder( builder: (context, constraints) { if (constraints.maxWidth > ResponsivePolicy.md) { return Scaffold( body: SafeArea( child: Row( children: [ NavigationRail( extended: true, leading: Image( image: AssetImage('assets/velocity.png'), width: 150, ), trailing: _buildLogin(context), labelType: NavigationRailLabelType.none, indicatorColor: Theme.of( context, ).colorScheme.primary.withAlpha(30), onDestinationSelected: (index) { _handleTap(index, context); }, selectedIndex: _calculateSelectedIndex(context), destinations: _buildNavigationRailDestinations(), ), VerticalDivider( thickness: 1, width: 1, color: Theme.of(context).colorScheme.primary.withAlpha(30), ), Expanded(child: widget.child), ], ), ), ); } return Scaffold( body: widget.child, bottomNavigationBar: NavigationBar( indicatorColor: Theme.of(context).colorScheme.primary.withAlpha(30), onDestinationSelected: (index) { _handleTap(index, context); }, selectedIndex: _calculateSelectedIndex(context), destinations: _buildNavigationDestinations(), ), ); }, ); } List _buildNavigationRailDestinations() { List destinations = []; for (var destination in _buildDestinationData()) { destinations.add( NavigationRailDestination( icon: destination["icon"] as Widget, selectedIcon: destination["selectedIcon"] as Widget, label: Text(destination["label"], style: TextStyle(fontSize: 16)), ), ); } return destinations; } List _buildNavigationDestinations() { List destinations = []; for (var destination in _buildDestinationData()) { destinations.add( NavigationDestination( icon: destination["icon"] as Widget, selectedIcon: destination["selectedIcon"] as Widget, label: destination["label"] as String, ), ); } return destinations; } List> _buildDestinationData() { return [ { "icon": Icon(Icons.home_outlined), "selectedIcon": Icon(Icons.home), "label": 'Home', }, { "icon": Icon(Icons.group_outlined), "selectedIcon": Icon(Icons.group), "label": 'Groups', }, { "icon": Icon(Icons.history_outlined), "selectedIcon": Icon(Icons.history), "label": 'History', }, ]; } Widget _buildLogin(BuildContext context) { if (_isLoggedIn) { return SizedBox(); } return Padding( padding: const EdgeInsets.symmetric(vertical: 20), child: Column( children: [ Container( width: 220, decoration: BoxDecoration( border: Border.all(color: Colors.black87.withAlpha(20)), borderRadius: BorderRadius.circular(12), ), padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20), child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.spaceBetween, spacing: 10, children: [ Text( 'Register or Login for more features', style: TextStyle(fontSize: 16), ), InkWell( onTap: () { context.push('/onboarding/landing'); }, borderRadius: BorderRadius.circular(12), child: Container( padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6), decoration: BoxDecoration( color: Theme.of( context, ).colorScheme.primary.withValues(alpha: 0.15), borderRadius: BorderRadius.circular(12), ), child: Text('Get Started', style: TextStyle(fontSize: 14)), ), ), ], ), ), SizedBox(height: 10), ], ), ); } void _handleTap(int index, BuildContext context) { switch (index) { case 0: context.go('/home'); break; case 1: context.go('/groups'); break; case 2: context.go('/history'); break; } } int _calculateSelectedIndex(BuildContext context) { final String location = GoRouterState.of(context).uri.path; if (location.startsWith('/groups')) return 1; if (location.startsWith('/history')) return 2; return 0; } }