Fixed navigation issue

This commit is contained in:
Mohammad Salameh
2024-03-04 15:03:57 +03:00
parent 7ecac02a8a
commit 49b24e14eb
6 changed files with 64 additions and 18 deletions

View File

@ -21,7 +21,7 @@ class DefaultNavBar extends StatelessWidget {
onTap: (int index) {
cubit.updatePageIndex(index, context);
if (DevicesCubit.get(context).chosenCategoryView != null) {
Navigator.pop(context);
DevicesCubit().clearCategoriesSelection(context);
}
},
currentIndex: cubit.getPageIndex,

View File

@ -209,7 +209,7 @@ class DevicesCubit extends Cubit<DevicesState> {
),
];
void selectCategory(int index) {
selectCategory(int index) {
for (var i = 0; i < categories.length; i++) {
if (i == index) {
categories[i].isSelected = true;
@ -220,6 +220,13 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(DevicesCategoryChanged());
}
unselectAllCategories() {
for (var category in categories) {
category.isSelected = false;
}
emit(DevicesCategoryChanged());
}
Widget? get chosenCategoryView {
for (var category in categories) {
if (category.isSelected) {
@ -229,7 +236,7 @@ class DevicesCubit extends Cubit<DevicesState> {
return null;
}
void selectDevice(DeviceModel device) {
selectDevice(DeviceModel device) {
for (var category in categories) {
for (var device in category.devices) {
if (device.isSelected) {
@ -254,7 +261,7 @@ class DevicesCubit extends Cubit<DevicesState> {
return null;
}
void changeCategorySwitchValue(DevicesCategoryModel category) {
changeCategorySwitchValue(DevicesCategoryModel category) {
if (category.devicesStatus != null) {
category.devicesStatus = !category.devicesStatus!;
for (var device in category.devices) {
@ -270,7 +277,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(CategorySwitchChanged());
}
void turnOnOffDevice(DeviceModel device) {
turnOnOffDevice(DeviceModel device) {
device.status = !device.status!;
DevicesCategoryModel category =
categories.firstWhere((category) => category.devices.contains(device));
@ -278,7 +285,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(DeviceSwitchChanged());
}
void updateDevicesStatus(DevicesCategoryModel category) {
updateDevicesStatus(DevicesCategoryModel category) {
bool? tempStatus = category.devices[0].status;
for (var ac in category.devices) {
//check if there any ac have a different status than the initial ==> turn off the universal switch
@ -292,7 +299,7 @@ class DevicesCubit extends Cubit<DevicesState> {
}
}
void turnAllDevicesOff(DevicesCategoryModel category) {
turnAllDevicesOff(DevicesCategoryModel category) {
for (var device in category.devices) {
device.status = false;
}
@ -300,7 +307,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(CategorySwitchChanged());
}
void turnAllDevicesOn(DevicesCategoryModel category) {
turnAllDevicesOn(DevicesCategoryModel category) {
for (var device in category.devices) {
device.status = true;
}
@ -308,7 +315,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(CategorySwitchChanged());
}
void areAllDevicesOff(DevicesCategoryModel category) {
areAllDevicesOff(DevicesCategoryModel category) {
for (var device in category.devices) {
if (device.status ?? false) {
category.devicesStatus = false;
@ -318,12 +325,15 @@ class DevicesCubit extends Cubit<DevicesState> {
}
}
static void clearCategoriesSelection() {
clearCategoriesSelection(BuildContext context) {
for (var category in categories) {
category.isSelected = false;
for (var device in category.devices) {
device.isSelected = false;
}
}
Navigator.popUntil(context, (route) => route.isFirst);
emit(DevicesCategoryChanged());
}
}

View File

@ -9,6 +9,10 @@ import '../../bloc/devices_cubit.dart';
import 'switches.dart';
class DevicesViewBody extends StatelessWidget {
const DevicesViewBody({
super.key,
});
@override
Widget build(BuildContext context) {
return BlocProvider(
@ -127,8 +131,4 @@ class DevicesViewBody extends StatelessWidget {
),
);
}
const DevicesViewBody({
super.key,
});
}

View File

@ -1,7 +1,6 @@
// ignore_for_file: avoid_print
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
class MyBlocObserver extends BlocObserver {
@override
@ -25,9 +24,6 @@ class MyBlocObserver extends BlocObserver {
@override
void onClose(BlocBase bloc) {
super.onClose(bloc);
if (bloc is DevicesCubit) {
DevicesCubit.clearCategoriesSelection();
}
print('onClose -- ${bloc.runtimeType}');
}
}