// ignore_for_file: public_member_api_docs, sort_constructors_first import 'dart:async'; import 'dart:developer'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:enaklo_pos/presentation/customer/pages/customer_page.dart'; import 'package:enaklo_pos/presentation/home/models/product_quantity.dart'; import 'package:enaklo_pos/presentation/setting/pages/setting_page.dart'; import 'package:enaklo_pos/presentation/table/pages/table_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:enaklo_pos/data/models/response/table_model.dart'; import 'package:enaklo_pos/core/constants/colors.dart'; import 'package:enaklo_pos/core/extensions/build_context_ext.dart'; import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart'; import 'package:enaklo_pos/presentation/auth/login_page.dart'; import 'package:enaklo_pos/presentation/report/pages/report_page.dart'; import 'package:enaklo_pos/presentation/setting/bloc/sync_order/sync_order_bloc.dart'; import '../../../core/assets/assets.gen.dart'; import '../../auth/bloc/logout/logout_bloc.dart'; import '../bloc/online_checker/online_checker_bloc.dart'; import '../widgets/nav_item.dart'; import 'home_page.dart'; class DashboardPage extends StatefulWidget { final int? index; final TableModel? table; final List? items; const DashboardPage({ super.key, this.index = 0, this.table, this.items, }); @override State createState() => _DashboardPageState(); } class _DashboardPageState extends State { int _selectedIndex = 0; List _pages = []; void _onItemTapped(int index) { _selectedIndex = index; setState(() {}); } late StreamSubscription> _connectivitySubscription; @override void initState() { super.initState(); _selectedIndex = widget.index!; _pages = [ HomePage( isTable: false, table: widget.table, items: widget.items ?? [], ), TablePage( items: widget.items ?? [], ), const ReportPage(), const CustomerPage(), const SettingPage(), ]; // ignore: unused_local_variable _connectivitySubscription = Connectivity() .onConnectivityChanged .listen((List connectivityResult) { if (!mounted) return; // <-- Tambahkan ini! if (connectivityResult.contains(ConnectivityResult.mobile)) { context .read() .add(const OnlineCheckerEvent.check(true)); } else if (connectivityResult.contains(ConnectivityResult.wifi)) { context .read() .add(const OnlineCheckerEvent.check(true)); } else { context .read() .add(const OnlineCheckerEvent.check(false)); } }); } @override void dispose() { _connectivitySubscription.cancel(); // <-- Cancel subscription di dispose super.dispose(); } @override Widget build(BuildContext context) { return SafeArea( child: Scaffold( body: Row( children: [ Expanded( flex: 1, child: SizedBox( height: (context.deviceHeight - 40.0).clamp(0.0, double.infinity), child: ColoredBox( color: AppColors.primary, child: ListView( children: [ NavItem( iconPath: Assets.icons.homeResto.path, isActive: _selectedIndex == 0, onTap: () => _onItemTapped(0), ), NavItem( iconPath: Assets.icons.kelolaProduk.path, isActive: _selectedIndex == 1, onTap: () => _onItemTapped(1), ), NavItem( iconPath: Assets.icons.dashboard.path, isActive: _selectedIndex == 2, onTap: () => _onItemTapped(2), ), NavItem( iconPath: Assets.icons.people.path, isActive: _selectedIndex == 3, onTap: () => _onItemTapped(3), ), NavItem( iconPath: Assets.icons.setting.path, isActive: _selectedIndex == 4, onTap: () => _onItemTapped(4), ), //container flag online/offline BlocBuilder( builder: (context, state) { return state.maybeWhen( orElse: () => Container( width: 40, margin: const EdgeInsets.symmetric(horizontal: 16), padding: const EdgeInsets.all(8.0), decoration: BoxDecoration( color: AppColors.red, borderRadius: BorderRadius.circular(8.0), ), child: const Icon( Icons.signal_wifi_off, color: AppColors.white, ), ), online: () { log("🌐 Dashboard: Internet connection detected, triggering sync"); context.read().add( const SyncOrderEvent.syncOrder(), ); return Container( width: 40, margin: const EdgeInsets.symmetric(horizontal: 16), padding: const EdgeInsets.all(8.0), decoration: BoxDecoration( color: AppColors.green, borderRadius: BorderRadius.circular(8.0), ), child: const Icon( Icons.wifi, color: AppColors.white, ), ); }, ); }, ), BlocListener( listener: (context, state) { state.maybeMap( orElse: () {}, error: (e) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(e.message), backgroundColor: AppColors.red, ), ); }, success: (value) { AuthLocalDataSource().removeAuthData(); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Logout success'), backgroundColor: AppColors.primary, ), ); Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) { return const LoginPage(); })); }, ); }, child: NavItem( iconPath: Assets.icons.logout.path, isActive: false, onTap: () { context .read() .add(const LogoutEvent.logout()); }, ), ), ], ), ), ), ), Expanded( flex: 10, child: _pages[_selectedIndex], ), ], ), ), ); } }