import 'package:flutter/material.dart';
import '../../../app/theme.dart';
import '../../../shared/widgets/brand_mark.dart';
import '../../../core/auth/auth_store.dart';
import '../../../core/app_version.dart';
import '../../../core/sync/master_data_sync.dart';
import '../../../core/sync/report_asset_sync.dart';
import '../../../core/sync/report_sync.dart';
import '../../../core/dashboard/web_dashboard_store.dart';
import '../../customers/data/customer_store.dart';
import '../../employees/data/employee_store.dart';
import '../../profile/data/company_store.dart';
import '../../reports/data/demo_report_store.dart';
import '../../reports/data/report_photo_store.dart';
import '../../jobs/data/job_store.dart';
import '../../jobs/presentation/jobs_screen.dart';
import '../../customers/presentation/customers_screen.dart';
import '../../profile/presentation/profile_screen.dart';
import '../../reports/presentation/create_report_screen.dart';
import '../../reports/presentation/reports_screen.dart';
import 'dashboard_screen.dart';
class AppShell extends StatefulWidget {
const AppShell({super.key});
@override
State<AppShell> createState() => _AppShellState();
}
class _AppShellState extends State<AppShell> {
int _selectedIndex = 0;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
await CustomerStore.instance.initialize();
await JobStore.instance.initialize();
await EmployeeStore.instance.initialize();
await CompanyStore.instance.initialize();
await DemoReportStore.instance.initialize();
await ReportPhotoStore.instance.initialize();
await MasterDataSyncController.instance.initialize();
await ReportSyncController.instance.initialize();
await ReportSyncController.instance.synchronize();
await ReportAssetSyncController.instance.initialize();
await ReportAssetSyncController.instance.synchronize();
await WebDashboardStore.instance.load();
});
}
static const _destinations = [
NavigationDestination(
icon: Icon(Icons.space_dashboard_outlined),
selectedIcon: Icon(Icons.space_dashboard_rounded),
label: 'Übersicht',
),
NavigationDestination(
icon: Icon(Icons.work_outline_rounded),
selectedIcon: Icon(Icons.work_rounded),
label: 'Aufträge',
),
NavigationDestination(
icon: Icon(Icons.people_outline_rounded),
selectedIcon: Icon(Icons.people_rounded),
label: 'Kunden',
),
NavigationDestination(
icon: Icon(Icons.description_outlined),
selectedIcon: Icon(Icons.description_rounded),
label: 'Nachweise',
),
NavigationDestination(
icon: Icon(Icons.business_outlined),
selectedIcon: Icon(Icons.business_rounded),
label: 'Firma',
),
];
Future<void> _createReport() async {
final saved = await Navigator.of(
context,
).push<bool>(MaterialPageRoute(builder: (_) => const CreateReportScreen()));
if (saved == true && mounted) {
setState(() => _selectedIndex = 3);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Nachweis wurde als lokaler Demo-Entwurf gespeichert.'),
),
);
}
}
@override
Widget build(BuildContext context) {
final screens = [
DashboardScreen(onCreateReport: _createReport),
const JobsScreen(),
const CustomersScreen(),
ReportsScreen(onCreate: _createReport),
const ProfileScreen(),
];
return LayoutBuilder(
builder: (context, constraints) {
final wide = constraints.maxWidth >= 840;
final content = IndexedStack(index: _selectedIndex, children: screens);
return Scaffold(
appBar: wide
? null
: AppBar(
title: const BrandMark(),
backgroundColor: Colors.white,
surfaceTintColor: Colors.transparent,
),
body: wide
? Row(
children: [
Container(
width: 250,
color: AppColors.forest,
padding: const EdgeInsets.symmetric(vertical: 28),
child: Column(
children: [
const Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
child: BrandMark(light: true),
),
const SizedBox(height: 36),
Expanded(
child: NavigationRail(
backgroundColor: Colors.transparent,
extended: true,
minExtendedWidth: 230,
selectedIndex: _selectedIndex,
onDestinationSelected: (value) =>
setState(() => _selectedIndex = value),
indicatorColor: AppColors.lime,
selectedIconTheme: const IconThemeData(
color: AppColors.ink,
),
selectedLabelTextStyle: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
),
unselectedIconTheme: const IconThemeData(
color: Colors.white70,
),
unselectedLabelTextStyle: const TextStyle(
color: Colors.white70,
),
destinations: _destinations
.map(
(item) => NavigationRailDestination(
icon: item.icon,
selectedIcon: item.selectedIcon,
label: Text(item.label),
),
)
.toList(),
),
),
ListenableBuilder(
listenable: AuthStore.instance,
builder: (_, _) => ListTile(
onTap: AuthStore.instance.logout,
leading: CircleAvatar(
backgroundColor: AppColors.lime,
child: Text(
(AuthStore.instance.session?.email ?? 'M')
.substring(0, 1)
.toUpperCase(),
style: TextStyle(
color: AppColors.ink,
fontWeight: FontWeight.w800,
),
),
),
title: Text(
AuthStore.instance.session?.email ??
'Angemeldet',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
),
),
subtitle: Text(
AuthStore.instance.session?.role ?? 'Rolle',
style: TextStyle(color: Colors.white60),
),
),
),
const Padding(
padding: EdgeInsets.fromLTRB(20, 8, 20, 12),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
AppVersion.label,
style: TextStyle(
color: Colors.white54,
fontSize: 11,
fontWeight: FontWeight.w600,
),
),
),
),
],
),
),
Expanded(child: content),
],
)
: content,
bottomNavigationBar: wide
? null
: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Padding(
padding: EdgeInsets.only(top: 4, bottom: 4),
child: Text(
AppVersion.label,
style: TextStyle(
color: Color(0xFF6A756F),
fontSize: 11,
fontWeight: FontWeight.w600,
),
),
),
NavigationBar(
selectedIndex: _selectedIndex,
onDestinationSelected: (value) =>
setState(() => _selectedIndex = value),
destinations: _destinations,
),
],
),
);
},
);
}
}
