mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Refactor TotalEnergyConsumptionChart
to accept chartData as a parameter that it takes from TotalEnergyConsumptionBloc
and update TotalEnergyConsumptionChartBox
to use Bloc for state management.
This commit is contained in:
@ -6,21 +6,6 @@ import 'package:syncrow_web/pages/analytics/models/energy_data_model.dart';
|
|||||||
import 'package:syncrow_web/utils/color_manager.dart';
|
import 'package:syncrow_web/utils/color_manager.dart';
|
||||||
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
||||||
|
|
||||||
final _chartData = [
|
|
||||||
EnergyDataModel(date: DateTime(2025, 1), value: 18000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 2), value: 25000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 3), value: 22000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 4), value: 21000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 5), value: 30000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 6), value: 23000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 7), value: 21000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 8), value: 25000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 9), value: 21100),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 10), value: 22000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 11), value: 21000),
|
|
||||||
EnergyDataModel(date: DateTime(2025, 12), value: 27500),
|
|
||||||
];
|
|
||||||
|
|
||||||
// energy_consumption_chart will return id, name and consumption
|
// energy_consumption_chart will return id, name and consumption
|
||||||
const phasesJson = {
|
const phasesJson = {
|
||||||
"1": {
|
"1": {
|
||||||
@ -31,7 +16,9 @@ const phasesJson = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class TotalEnergyConsumptionChart extends StatelessWidget {
|
class TotalEnergyConsumptionChart extends StatelessWidget {
|
||||||
const TotalEnergyConsumptionChart({super.key});
|
const TotalEnergyConsumptionChart({required this.chartData, super.key});
|
||||||
|
|
||||||
|
final List<EnergyDataModel> chartData;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -73,7 +60,7 @@ class TotalEnergyConsumptionChart extends StatelessWidget {
|
|||||||
preventCurveOvershootingThreshold: 0.1,
|
preventCurveOvershootingThreshold: 0.1,
|
||||||
curveSmoothness: 0.55,
|
curveSmoothness: 0.55,
|
||||||
preventCurveOverShooting: true,
|
preventCurveOverShooting: true,
|
||||||
spots: _chartData
|
spots: chartData
|
||||||
.asMap()
|
.asMap()
|
||||||
.entries
|
.entries
|
||||||
.map(
|
.map(
|
||||||
@ -122,7 +109,8 @@ class TotalEnergyConsumptionChart extends StatelessWidget {
|
|||||||
child: FittedBox(
|
child: FittedBox(
|
||||||
fit: BoxFit.scaleDown,
|
fit: BoxFit.scaleDown,
|
||||||
child: Text(
|
child: Text(
|
||||||
_chartData[value.toInt()].date.month.getMonthName,
|
chartData.elementAtOrNull(value.toInt())?.date.month.getMonthName ??
|
||||||
|
'',
|
||||||
style: context.textTheme.bodySmall?.copyWith(
|
style: context.textTheme.bodySmall?.copyWith(
|
||||||
fontSize: 12,
|
fontSize: 12,
|
||||||
color: ColorsManager.greyColor,
|
color: ColorsManager.greyColor,
|
||||||
|
@ -1,30 +1,55 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:syncrow_web/pages/analytics/modules/energy_management/blocs/total_energy_consumption/total_energy_consumption_bloc.dart';
|
||||||
import 'package:syncrow_web/pages/analytics/modules/energy_management/widgets/total_energy_consumption_chart.dart';
|
import 'package:syncrow_web/pages/analytics/modules/energy_management/widgets/total_energy_consumption_chart.dart';
|
||||||
|
import 'package:syncrow_web/pages/analytics/params/get_total_energy_consumption_param.dart';
|
||||||
import 'package:syncrow_web/utils/style.dart';
|
import 'package:syncrow_web/utils/style.dart';
|
||||||
|
|
||||||
class TotalEnergyConsumptionChartBox extends StatelessWidget {
|
class TotalEnergyConsumptionChartBox extends StatefulWidget {
|
||||||
const TotalEnergyConsumptionChartBox({super.key});
|
const TotalEnergyConsumptionChartBox({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<TotalEnergyConsumptionChartBox> createState() =>
|
||||||
|
_TotalEnergyConsumptionChartBoxState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _TotalEnergyConsumptionChartBoxState
|
||||||
|
extends State<TotalEnergyConsumptionChartBox> {
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
final param = GetTotalEnergyConsumptionParam(
|
||||||
|
startDate: DateTime.now().subtract(const Duration(days: 30)),
|
||||||
|
endDate: DateTime.now(),
|
||||||
|
spaceId: '123',
|
||||||
|
);
|
||||||
|
context.read<TotalEnergyConsumptionBloc>().add(
|
||||||
|
TotalEnergyConsumptionLoadEvent(param: param),
|
||||||
|
);
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Container(
|
return BlocBuilder<TotalEnergyConsumptionBloc, TotalEnergyConsumptionState>(
|
||||||
decoration: subSectionContainerDecoration.copyWith(
|
builder: (context, state) => Container(
|
||||||
borderRadius: BorderRadius.circular(30),
|
decoration: subSectionContainerDecoration.copyWith(
|
||||||
),
|
borderRadius: BorderRadius.circular(30),
|
||||||
padding: const EdgeInsets.all(30),
|
),
|
||||||
child: Column(
|
padding: const EdgeInsets.all(30),
|
||||||
spacing: 20,
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
spacing: 20,
|
||||||
children: [
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
Text(
|
children: [
|
||||||
'Total Energy Consumption',
|
Text(
|
||||||
style: TextStyle(
|
'Total Energy Consumption',
|
||||||
fontSize: 22,
|
style: TextStyle(
|
||||||
fontWeight: FontWeight.w700,
|
fontSize: 22,
|
||||||
|
fontWeight: FontWeight.w700,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
TotalEnergyConsumptionChart(chartData: state.chartData),
|
||||||
const TotalEnergyConsumptionChart(),
|
],
|
||||||
],
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
class GetTotalEnergyConsumptionParam {
|
class GetTotalEnergyConsumptionParam {
|
||||||
final String? startDate;
|
final DateTime? startDate;
|
||||||
final String? endDate;
|
final DateTime? endDate;
|
||||||
final String? spaceId;
|
final String? spaceId;
|
||||||
|
|
||||||
GetTotalEnergyConsumptionParam({
|
GetTotalEnergyConsumptionParam({
|
||||||
@ -11,8 +11,8 @@ class GetTotalEnergyConsumptionParam {
|
|||||||
|
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {
|
return {
|
||||||
'startDate': startDate,
|
'startDate': startDate?.toIso8601String(),
|
||||||
'endDate': endDate,
|
'endDate': endDate?.toIso8601String(),
|
||||||
'spaceId': spaceId,
|
'spaceId': spaceId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user