mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 01:56:19 +00:00
Update CustomSwitch widget to handle device status and control
Refactored CustomSwitch widget to properly handle device status and control based on the device's status values. Added necessary checks and updated UI accordingly.
This commit is contained in:
@ -2,80 +2,87 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
|
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
|
||||||
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
||||||
|
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
|
|
||||||
class CustomSwitch extends StatelessWidget {
|
class CustomSwitch extends StatelessWidget {
|
||||||
const CustomSwitch({super.key, this.category, this.device})
|
const CustomSwitch({super.key, required this.device});
|
||||||
: assert(category != null || device != null);
|
|
||||||
|
|
||||||
final DevicesCategoryModel? category;
|
|
||||||
|
|
||||||
final DeviceModel? device;
|
|
||||||
|
|
||||||
|
final DeviceModel device;
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocBuilder<DevicesCubit, DevicesState>(
|
return BlocBuilder<DevicesCubit, DevicesState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
bool? status;
|
bool? status;
|
||||||
if (device != null) {
|
if (device.status.isNotEmpty) {
|
||||||
status = device!.isOnline;
|
status = device.status
|
||||||
} else if (category != null) {
|
.firstWhereOrNull((status) => status.code == "switch")
|
||||||
status = category!.devicesStatus;
|
?.value;
|
||||||
}
|
}
|
||||||
return GestureDetector(
|
return status == null
|
||||||
onTap: () {
|
? const SizedBox()
|
||||||
if (device != null) {
|
: GestureDetector(
|
||||||
DevicesCubit.getInstance().turnOnOffDevice(device!);
|
onTap: () {
|
||||||
} else if (category != null) {
|
DevicesCubit.getInstance().deviceControl(
|
||||||
DevicesCubit.getInstance().changeCategorySwitchValue(category!);
|
DeviceControlModel(
|
||||||
}
|
deviceId: device.id,
|
||||||
},
|
code: device.status
|
||||||
child: Container(
|
.firstWhere((status) => status.code == "switch")
|
||||||
width: 45.0,
|
.code,
|
||||||
height: 28.0,
|
value: !device.status
|
||||||
decoration: BoxDecoration(
|
.firstWhere((status) => status.code == "switch")
|
||||||
borderRadius: BorderRadius.circular(24.0),
|
.value!,
|
||||||
color: status != null
|
),
|
||||||
? status
|
device.id!,
|
||||||
? ColorsManager.primaryColor
|
);
|
||||||
: const Color(0xFFD9D9D9)
|
},
|
||||||
: const Color(0xFFD9D9D9),
|
child: Container(
|
||||||
),
|
width: 45.0,
|
||||||
child: Center(
|
height: 28.0,
|
||||||
child: Container(
|
decoration: BoxDecoration(
|
||||||
width: 40.0,
|
borderRadius: BorderRadius.circular(24.0),
|
||||||
height: 23.0,
|
color: status != null
|
||||||
decoration: BoxDecoration(
|
|
||||||
borderRadius: BorderRadius.circular(24.0),
|
|
||||||
color: Colors.white,
|
|
||||||
),
|
|
||||||
child: Padding(
|
|
||||||
padding: const EdgeInsets.all(2.0),
|
|
||||||
child: Container(
|
|
||||||
alignment: status != null
|
|
||||||
? status
|
? status
|
||||||
? Alignment.centerRight
|
? ColorsManager.primaryColor
|
||||||
: Alignment.centerLeft
|
: const Color(0xFFD9D9D9)
|
||||||
: Alignment.centerLeft,
|
: const Color(0xFFD9D9D9),
|
||||||
|
),
|
||||||
|
child: Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
width: 20.0,
|
width: 40.0,
|
||||||
height: 20.0,
|
height: 23.0,
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
shape: BoxShape.circle,
|
borderRadius: BorderRadius.circular(24.0),
|
||||||
color: status != null
|
color: Colors.white,
|
||||||
? status
|
),
|
||||||
? ColorsManager.primaryColor
|
child: Padding(
|
||||||
: Colors.grey
|
padding: const EdgeInsets.all(2.0),
|
||||||
: Colors.grey,
|
child: Container(
|
||||||
|
alignment: status != null
|
||||||
|
? status
|
||||||
|
? Alignment.centerRight
|
||||||
|
: Alignment.centerLeft
|
||||||
|
: Alignment.centerLeft,
|
||||||
|
child: Container(
|
||||||
|
width: 20.0,
|
||||||
|
height: 20.0,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
shape: BoxShape.circle,
|
||||||
|
color: status != null
|
||||||
|
? status
|
||||||
|
? ColorsManager.primaryColor
|
||||||
|
: Colors.grey
|
||||||
|
: Colors.grey,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
);
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user