mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 17:47:28 +00:00
switch functionality initial implantation
This commit is contained in:
@ -1,8 +1,10 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
import 'package:flutter/material.dart';
|
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/model/ac_model.dart';
|
import 'package:syncrow_app/features/devices/model/ac_model.dart';
|
||||||
import 'package:syncrow_app/features/devices/model/curtain_model.dart';
|
import 'package:syncrow_app/features/devices/model/curtain_model.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/features/devices/model/light_model.dart';
|
import 'package:syncrow_app/features/devices/model/light_model.dart';
|
||||||
import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_view.dart';
|
import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_view.dart';
|
||||||
@ -12,6 +14,8 @@ import 'package:syncrow_app/features/devices/view/widgets/lights/lights_view.dar
|
|||||||
import 'package:syncrow_app/features/devices/view/widgets/lights_switches/light_switches.dart';
|
import 'package:syncrow_app/features/devices/view/widgets/lights_switches/light_switches.dart';
|
||||||
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_view.dart';
|
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_view.dart';
|
||||||
import 'package:syncrow_app/generated/assets.dart';
|
import 'package:syncrow_app/generated/assets.dart';
|
||||||
|
import 'package:syncrow_app/services/api/devices_api.dart';
|
||||||
|
import 'package:syncrow_app/services/api/network_exception.dart';
|
||||||
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
||||||
|
|
||||||
part 'devices_state.dart';
|
part 'devices_state.dart';
|
||||||
@ -395,4 +399,27 @@ class DevicesCubit extends Cubit<DevicesState> {
|
|||||||
|
|
||||||
emit(DevicesCategoryChanged());
|
emit(DevicesCategoryChanged());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//fetchRooms(SpaceModel space) async {
|
||||||
|
// emit(SpaceRoomsLoading());
|
||||||
|
// try {
|
||||||
|
// space.rooms = await SpacesAPI.getRooms(space.id!);
|
||||||
|
// if (space.rooms != null) {
|
||||||
|
// emit(SpaceRoomsLoaded(space.rooms!));
|
||||||
|
// } else {
|
||||||
|
// emit(SpaceRoomsError("No rooms found"));
|
||||||
|
// }
|
||||||
|
// } on DioException catch (e) {
|
||||||
|
// emit(SpacesError(ServerFailure.fromDioError(e).errMessage));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
deviceControl(DeviceControlModel control) async {
|
||||||
|
emit(DeviceControlLoading());
|
||||||
|
try {
|
||||||
|
var response = await DevicesAPI.controlDevice(control);
|
||||||
|
emit(DeviceControlSuccess());
|
||||||
|
} on DioException catch (e) {
|
||||||
|
emit(DeviceControlError(ServerFailure.fromDioError(e).errMessage));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,3 +20,13 @@ class CategorySwitchChanged extends DevicesState {}
|
|||||||
class DeviceSwitchChanged extends DevicesState {}
|
class DeviceSwitchChanged extends DevicesState {}
|
||||||
|
|
||||||
class DeviceSelected extends DevicesState {}
|
class DeviceSelected extends DevicesState {}
|
||||||
|
|
||||||
|
class DeviceControlLoading extends DevicesState {}
|
||||||
|
|
||||||
|
class DeviceControlSuccess extends DevicesState {}
|
||||||
|
|
||||||
|
class DeviceControlError extends DevicesState {
|
||||||
|
final String errorMsg;
|
||||||
|
|
||||||
|
DeviceControlError(this.errorMsg);
|
||||||
|
}
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:flutter_svg/svg.dart';
|
||||||
|
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
|
||||||
|
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||||
|
import 'package:syncrow_app/generated/assets.dart';
|
||||||
|
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||||
|
|
||||||
|
class LightSwitch extends StatelessWidget {
|
||||||
|
const LightSwitch({
|
||||||
|
super.key,
|
||||||
|
required this.control,
|
||||||
|
});
|
||||||
|
|
||||||
|
final DeviceControlModel control;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return BlocBuilder<DevicesCubit, DevicesState>(
|
||||||
|
builder: (context, state) {
|
||||||
|
return state is DeviceControlLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: InkWell(
|
||||||
|
overlayColor: MaterialStateProperty.all(Colors.transparent),
|
||||||
|
onTap: () {
|
||||||
|
DevicesCubit.get(context)
|
||||||
|
.deviceControl(control)
|
||||||
|
.then((value) {
|
||||||
|
if (control.value ?? true) {
|
||||||
|
control.value = false;
|
||||||
|
} else {
|
||||||
|
control.value = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
child: Stack(
|
||||||
|
alignment: !control.value!
|
||||||
|
? Alignment.topCenter
|
||||||
|
: Alignment.bottomCenter,
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
borderRadius:
|
||||||
|
const BorderRadius.all(Radius.circular(100.0)),
|
||||||
|
color: !control.value!
|
||||||
|
? ColorsManager.primaryColorWithOpacity
|
||||||
|
: ColorsManager.switchOffColor,
|
||||||
|
),
|
||||||
|
width: 60,
|
||||||
|
height: 115,
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(5.0),
|
||||||
|
child: SizedBox.square(
|
||||||
|
dimension: 60,
|
||||||
|
child: SvgPicture.asset(
|
||||||
|
!control.value!
|
||||||
|
? Assets.iconsLightSwitchOn
|
||||||
|
: Assets.iconsLightSwitchOff,
|
||||||
|
fit: BoxFit.fill,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,54 +0,0 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_svg/svg.dart';
|
|
||||||
import 'package:syncrow_app/generated/assets.dart';
|
|
||||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
|
||||||
|
|
||||||
class LightSwitch extends StatefulWidget {
|
|
||||||
const LightSwitch({
|
|
||||||
super.key,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<LightSwitch> createState() => _LightSwitchState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _LightSwitchState extends State<LightSwitch> {
|
|
||||||
bool isOn = false;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return InkWell(
|
|
||||||
overlayColor: MaterialStateProperty.all(Colors.transparent),
|
|
||||||
onTap: () {
|
|
||||||
setState(() {
|
|
||||||
isOn = !isOn;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
child: Stack(
|
|
||||||
alignment: isOn ? Alignment.topCenter : Alignment.bottomCenter,
|
|
||||||
children: [
|
|
||||||
Container(
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
borderRadius: const BorderRadius.all(Radius.circular(100.0)),
|
|
||||||
color: isOn
|
|
||||||
? ColorsManager.primaryColorWithOpacity
|
|
||||||
: ColorsManager.switchOffColor,
|
|
||||||
),
|
|
||||||
width: 60,
|
|
||||||
height: 115,
|
|
||||||
),
|
|
||||||
Padding(
|
|
||||||
padding: const EdgeInsets.all(5.0),
|
|
||||||
child: SizedBox.square(
|
|
||||||
dimension: 60,
|
|
||||||
child: SvgPicture.asset(
|
|
||||||
isOn ? Assets.iconsLightSwitchOn : Assets.iconsLightSwitchOff,
|
|
||||||
fit: BoxFit.fill,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.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/features/devices/view/widgets/lights_switches/light_switche.dart';
|
import 'package:syncrow_app/features/devices/view/widgets/lights_switches/light_switch.dart';
|
||||||
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
|
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
|
||||||
import 'package:syncrow_app/utils/context_extension.dart';
|
import 'package:syncrow_app/utils/context_extension.dart';
|
||||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||||
@ -20,13 +21,31 @@ class LightSwitchesBody extends StatelessWidget {
|
|||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
children: [
|
children: [
|
||||||
const Expanded(child: SizedBox.shrink()),
|
const Expanded(child: SizedBox.shrink()),
|
||||||
const Row(
|
Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
LightSwitch(),
|
LightSwitch(
|
||||||
LightSwitch(),
|
control: DeviceControlModel(
|
||||||
LightSwitch(),
|
deviceId: 'bfe10693d4fd263206ocq9',
|
||||||
|
code: 'switch_1',
|
||||||
|
value: true,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
LightSwitch(
|
||||||
|
control: DeviceControlModel(
|
||||||
|
deviceId: 'bfe10693d4fd263206ocq9',
|
||||||
|
code: 'switch_2',
|
||||||
|
value: true,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
LightSwitch(
|
||||||
|
control: DeviceControlModel(
|
||||||
|
deviceId: 'bfe10693d4fd263206ocq9',
|
||||||
|
code: 'switch_3',
|
||||||
|
value: true,
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
|
@ -13,4 +13,7 @@ abstract class ApiEndpoints {
|
|||||||
// Spaces
|
// Spaces
|
||||||
static const String spaces = '$baseUrl/home';
|
static const String spaces = '$baseUrl/home';
|
||||||
static const String rooms = '$baseUrl/room';
|
static const String rooms = '$baseUrl/room';
|
||||||
|
|
||||||
|
// Devices
|
||||||
|
static const String control = '$baseUrl/device/control';
|
||||||
}
|
}
|
||||||
|
18
lib/services/api/devices_api.dart
Normal file
18
lib/services/api/devices_api.dart
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||||
|
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
||||||
|
import 'package:syncrow_app/services/api/http_service.dart';
|
||||||
|
|
||||||
|
class DevicesAPI {
|
||||||
|
static Future<Map<String, dynamic>> controlDevice(
|
||||||
|
DeviceControlModel controlModel) async {
|
||||||
|
final response = await HTTPService().post(
|
||||||
|
path: ApiEndpoints.control,
|
||||||
|
body: controlModel.toJson(),
|
||||||
|
showServerMessage: false,
|
||||||
|
expectedResponseModel: (json) {
|
||||||
|
return json;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user