build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  // Crear una copia de la lista monitoreos y ordenarla por fecha de la más reciente a la más antigua
  List<Map<String, dynamic>> sortedMonitoreos =
      List<Map<String, dynamic>>.from(monitoreos);
  sortedMonitoreos
      .sort((a, b) => b['fecha_monitoreo'].compareTo(a['fecha_monitoreo']));

  // Obtener las fechas y temperaturas de los monitoreos
  List fechas = sortedMonitoreos
      .map((monitoreo) => monitoreo['fecha_monitoreo'])
      .toList();
  List<double> temperaturas = sortedMonitoreos.map((monitoreo) {
    var temp = monitoreo['temperatura'];
    if (temp is String) {
      return double.parse(temp);
    } else if (temp is double) {
      return temp;
    } else {
      throw TypeError(); // O maneja este error de una manera más apropiada
    }
  }).toList();

  // Calcular la media de las temperaturas
  double mediaTemperaturas = temperaturas.isNotEmpty
      ? temperaturas.reduce((a, b) => a + b) / temperaturas.length
      : 0.0;

  return Column(
    children: [
      const Text(
        'Estadísticas de Temperatura',
        style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
      ),
      Container(
        height: 300,
        padding: const EdgeInsets.all(16),
        child: LineChart(
          LineChartData(
            gridData: FlGridData(
              show: true,
              drawVerticalLine: true,
              getDrawingHorizontalLine: (value) {
                return const FlLine(
                  color: Color(0xff37434d),
                  strokeWidth: 1,
                );
              },
              getDrawingVerticalLine: (value) {
                return const FlLine(
                  color: Color(0xff37434d),
                  strokeWidth: 1,
                );
              },
            ),
            titlesData: FlTitlesData(
              show: true,
              bottomTitles: AxisTitles(
                sideTitles: SideTitles(
                  showTitles: true,
                  getTitlesWidget: (value, meta) {
                    int index = value.toInt();
                    if (index >= 0 && index < fechas.length) {
                      return Text(
                        fechas[index].substring(0, 10),
                        style: const TextStyle(
                          color: Color(0xff7589a2),
                          fontWeight: FontWeight.bold,
                          fontSize: 13,
                        ),
                      );
                    }
                    return Container();
                  },
                  reservedSize: 30,
                ),
              ),
              leftTitles: AxisTitles(
                sideTitles: SideTitles(
                  showTitles: true,
                  getTitlesWidget: (value, meta) {
                    if (value % 5 == 0) {
                      return Text(
                        '${value.toInt()}°C',
                        style: const TextStyle(
                          color: Color(0xff7589a2),
                          fontWeight: FontWeight.bold,
                          fontSize: 13,
                        ),
                      );
                    }
                    return Container();
                  },
                  reservedSize: 32,
                  interval: 5,
                ),
              ),
              rightTitles: const AxisTitles(
                sideTitles: SideTitles(showTitles: false),
              ),
              topTitles: const AxisTitles(
                sideTitles: SideTitles(showTitles: false),
              ),
            ),
            borderData: FlBorderData(
              show: true,
              border: Border.all(color: const Color(0xff37434d), width: 1),
            ),
            minX: 0,
            maxX: temperaturas.length.toDouble() - 1,
            minY: 0,
            maxY: 40,
            lineBarsData: [
              LineChartBarData(
                spots: List.generate(
                  temperaturas.length,
                  (index) => FlSpot(index.toDouble(), temperaturas[index]),
                ),
                isCurved: true,
                gradient: const LinearGradient(
                  colors: [
                    Colors.blueAccent,
                    Colors.lightBlueAccent,
                    Colors.greenAccent,
                    Colors.yellowAccent,
                  ],
                ),
                barWidth: 4,
                belowBarData: BarAreaData(
                  show: true,
                  gradient: LinearGradient(
                    colors: [
                      Colors.blueAccent.withOpacity(0.3),
                      Colors.lightBlueAccent.withOpacity(0.3),
                      Colors.greenAccent.withOpacity(0.3),
                      Colors.yellowAccent.withOpacity(0.3),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
      const SizedBox(height: 50),
      Text(
        'Media de Temperaturas: ${mediaTemperaturas.toStringAsFixed(1)}°C',
        style: const TextStyle(fontSize: 16),
      ),
    ],
  );
}