From 6230036c048ae477b4cd2a357f7df6aa87597652 Mon Sep 17 00:00:00 2001 From: anhtunz Date: Thu, 3 Apr 2025 10:44:29 +0700 Subject: [PATCH] Fix(bugs): Handle Touch PieChart --- .../devices/devices_manager_screen.dart | 1 + lib/feature/main/main_screen.dart | 1 - lib/product/network/network_manager.dart | 2 +- lib/product/shared/shared_pie_chart.dart | 167 +++++++++--------- 4 files changed, 89 insertions(+), 82 deletions(-) diff --git a/lib/feature/devices/devices_manager_screen.dart b/lib/feature/devices/devices_manager_screen.dart index 6640698..12844f7 100644 --- a/lib/feature/devices/devices_manager_screen.dart +++ b/lib/feature/devices/devices_manager_screen.dart @@ -371,6 +371,7 @@ class TagState extends StatelessWidget { child: const Icon( Icons.close, size: 20, + color: Colors.white, ), ), ], diff --git a/lib/feature/main/main_screen.dart b/lib/feature/main/main_screen.dart index a4cbc12..c979760 100644 --- a/lib/feature/main/main_screen.dart +++ b/lib/feature/main/main_screen.dart @@ -189,7 +189,6 @@ class _MainScreenState extends State with WidgetsBindingObserver { @override Widget build(BuildContext context) { - return StreamBuilder( stream: mainBloc.streamThemeMode, initialData: isLight, diff --git a/lib/product/network/network_manager.dart b/lib/product/network/network_manager.dart index 79046a0..ff0c335 100644 --- a/lib/product/network/network_manager.dart +++ b/lib/product/network/network_manager.dart @@ -1,7 +1,7 @@ import 'dart:convert'; import 'dart:developer'; -import 'package:sfm_app/product/constant/status_code/status_code_constants.dart'; +import '../constant/status_code/status_code_constants.dart'; import '../cache/local_manager.dart'; import '../constant/app/app_constants.dart'; diff --git a/lib/product/shared/shared_pie_chart.dart b/lib/product/shared/shared_pie_chart.dart index d6eddab..4396d6c 100644 --- a/lib/product/shared/shared_pie_chart.dart +++ b/lib/product/shared/shared_pie_chart.dart @@ -10,10 +10,7 @@ import '../services/language_services.dart'; import '../constant/app/app_constants.dart'; class SharedPieChart extends StatefulWidget { - const SharedPieChart( - {super.key, - required this.deviceByState, - required this.devicesManagerBloc}); + const SharedPieChart({super.key, required this.deviceByState, required this.devicesManagerBloc}); final Map> deviceByState; final DevicesManagerBloc devicesManagerBloc; @@ -23,7 +20,8 @@ class SharedPieChart extends StatefulWidget { } class _SharedPieChartState extends State { - int lastTouchedIndex = -1; + int lastTouchedIndex = 1000; + @override Widget build(BuildContext context) { TextStyle titleStyle = const TextStyle( @@ -31,17 +29,33 @@ class _SharedPieChartState extends State { fontSize: 20, fontWeight: FontWeight.bold, ); - int offlineCount = - widget.deviceByState[ApplicationConstants.OFFLINE_STATE]?.length ?? 0; - int normalCount = - widget.deviceByState[ApplicationConstants.NORMAL_STATE]?.length ?? 0; - int warningCount = - widget.deviceByState[ApplicationConstants.WARNING_STATE]?.length ?? 0; - int inProgressCount = - widget.deviceByState[ApplicationConstants.INPROGRESS_STATE]?.length ?? - 0; - int errorCount = - widget.deviceByState[ApplicationConstants.ERROR_STATE]?.length ?? 0; + List> states = [ + { + 'count': widget.deviceByState[ApplicationConstants.OFFLINE_STATE]?.length ?? 0, + 'color': Colors.grey + }, + { + 'count': widget.deviceByState[ApplicationConstants.NORMAL_STATE]?.length ?? 0, + 'color': Colors.green + }, + { + 'count': widget.deviceByState[ApplicationConstants.WARNING_STATE]?.length ?? 0, + 'color': Colors.red + }, + { + 'count': widget.deviceByState[ApplicationConstants.INPROGRESS_STATE]?.length ?? 0, + 'color': Colors.yellow + }, + { + 'count': widget.deviceByState[ApplicationConstants.ERROR_STATE]?.length ?? 0, + 'color': Colors.black + }, + ]; + int offlineCount = widget.deviceByState[ApplicationConstants.OFFLINE_STATE]?.length ?? 0; + int normalCount = widget.deviceByState[ApplicationConstants.NORMAL_STATE]?.length ?? 0; + int warningCount = widget.deviceByState[ApplicationConstants.WARNING_STATE]?.length ?? 0; + int inProgressCount = widget.deviceByState[ApplicationConstants.INPROGRESS_STATE]?.length ?? 0; + int errorCount = widget.deviceByState[ApplicationConstants.ERROR_STATE]?.length ?? 0; return AspectRatio( aspectRatio: 1.5, child: Row( @@ -54,55 +68,28 @@ class _SharedPieChartState extends State { aspectRatio: 1, child: PieChart( PieChartData( - // pieTouchData: PieTouchData( - // touchCallback: (FlTouchEvent event, pieTouchResponse) { - // if (!event.isInterestedForInteractions || - // pieTouchResponse == null || - // pieTouchResponse.touchedSection == null) { - // return; - // } - // int newTouchedIndex = - // pieTouchResponse.touchedSection!.touchedSectionIndex; - // updateDevicesOnTapPieChart(newTouchedIndex); - // }, - // ), - sections: [ - PieChartSectionData( - color: Colors.grey, - value: offlineCount.toDouble(), - title: offlineCount.toString(), - radius: context.dynamicWidth(0.2), - titleStyle: titleStyle, - ), - PieChartSectionData( - color: Colors.green, - value: normalCount.toDouble(), - title: normalCount.toString(), - radius: context.dynamicWidth(0.2), - titleStyle: titleStyle, - ), - PieChartSectionData( - color: Colors.red, - value: warningCount.toDouble(), - title: warningCount.toString(), - radius: context.dynamicWidth(0.2), - titleStyle: titleStyle, - ), - PieChartSectionData( - color: Colors.yellow, - value: inProgressCount.toDouble(), - title: inProgressCount.toString(), - radius: context.dynamicWidth(0.2), - titleStyle: titleStyle, - ), - PieChartSectionData( - color: Colors.black, - value: errorCount.toDouble(), - title: errorCount.toString(), - radius: context.dynamicWidth(0.2), - titleStyle: titleStyle, - ), - ], + pieTouchData: PieTouchData( + enabled: true, + touchCallback: (FlTouchEvent event, pieTouchResponse) { + if (!event.isInterestedForInteractions || + pieTouchResponse == null || + pieTouchResponse.touchedSection == null) { + return; + } + int newTouchedIndex = pieTouchResponse.touchedSection!.touchedSectionIndex; + updateDevicesOnTapPieChart(newTouchedIndex,states); + }, + ), + sections: states + .where((state) => state['count'] != 0) + .map((state) => PieChartSectionData( + color: state['color'], + value: (state['count'] as int).toDouble(), + title: state['count'].toString(), + radius: context.dynamicWidth(0.2), + titleStyle: titleStyle, + )) + .toList(), centerSpaceRadius: context.dynamicWidth(0.05), sectionsSpace: 2, ), @@ -181,22 +168,41 @@ class _SharedPieChartState extends State { ); } - void updateDevicesOnTapPieChart(int touchedIndex) { - if (touchedIndex != lastTouchedIndex) { - log("Update PieChart On Tap Function"); - lastTouchedIndex = touchedIndex; - if (touchedIndex == 0) { - widget.devicesManagerBloc.getDeviceByState(-1); - } else if (touchedIndex == 1) { - widget.devicesManagerBloc.getDeviceByState(0); - } else if (touchedIndex == 2) { - widget.devicesManagerBloc.getDeviceByState(1); - } else if (touchedIndex == 3) { - widget.devicesManagerBloc.getDeviceByState(2); - } else { - widget.devicesManagerBloc.getDeviceByState(-2); + void updateDevicesOnTapPieChart(int touchedIndex, List> states) { + final filteredStates = states.where((state) => state['count'] != 0).toList(); + + if (touchedIndex >= 0 && touchedIndex < filteredStates.length) { + final originalIndex = states.indexOf(filteredStates[touchedIndex]); + + if (originalIndex != lastTouchedIndex) { + lastTouchedIndex = originalIndex; + log("Update PieChart On Tap Function"); + log("Touched Index: $touchedIndex, Original Index: $originalIndex"); + + switch (originalIndex) { + case 0: // OFFLINE_STATE + log("Touched Index device state = -1"); + widget.devicesManagerBloc.getDeviceByState(-1); + break; + case 1: // NORMAL_STATE + log("Touched Index Get device state = 0"); + widget.devicesManagerBloc.getDeviceByState(0); + break; + case 2: // WARNING_STATE + log("Touched Index Get device state = 1"); + widget.devicesManagerBloc.getDeviceByState(1); + break; + case 3: // INPROGRESS_STATE + log("Touched Index Get device state = 2"); + widget.devicesManagerBloc.getDeviceByState(2); + break; + case 4: // ERROR_STATE + log("Touched Index Get device state = 3"); + widget.devicesManagerBloc.getDeviceByState(3); + break; + } } - } else {} + } } } @@ -209,6 +215,7 @@ class Indicator extends StatelessWidget { this.size = 16, this.textColor, }); + final Color color; final String text; final bool isSquare;