From 104db4914b218359aa639e60c153a7d4468725c6 Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Mon, 3 Jun 2024 23:09:06 +0200
Subject: [PATCH] update: working changing bar data

---
 app/lib/widgets/bar_data.dart    | 63 ++++++++++++++++++--------------
 app/lib/widgets/main_layout.dart | 11 +++---
 app/lib/widgets/stat_charts.dart |  2 +-
 3 files changed, 42 insertions(+), 34 deletions(-)

diff --git a/app/lib/widgets/bar_data.dart b/app/lib/widgets/bar_data.dart
index 5e3e981e..43ab9710 100644
--- a/app/lib/widgets/bar_data.dart
+++ b/app/lib/widgets/bar_data.dart
@@ -24,37 +24,43 @@ class _BarDataState extends State<BarData> {
   @override
   void initState() {
     super.initState();
+  }
 
-    // Allocate bar data dynamically based from the selected subdivision
-    for (int i = 0; i < 7; i++) {
-      var entry = selectedSubDiv?.iceStats[i];
-      if (entry != null) {
-        // NB test data for demo
-        Random random = Random();
-        double blackIce = 2.0 + random.nextDouble() * (4.0 - 2.0);
-        double slushIce = 1.0 + random.nextDouble() * (1.5 - 1.0);
-        double snowDepth = random.nextDouble() * 1.3;
-
-        barData[i] = [
-          //entry.blackIce,
-          //entry.slushIce,
-          //entry.snowDepth,
-          blackIce,  // NB test data for demo
-          slushIce,  // ...
-          snowDepth, // ..
-        ];
-
-        // NB test data for demo
-        totalHeight = blackIce + slushIce + snowDepth + 0.3; // NB demo data
-
-        // Find tallest layer
-        if (entry.totalIce > totalHeight) {
-          totalHeight = entry.totalIce;
+  /// Extracts the ice statistics from the currently selected subdivision
+  void setBarChartData() {
+    print("Updating bar chart data!!");
+    setState(() {
+      // Allocate bar data dynamically based from the selected subdivision
+      for (int i = 0; i < 7; i++) {
+        var entry = selectedSubDiv?.iceStats[i];
+        if (entry != null) {
+          // NB test data for demo
+          Random random = Random();
+          double blackIce = 2.0 + random.nextDouble() * (4.0 - 2.0);
+          double slushIce = 1.0 + random.nextDouble() * (1.5 - 1.0);
+          double snowDepth = random.nextDouble() * 1.3;
+
+          barData[i] = [
+            //entry.blackIce,
+            //entry.slushIce,
+            //entry.snowDepth,
+            blackIce,  // NB test data for demo
+            slushIce,  // ...
+            snowDepth, // ..
+          ];
+
+          // NB test data for demo
+          totalHeight = blackIce + slushIce + snowDepth + 0.3; // NB demo data
+
+          // Find tallest layer
+          if (entry.totalIce > totalHeight) {
+            totalHeight = entry.totalIce;
+          }
+        } else {
+          barData[i] = [0.0, 0.0, 0.0];
         }
-      } else {
-        barData[i] = [0.0, 0.0, 0.0];
       }
-    }
+    });
   }
 
   BarChartGroupData generateGroup(
@@ -125,6 +131,7 @@ class _BarDataState extends State<BarData> {
 
   @override
   Widget build(BuildContext context) {
+    setBarChartData();
       return Column(
           children: [
             SizedBox(
diff --git a/app/lib/widgets/main_layout.dart b/app/lib/widgets/main_layout.dart
index 17a1c82b..2f2f3d02 100644
--- a/app/lib/widgets/main_layout.dart
+++ b/app/lib/widgets/main_layout.dart
@@ -5,6 +5,7 @@ import 'package:share_plus/share_plus.dart';
 import 'package:google_fonts/google_fonts.dart';
 import 'package:shared_preferences/shared_preferences.dart';
 
+import 'bar_data.dart';
 import 'osm_layer.dart';
 import 'stat_charts.dart';
 import 'info_layer.dart';
@@ -63,17 +64,17 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
   /// Tile selection handler
   void handleSelection(int index) {
     String indexString = index.toString();
-    setState(() {
       for (Measurement measurement in widget.measurements) {
         for (SubDiv subdivision in measurement.subDivs) {
           if (subdivision.sub_div_id == indexString) {
-            selectedSubDiv = subdivision;
-            selectedMeasurement = measurement;
+            setState(() {
+              selectedSubDiv = subdivision;
+              selectedMeasurement = measurement;
+            });
             break;
           }
         }
       }
-    });
   }
 
   @override
@@ -378,7 +379,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
                 const SizedBox(height: contPadding*2.5),
                 SizedBox(
                   width: screenWidth * boxWidth * 1.2,
-                  child: const StatCharts(),
+                  child: StatCharts(),
                 ),
                 const SizedBox(height: contPadding*4),
               ],
diff --git a/app/lib/widgets/stat_charts.dart b/app/lib/widgets/stat_charts.dart
index a34af6ab..f83418ad 100644
--- a/app/lib/widgets/stat_charts.dart
+++ b/app/lib/widgets/stat_charts.dart
@@ -79,7 +79,7 @@ class StatCharts extends StatelessWidget {
         ),
         SizedBox(
           width: MediaQuery.of(context).size.width-30,
-          child: const BarData(),
+          child: BarData(),
         ),
         /*
         const SizedBox(height: 60),
-- 
GitLab