Refactor UI structure in curtain and light widgets

Simplify UI structure in curtain_view.dart and light_interface.dart by
rearranging the widget tree for better readability and maintainability.
This commit is contained in:
Mohammad Salameh
2024-04-07 12:50:12 +03:00
parent af7b497cdb
commit 6dfce18703
4 changed files with 41 additions and 42 deletions

View File

@ -24,7 +24,6 @@ part 'devices_state.dart';
class DevicesCubit extends Cubit<DevicesState> { class DevicesCubit extends Cubit<DevicesState> {
DevicesCubit._() : super(DevicesInitial()) { DevicesCubit._() : super(DevicesInitial()) {
print('device cubit created');
if (HomeCubit.getInstance().selectedSpace != null) { if (HomeCubit.getInstance().selectedSpace != null) {
fetchGroups(HomeCubit.getInstance().selectedSpace!.id!); fetchGroups(HomeCubit.getInstance().selectedSpace!.id!);
for (var room in HomeCubit.getInstance().selectedSpace!.rooms!) { for (var room in HomeCubit.getInstance().selectedSpace!.rooms!) {
@ -256,20 +255,21 @@ class DevicesCubit extends Cubit<DevicesState> {
code: control.code, code: control.code,
)); ));
try { try {
await DevicesAPI.controlDevice(control).then((value) { await DevicesAPI.controlDevice(control).then((response) {
emitSafe(DeviceControlSuccess( emitSafe(DeviceControlSuccess(
code: control.code, code: control.code,
)); ));
if (value['result'] ?? false) { if (response['success'] ?? false) {
Future.delayed(const Duration(milliseconds: 400), () { Future.delayed(const Duration(milliseconds: 400), () {
getDevicesStatues( getDevicesStatues(
deviceId, deviceId,
HomeCubit.getInstance().selectedSpace!.rooms!.indexOf( HomeCubit.getInstance().selectedSpace!.rooms!.indexOf(
HomeCubit.getInstance().selectedRoom!, HomeCubit.getInstance().selectedRoom!,
), ),
code: control.code, code: control.code);
);
}); });
} else {
emitSafe(DeviceControlError('Failed to control the device'));
} }
}); });
} on DioException catch (e) { } on DioException catch (e) {
@ -291,8 +291,9 @@ class DevicesCubit extends Cubit<DevicesState> {
} }
} }
fetchDevicesByRoomId(int roomId) async { fetchDevicesByRoomId(int? roomId) async {
if (_isClosed) return; if (_isClosed) return;
if (roomId == null) return;
try { try {
emitSafe(GetDevicesLoading()); emitSafe(GetDevicesLoading());

View File

@ -20,13 +20,13 @@ class CurtainView extends StatelessWidget {
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
statusBarIconBrightness: Brightness.light, statusBarIconBrightness: Brightness.light,
), ),
child: SafeArea( child: Scaffold(
child: Scaffold( backgroundColor: ColorsManager.backgroundColor,
backgroundColor: ColorsManager.backgroundColor, extendBodyBehindAppBar: true,
extendBodyBehindAppBar: true, extendBody: true,
extendBody: true, appBar: const CategoryViewAppBar(),
appBar: const CategoryViewAppBar(), body: SafeArea(
body: Container( child: Container(
width: MediaQuery.sizeOf(context).width, width: MediaQuery.sizeOf(context).width,
height: MediaQuery.sizeOf(context).height, height: MediaQuery.sizeOf(context).height,
decoration: const BoxDecoration( decoration: const BoxDecoration(

View File

@ -24,31 +24,29 @@ class LightInterface extends StatelessWidget {
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
statusBarIconBrightness: Brightness.light, statusBarIconBrightness: Brightness.light,
), ),
child: SafeArea( child: Scaffold(
child: Scaffold( backgroundColor: ColorsManager.backgroundColor,
backgroundColor: ColorsManager.backgroundColor, extendBodyBehindAppBar: true,
extendBodyBehindAppBar: true, extendBody: true,
extendBody: true, appBar: AppBar(
appBar: AppBar( backgroundColor: Colors.transparent,
backgroundColor: Colors.transparent, centerTitle: true,
centerTitle: true, title: BodyLarge(
title: BodyLarge( text: light.name ?? "",
text: light.name ?? "", fontColor: ColorsManager.primaryColor,
fontColor: ColorsManager.primaryColor, fontWeight: FontsManager.bold,
fontWeight: FontsManager.bold,
),
), ),
body: Padding( ),
padding: const EdgeInsets.only(top: 70, right: 20, left: 20), body: Padding(
child: SingleChildScrollView( padding: const EdgeInsets.only(top: 70, right: 20, left: 20),
child: Column( child: SingleChildScrollView(
crossAxisAlignment: CrossAxisAlignment.stretch, child: Column(
children: [ crossAxisAlignment: CrossAxisAlignment.stretch,
LightInterfaceSwitch(light: light), children: [
LightInterfaceContols(light: light), LightInterfaceSwitch(light: light),
const LightInterfaceTimer(), LightInterfaceContols(light: light),
], const LightInterfaceTimer(),
), ],
), ),
), ),
), ),

View File

@ -34,7 +34,7 @@ class WizartSwitches extends StatelessWidget {
itemBuilder: (_, index) { itemBuilder: (_, index) {
return InkWell( return InkWell(
onTap: () { onTap: () {
DevicesCubit.getInstance().selectCategory(index); // DevicesCubit.getInstance().selectCategory(index);
//Navigate to the chosen category view without animation //Navigate to the chosen category view without animation
Navigator.push(context, Navigator.push(context,