mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 01:56:19 +00:00
implemented light brightness slider
optimised state emit to only emit when change is detected
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/devices/model/light_model.dart';
|
||||
|
||||
part 'lights_state.dart';
|
||||
|
||||
@ -6,4 +7,26 @@ class LightsCubit extends Cubit<LightsState> {
|
||||
LightsCubit() : super(LightsInitial());
|
||||
|
||||
static LightsCubit get(context) => BlocProvider.of(context);
|
||||
|
||||
int getBrightness(LightModel light) {
|
||||
return light.brightness.toInt();
|
||||
}
|
||||
|
||||
setBrightness(LightModel light, double value) {
|
||||
value = (value / 5).ceil() * 5;
|
||||
if (value != light.brightness) {
|
||||
light.brightness = value;
|
||||
emit(LightBrightnessChanged(value));
|
||||
}
|
||||
}
|
||||
|
||||
onHorizontalDragUpdate(LightModel light, double dx, double screenWidth) {
|
||||
double newBrightness = (dx / (screenWidth - 15) * 100);
|
||||
if (newBrightness > 100) {
|
||||
newBrightness = 100;
|
||||
} else if (newBrightness < 0) {
|
||||
newBrightness = 0;
|
||||
}
|
||||
setBrightness(light, newBrightness);
|
||||
}
|
||||
}
|
||||
|
@ -3,3 +3,19 @@ part of 'lights_cubit.dart';
|
||||
abstract class LightsState {}
|
||||
|
||||
class LightsInitial extends LightsState {}
|
||||
|
||||
class LightsLoading extends LightsState {}
|
||||
|
||||
class LightsSuccess extends LightsState {}
|
||||
|
||||
class LightsFailure extends LightsState {
|
||||
final String message;
|
||||
|
||||
LightsFailure(this.message);
|
||||
}
|
||||
|
||||
class LightBrightnessChanged extends LightsState {
|
||||
final double brightness;
|
||||
|
||||
LightBrightnessChanged(this.brightness);
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||
|
||||
class LightModel extends DeviceModel {
|
||||
final double brightness;
|
||||
final int color;
|
||||
late double brightness;
|
||||
late int color;
|
||||
|
||||
final int lightingMode;
|
||||
late int lightingMode;
|
||||
|
||||
LightModel({
|
||||
required this.brightness,
|
||||
|
@ -0,0 +1,66 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/lights/lights_cubit.dart';
|
||||
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
|
||||
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
|
||||
import 'package:syncrow_app/utils/context_extension.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||
|
||||
import '../../../model/light_model.dart';
|
||||
|
||||
class LightBrightness extends StatelessWidget {
|
||||
const LightBrightness({
|
||||
super.key,
|
||||
required this.light,
|
||||
});
|
||||
|
||||
final LightModel light;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<LightsCubit, LightsState>(
|
||||
builder: (context, state) {
|
||||
return GestureDetector(
|
||||
onHorizontalDragUpdate: (details) => LightsCubit.get(context)
|
||||
.onHorizontalDragUpdate(light, details.localPosition.dx,
|
||||
MediaQuery.of(context).size.width - 15),
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
const DefaultContainer(
|
||||
height: 60,
|
||||
child: SizedBox.expand(),
|
||||
),
|
||||
AnimatedPositioned(
|
||||
left: 0,
|
||||
duration: const Duration(milliseconds: 50),
|
||||
child: Container(
|
||||
height: 60,
|
||||
width: (MediaQuery.of(context).size.width - 30) *
|
||||
light.brightness /
|
||||
100,
|
||||
decoration: BoxDecoration(
|
||||
color: ColorsManager.primaryColor.withOpacity(0.6),
|
||||
borderRadius: light.brightness != 100
|
||||
? const BorderRadius.only(
|
||||
topLeft: Radius.circular(20),
|
||||
bottomLeft: Radius.circular(20),
|
||||
)
|
||||
: BorderRadius.circular(20),
|
||||
),
|
||||
),
|
||||
),
|
||||
BodyLarge(
|
||||
text: "${light.brightness}%",
|
||||
style: context.bodyLarge.copyWith(
|
||||
color: Colors.black,
|
||||
fontSize: 23,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:syncrow_app/features/devices/view/widgets/lights/light_brightness.dart';
|
||||
|
||||
import '../../../../shared_widgets/devices_default_switch.dart';
|
||||
import '../../../../shared_widgets/text_widgets/body_small.dart';
|
||||
@ -47,6 +48,8 @@ class LightsList extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 5),
|
||||
DevicesDefaultSwitch(model: lights[index]),
|
||||
const SizedBox(height: 10),
|
||||
LightBrightness(light: lights[index]),
|
||||
],
|
||||
);
|
||||
},
|
||||
|
@ -53,7 +53,7 @@ class LightsView extends StatelessWidget {
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 60, right: 15, left: 15, bottom: 80),
|
||||
top: 70, right: 15, left: 15, bottom: 80),
|
||||
child: SizedBox.expand(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
|
@ -6,19 +6,22 @@ class DefaultContainer extends StatelessWidget {
|
||||
required this.child,
|
||||
this.height,
|
||||
this.width,
|
||||
this.color,
|
||||
});
|
||||
|
||||
final double? height;
|
||||
final double? width;
|
||||
final Widget child;
|
||||
|
||||
final Color? color;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
height: height,
|
||||
width: width,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
color: color ?? Colors.white,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
padding: const EdgeInsets.all(10),
|
||||
|
Reference in New Issue
Block a user