From 42051bb977db60cad0f1e46ca8aa22c29b3bbf40 Mon Sep 17 00:00:00 2001 From: Mohammad Salameh Date: Tue, 19 Mar 2024 13:29:12 +0300 Subject: [PATCH] Fixed the overflow in the login screen, the page is scrollable. - Fixed Scrolling wasent working in login screen - Changed Home page to indexed based view instead of PageView --- lib/features/app_layout/bloc/nav_cubit.dart | 7 +- lib/features/app_layout/view/app_layout.dart | 12 +- .../app_layout/view/widgets/app_body.dart | 6 +- .../auth/view/widgets/login/login_view.dart | 64 ++-- lib/features/devices/bloc/devices_cubit.dart | 296 ++---------------- .../view/widgets/lights/lights_view.dart | 4 +- .../view/widgets/lights/lights_view_list.dart | 2 +- .../devices/view/widgets/wizard_switches.dart | 121 +++---- lib/my_app.dart | 5 - lib/services/api/http_service.dart | 4 +- 10 files changed, 148 insertions(+), 373 deletions(-) diff --git a/lib/features/app_layout/bloc/nav_cubit.dart b/lib/features/app_layout/bloc/nav_cubit.dart index ebcf53a..b485ad8 100644 --- a/lib/features/app_layout/bloc/nav_cubit.dart +++ b/lib/features/app_layout/bloc/nav_cubit.dart @@ -17,10 +17,8 @@ class NavCubit extends Cubit { static NavCubit of(context) => BlocProvider.of(context); - //functoin to do the important work when the user logs out static clear() { pageIndex = 0; - pageController.jumpToPage(0); } static int pageIndex = 0; @@ -146,12 +144,9 @@ class NavCubit extends Cubit { const MenuView(), ]; - static final PageController pageController = PageController(); - void updatePageIndex(int index) { pageIndex = index; - pageController.animateToPage(index, - duration: const Duration(milliseconds: 150), curve: Curves.easeIn); + emit(NavChangePage()); } } diff --git a/lib/features/app_layout/view/app_layout.dart b/lib/features/app_layout/view/app_layout.dart index bce053e..dbb164c 100644 --- a/lib/features/app_layout/view/app_layout.dart +++ b/lib/features/app_layout/view/app_layout.dart @@ -6,6 +6,7 @@ import 'package:syncrow_app/features/app_layout/bloc/spaces_cubit.dart'; import 'package:syncrow_app/features/app_layout/view/widgets/app_body.dart'; import 'package:syncrow_app/features/app_layout/view/widgets/default_app_bar.dart'; import 'package:syncrow_app/features/app_layout/view/widgets/default_nav_bar.dart'; +import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart'; import 'package:syncrow_app/navigation/routing_constants.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; @@ -14,8 +15,15 @@ class AppLayout extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => SpacesCubit(), + return MultiBlocProvider( + providers: [ + BlocProvider( + create: (context) => SpacesCubit(), + ), + BlocProvider( + create: (context) => DevicesCubit(), + ), + ], child: BlocListener( listener: (context, state) { if (state is SpacesError) { diff --git a/lib/features/app_layout/view/widgets/app_body.dart b/lib/features/app_layout/view/widgets/app_body.dart index 49cec88..0ba7833 100644 --- a/lib/features/app_layout/view/widgets/app_body.dart +++ b/lib/features/app_layout/view/widgets/app_body.dart @@ -38,11 +38,7 @@ class AppBody extends StatelessWidget { }, builder: (context, state) { return state is! SpacesLoading || state is! SpaceRoomsLoading - ? PageView( - physics: const NeverScrollableScrollPhysics(), - controller: NavCubit.pageController, - children: NavCubit.of(context).pages, - ) + ? NavCubit.of(context).pages[NavCubit.pageIndex] : const Center(child: CircularProgressIndicator()); }, ), diff --git a/lib/features/auth/view/widgets/login/login_view.dart b/lib/features/auth/view/widgets/login/login_view.dart index 34c00ab..06713c2 100644 --- a/lib/features/auth/view/widgets/login/login_view.dart +++ b/lib/features/auth/view/widgets/login/login_view.dart @@ -33,40 +33,40 @@ class LoginView extends StatelessWidget { } }, builder: (context, state) { - return Scaffold( - body: Stack( - children: [ - Container( - width: MediaQuery.sizeOf(context).width, - height: MediaQuery.sizeOf(context).height, - decoration: const BoxDecoration( - image: DecorationImage( - image: AssetImage( - Assets.imagesBackground, + return SafeArea( + child: Scaffold( + body: Stack( + children: [ + Container( + width: MediaQuery.sizeOf(context).width, + height: MediaQuery.sizeOf(context).height, + decoration: const BoxDecoration( + image: DecorationImage( + image: AssetImage( + Assets.imagesBackground, + ), + fit: BoxFit.cover, ), - fit: BoxFit.cover, ), ), - ), - Container( - width: MediaQuery.sizeOf(context).width, - height: MediaQuery.sizeOf(context).height, - decoration: const BoxDecoration( - image: DecorationImage( - image: AssetImage(Assets.imagesVector), - fit: BoxFit.cover, - opacity: 0.9, + Container( + width: MediaQuery.sizeOf(context).width, + height: MediaQuery.sizeOf(context).height, + decoration: const BoxDecoration( + image: DecorationImage( + image: AssetImage(Assets.imagesVector), + fit: BoxFit.cover, + opacity: 0.9, + ), ), ), - ), - Padding( - padding: const EdgeInsets.symmetric( - horizontal: Constants.defaultPadding, - ), - child: SingleChildScrollView( - child: SizedBox( - width: MediaQuery.sizeOf(context).width, - height: MediaQuery.sizeOf(context).height, + Padding( + padding: const EdgeInsets.only( + right: Constants.defaultPadding, + left: Constants.defaultPadding, + top: Constants.defaultPadding, + ), + child: SingleChildScrollView( child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, @@ -99,9 +99,9 @@ class LoginView extends StatelessWidget { ), ), ), - ), - ) - ], + ) + ], + ), ), ); }, diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index 8105505..2f3c7e1 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -1,4 +1,4 @@ -// ignore_for_file: constant_identifier_names +// ignore_for_file: constant_identifier_names, unused_import import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; @@ -22,273 +22,51 @@ part 'devices_state.dart'; class DevicesCubit extends Cubit { DevicesCubit() : super(DevicesInitial()) { - fetchGroups(SpacesCubit.selectedSpace!); + fetchGroups(SpacesCubit.selectedSpace!.id!); } static DevicesCubit get(context) => BlocProvider.of(context); - static List allCategories = [ - // DevicesCategoryModel( - // devices: [ - // DeviceModel( - // name: "Living Room AC", - // id: 0, - // functions: [], - // status: false, - // temperature: 20, - // fanSpeed: 0, - // tempMode: 0, - // coolTo: 20, - // type: DeviceType.AC, - // image: '', - // timer: null, - // bounds: Bounds( - // min: 20, - // max: 30, - // ), - // ), - // DeviceModel( - // name: "Master Bedroom AC", - // id: 1, - // functions: [], - // status: false, - // temperature: 20, - // fanSpeed: 0, - // tempMode: 0, - // coolTo: 20, - // type: DeviceType.AC, - // image: '', - // timer: null, - // bounds: Bounds( - // min: 20, - // max: 30, - // ), - // ), - // DeviceModel( - // name: "Kitchen AC", - // id: 2, - // functions: [], - // status: false, - // temperature: 20, - // fanSpeed: 0, - // tempMode: 0, - // coolTo: 20, - // type: DeviceType.AC, - // image: '', - // timer: null, - // bounds: Bounds( - // min: 20, - // max: 30, - // ), - // ), - // DeviceModel( - // name: "Bathroom AC", - // id: 3, - // functions: [], - // status: false, - // temperature: 20, - // fanSpeed: 0, - // tempMode: 0, - // coolTo: 20, - // type: DeviceType.AC, - // image: '', - // timer: null, - // bounds: Bounds( - // min: 20, - // max: 30, - // ), - // ), - // ], - // icon: Assets.iconsAC, - // name: 'ACs', - // type: DeviceType.AC, - // page: const ACsView(), - // ), - // DevicesCategoryModel( - // devices: [ - // DeviceModel( - // name: "Living Room Light", - // id: 0, - // functions: [], - // status: false, - // color: 0, - // brightness: 20, - // lightingMode: 1, - // timer: null, - // type: DeviceType.Lights, - // image: '', - // recentColors: [ - // 0xFF83D9FF, - // 0xFFFC3E81, - // 0xFFC0FF66, - // 0xFFFDC242, - // ], - // ), - // DeviceModel( - // name: "Master Bedroom Light", - // id: 1, - // functions: [], - // status: false, - // color: 2, - // brightness: 40, - // lightingMode: 1, - // timer: null, - // type: DeviceType.Lights, - // image: '', - // recentColors: [ - // 0xFF83D9FF, - // 0xFFFC3E81, - // 0xFFC0FF66, - // 0xFFFDC242, - // ], - // ), - // DeviceModel( - // name: "Kitchen Light", - // id: 2, - // functions: [], - // status: false, - // color: 1, - // brightness: 60, - // lightingMode: 1, - // timer: null, - // type: DeviceType.Lights, - // image: '', - // recentColors: [ - // 0xFF83D9FF, - // 0xFFFC3E81, - // 0xFFC0FF66, - // 0xFFFDC242, - // ], - // ), - // DeviceModel( - // name: "Bathroom Light", - // id: 3, - // functions: [], - // status: false, - // color: 3, - // brightness: 80, - // lightingMode: 1, - // timer: null, - // type: DeviceType.Lights, - // image: '', - // recentColors: [ - // 0xFF83D9FF, - // 0xFFFC3E81, - // 0xFFC0FF66, - // 0xFFFDC242, - // ], - // ), - // DeviceModel( - // name: "Balcony Light", - // id: 4, - // functions: [], - // status: false, - // color: 4, - // brightness: 100, - // lightingMode: 1, - // timer: null, - // type: DeviceType.Lights, - // image: '', - // recentColors: [ - // 0xFF83D9FF, - // 0xFFFC3E81, - // 0xFFC0FF66, - // 0xFFFDC242, - // ], - // ), - // ], - // icon: Assets.iconsLight, - // name: 'Lights', - // type: DeviceType.Lights, - // page: const LightsView(), - // ), - // DevicesCategoryModel( - // devices: [], - // icon: Assets.iconsDoorLock, - // name: 'Doors', - // type: DeviceType.DoorLock, - // page: const DoorView(), - // ), - // DevicesCategoryModel( - // devices: [ - // DeviceModel( - // openPercentage: 10, - // id: 1, - // functions: [], - // name: "Living Room Curtain", - // status: false, - // type: DeviceType.Curtain, - // image: '', - // timer: null, - // ), - // DeviceModel( - // openPercentage: 20, - // id: 2, - // functions: [], - // name: "Master Bedroom Curtain", - // status: false, - // type: DeviceType.Curtain, - // image: '', - // timer: null, - // ), - // ], - // icon: Assets.iconsCurtain, - // name: 'Curtains', - // type: DeviceType.Curtain, - // page: const CurtainView(), - // ), - // DevicesCategoryModel( - // devices: [], - // icon: Assets.icons3GangSwitch, - // name: 'Bedroom Lights', - // type: DeviceType.ThreeGang, - // page: const LightSwitchesView(), - // ), - // DevicesCategoryModel( - // devices: [], - // icon: Assets.iconsGateway, - // name: 'Gateway', - // type: DeviceType.Gateway, - // page: const GateWayView(), - // ), - ]; + static List? allCategories; selectCategory(int index) { - for (var i = 0; i < allCategories.length; i++) { + for (var i = 0; i < allCategories!.length; i++) { if (i == index) { - allCategories[i].isSelected = true; + allCategories![i].isSelected = true; } else { - allCategories[i].isSelected = false; + allCategories![i].isSelected = false; } } emit(DevicesCategoryChanged()); } unselectAllCategories() { - for (var category in allCategories) { + for (var category in allCategories!) { category.isSelected = false; } emit(DevicesCategoryChanged()); } Widget? get chosenCategoryView { - for (var category in allCategories) { - if (category.isSelected) { - switch (category.type) { - case DeviceType.AC: - return const ACsView(); - case DeviceType.Lights: - return const LightsView(); - case DeviceType.DoorLock: - return const DoorView(); - case DeviceType.Curtain: - return const CurtainView(); - case DeviceType.ThreeGang: - return const LightSwitchesView(); - case DeviceType.Gateway: - return const GateWayView(); - default: - return null; + if (allCategories != null) { + for (var category in allCategories!) { + if (category.isSelected) { + switch (category.type) { + case DeviceType.AC: + return const ACsView(); + case DeviceType.Lights: + return const LightsView(); + case DeviceType.DoorLock: + return const DoorView(); + case DeviceType.Curtain: + return const CurtainView(); + case DeviceType.ThreeGang: + return const LightSwitchesView(); + case DeviceType.Gateway: + return const GateWayView(); + default: + return null; + } } } } @@ -296,7 +74,7 @@ class DevicesCubit extends Cubit { } DevicesCategoryModel? get chosenCategory { - for (var category in allCategories) { + for (var category in allCategories!) { if (category.isSelected) { return category; } @@ -305,7 +83,7 @@ class DevicesCubit extends Cubit { } selectDevice(DeviceModel device) { - for (var category in allCategories) { + for (var category in allCategories!) { if (category.devices != null) { for (var device in category.devices!) { if (device.isSelected) { @@ -321,7 +99,7 @@ class DevicesCubit extends Cubit { } DeviceModel? getSelectedDevice() { - for (var category in allCategories) { + for (var category in allCategories!) { if (category.devices != null) { for (var device in category.devices!) { if (device.isSelected) { @@ -356,7 +134,7 @@ class DevicesCubit extends Cubit { turnOnOffDevice(DeviceModel device) { device.status = !device.status!; - DevicesCategoryModel category = allCategories.firstWhere((category) { + DevicesCategoryModel category = allCategories!.firstWhere((category) { if (category.devices != null) { return category.devices!.contains(device); } else { @@ -427,7 +205,7 @@ class DevicesCubit extends Cubit { } clearCategoriesSelection(BuildContext context) { - for (var category in allCategories) { + for (var category in allCategories!) { category.isSelected = false; if (category.devices != null) { for (var device in category.devices!) { @@ -450,16 +228,10 @@ class DevicesCubit extends Cubit { } } - fetchGroups( - SpaceModel space, - ) async { + fetchGroups(int spaceId) async { try { - if (state is! DevicesCategoriesLoading) { - emit(DevicesCategoriesLoading()); - allCategories = await DevicesAPI.fetchGroups(space.id!); - } else { - return; - } + emit(DevicesCategoriesLoading()); + allCategories = await DevicesAPI.fetchGroups(spaceId); emit(DevicesCategoriesSuccess()); } on DioException catch (error) { emit( diff --git a/lib/features/devices/view/widgets/lights/lights_view.dart b/lib/features/devices/view/widgets/lights/lights_view.dart index 694131b..80d3a23 100644 --- a/lib/features/devices/view/widgets/lights/lights_view.dart +++ b/lib/features/devices/view/widgets/lights/lights_view.dart @@ -27,8 +27,8 @@ class LightsView extends StatelessWidget { as DeviceModel; } List lights = []; - if (DevicesCubit.allCategories[1].devices != null) { - for (var device in DevicesCubit.allCategories[1].devices!) { + if (DevicesCubit.allCategories![1].devices != null) { + for (var device in DevicesCubit.allCategories![1].devices!) { lights.add(device); } } diff --git a/lib/features/devices/view/widgets/lights/lights_view_list.dart b/lib/features/devices/view/widgets/lights/lights_view_list.dart index e86f888..2348dd1 100644 --- a/lib/features/devices/view/widgets/lights/lights_view_list.dart +++ b/lib/features/devices/view/widgets/lights/lights_view_list.dart @@ -30,7 +30,7 @@ class LightsViewList extends StatelessWidget { children: [ const BodySmall(text: "All Lights"), UniversalSwitch( - category: DevicesCubit.allCategories[1], + category: DevicesCubit.allCategories![1], ), LightsList(lights: lights), ], diff --git a/lib/features/devices/view/widgets/wizard_switches.dart b/lib/features/devices/view/widgets/wizard_switches.dart index 006567f..8a200d2 100644 --- a/lib/features/devices/view/widgets/wizard_switches.dart +++ b/lib/features/devices/view/widgets/wizard_switches.dart @@ -18,71 +18,80 @@ class WizartSwitches extends StatelessWidget { Widget build(BuildContext context) { return BlocBuilder( builder: (context, state) { - return state is DevicesLoading - ? const Center(child: CircularProgressIndicator()) - : GridView.builder( - gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( - crossAxisCount: 2, - crossAxisSpacing: 10, - mainAxisSpacing: 10, - childAspectRatio: 1.5, - ), - padding: const EdgeInsets.only(top: 10), - physics: const NeverScrollableScrollPhysics(), - shrinkWrap: true, - itemCount: DevicesCubit.allCategories.length, - itemBuilder: (_, index) { - return InkWell( - onTap: () { - DevicesCubit.get(context).selectCategory(index); - //Navigate to the chosen category view without animation + return state is! DevicesLoading + ? DevicesCubit.allCategories != null + ? GridView.builder( + gridDelegate: + const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 2, + crossAxisSpacing: 10, + mainAxisSpacing: 10, + childAspectRatio: 1.5, + ), + padding: const EdgeInsets.only(top: 10), + physics: const NeverScrollableScrollPhysics(), + shrinkWrap: true, + itemCount: DevicesCubit.allCategories!.length, + itemBuilder: (_, index) { + return InkWell( + onTap: () { + DevicesCubit.get(context).selectCategory(index); + //Navigate to the chosen category view without animation - Navigator.push(context, - CustomPageRoute(builder: (context) { - return DevicesCubit.get(context).chosenCategoryView!; - })); - }, - child: DefaultContainer( - child: Padding( - padding: - const EdgeInsets.only(top: 10, right: 10, left: 10), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Row( + Navigator.push(context, + CustomPageRoute(builder: (context) { + return DevicesCubit.get(context) + .chosenCategoryView!; + })); + }, + child: DefaultContainer( + child: Padding( + padding: const EdgeInsets.only( + top: 10, right: 10, left: 10), + child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - SvgPicture.asset( - DevicesCubit.allCategories[index].icon!, - fit: BoxFit.contain, + Row( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisAlignment: + MainAxisAlignment.spaceBetween, + children: [ + SvgPicture.asset( + DevicesCubit.allCategories![index].icon!, + fit: BoxFit.contain, + ), + CustomSwitch( + category: + DevicesCubit.allCategories![index], + ), + ], ), - CustomSwitch( - category: DevicesCubit.allCategories[index], + Expanded( + child: FittedBox( + fit: BoxFit.scaleDown, + child: BodyLarge( + text: DevicesCubit + .allCategories![index].name!, + style: context.bodyLarge.copyWith( + fontWeight: FontWeight.bold, + height: 0, + fontSize: 24, + color: Colors.grey, + ), + ), + ), ), ], ), - Expanded( - child: FittedBox( - fit: BoxFit.scaleDown, - child: BodyLarge( - text: DevicesCubit.allCategories[index].name!, - style: context.bodyLarge.copyWith( - fontWeight: FontWeight.bold, - height: 0, - fontSize: 24, - color: Colors.grey, - ), - ), - ), - ), - ], + ), ), - ), - ), - ); - }, + ); + }, + ) + : const SizedBox.shrink() + : const Center( + child: CircularProgressIndicator(), ); }, ); diff --git a/lib/my_app.dart b/lib/my_app.dart index 8e88cdb..99375cc 100644 --- a/lib/my_app.dart +++ b/lib/my_app.dart @@ -5,8 +5,6 @@ import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; import 'package:syncrow_app/utils/resource_manager/theme_manager.dart'; - -import 'features/devices/bloc/devices_cubit.dart'; import 'navigation/router.dart' as router; import 'navigation/routing_constants.dart'; @@ -30,9 +28,6 @@ class MyApp extends StatelessWidget { lazy: false, create: (context) => NavCubit(), ), - BlocProvider( - create: (context) => DevicesCubit(), - ), ], child: MaterialApp( debugShowCheckedModeBanner: false, diff --git a/lib/services/api/http_service.dart b/lib/services/api/http_service.dart index c1dd03f..a5f24bd 100644 --- a/lib/services/api/http_service.dart +++ b/lib/services/api/http_service.dart @@ -38,8 +38,8 @@ class HTTPService { queryParameters: queryParameters, ); - debugPrint("status code is ${response.statusCode}"); - debugPrint("response data is ${response.data}"); + // debugPrint("status code is ${response.statusCode}"); + // debugPrint("response data is ${response.data}"); return expectedResponseModel(response.data); } catch (error) { debugPrint("******* Error");