|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:provider/provider.dart'; |
| 3 | +import 'package:pslab/constants.dart'; |
| 4 | +import 'package:pslab/providers/luxmeter_state_provider.dart'; |
| 5 | +import 'package:pslab/view/widgets/common_scaffold_widget.dart'; |
| 6 | +import 'package:pslab/view/widgets/luxmeter_card.dart'; |
| 7 | +import 'package:fl_chart/fl_chart.dart'; |
| 8 | + |
| 9 | +class LuxMeterScreen extends StatefulWidget { |
| 10 | + const LuxMeterScreen({super.key}); |
| 11 | + @override |
| 12 | + State<StatefulWidget> createState() => _LuxMeterScreenState(); |
| 13 | +} |
| 14 | + |
| 15 | +class _LuxMeterScreenState extends State<LuxMeterScreen> { |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return MultiProvider( |
| 19 | + providers: [ |
| 20 | + ChangeNotifierProvider<LuxMeterStateProvider>( |
| 21 | + create: (_) => LuxMeterStateProvider()..initializeSensors(), |
| 22 | + ), |
| 23 | + ], |
| 24 | + child: CommonScaffold( |
| 25 | + title: luxMeterTitle, |
| 26 | + body: SafeArea( |
| 27 | + child: Column( |
| 28 | + children: [ |
| 29 | + const Expanded( |
| 30 | + flex: 45, |
| 31 | + child: LuxMeterCard(), |
| 32 | + ), |
| 33 | + Expanded( |
| 34 | + flex: 55, |
| 35 | + child: _buildChartSection(), |
| 36 | + ), |
| 37 | + ], |
| 38 | + ), |
| 39 | + ), |
| 40 | + ), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + Widget _buildChartSection() { |
| 45 | + return Consumer<LuxMeterStateProvider>( |
| 46 | + builder: (context, provider, child) { |
| 47 | + final screenWidth = MediaQuery.of(context).size.width; |
| 48 | + final cardMargin = screenWidth < 400 ? 8.0 : 16.0; |
| 49 | + List<FlSpot> spots = provider.getLuxChartData(); |
| 50 | + double maxLux = provider.getMaxLux(); |
| 51 | + double maxTime = provider.getMaxTime(); |
| 52 | + double minTime = provider.getMinTime(); |
| 53 | + double timeInterval = provider.getTimeInterval(); |
| 54 | + |
| 55 | + return Container( |
| 56 | + margin: EdgeInsets.fromLTRB(cardMargin, 0, cardMargin, cardMargin), |
| 57 | + padding: EdgeInsets.all(cardMargin), |
| 58 | + decoration: BoxDecoration( |
| 59 | + color: Colors.black, |
| 60 | + borderRadius: BorderRadius.circular(8), |
| 61 | + ), |
| 62 | + child: _buildChart( |
| 63 | + screenWidth, maxLux, maxTime, minTime, timeInterval, spots), |
| 64 | + ); |
| 65 | + }, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + Widget sideTitleWidgets(double value, TitleMeta meta) { |
| 70 | + final screenWidth = MediaQuery.of(context).size.width; |
| 71 | + final fontSize = screenWidth < 400 |
| 72 | + ? 7.0 |
| 73 | + : screenWidth < 600 |
| 74 | + ? 8.0 |
| 75 | + : 9.0; |
| 76 | + final style = TextStyle( |
| 77 | + color: Colors.white, |
| 78 | + fontSize: fontSize, |
| 79 | + ); |
| 80 | + String timeText; |
| 81 | + if (value < 60) { |
| 82 | + timeText = '${value.toInt()}s'; |
| 83 | + } else if (value < 3600) { |
| 84 | + int minutes = (value / 60).floor(); |
| 85 | + int seconds = (value % 60).toInt(); |
| 86 | + timeText = '${minutes}m${seconds}s'; |
| 87 | + } else { |
| 88 | + int hours = (value / 3600).floor(); |
| 89 | + int minutes = ((value % 3600) / 60).floor(); |
| 90 | + timeText = '${hours}h${minutes}m'; |
| 91 | + } |
| 92 | + return SideTitleWidget( |
| 93 | + meta: meta, |
| 94 | + child: Text( |
| 95 | + maxLines: 1, |
| 96 | + timeText, |
| 97 | + style: style, |
| 98 | + ), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + Widget _buildChart(double screenWidth, double maxLux, double maxTime, |
| 103 | + double minTime, double timeInterval, List<FlSpot> spots) { |
| 104 | + final chartFontSize = screenWidth < 400 |
| 105 | + ? 8.0 |
| 106 | + : screenWidth < 600 |
| 107 | + ? 9.0 |
| 108 | + : 10.0; |
| 109 | + final axisNameFontSize = screenWidth < 400 ? 9.0 : 10.0; |
| 110 | + final reservedSizeBottom = screenWidth < 400 ? 25.0 : 30.0; |
| 111 | + final reservedSizeLeft = screenWidth < 400 ? 20.0 : 25.0; |
| 112 | + return LineChart( |
| 113 | + LineChartData( |
| 114 | + backgroundColor: Colors.black, |
| 115 | + titlesData: FlTitlesData( |
| 116 | + show: true, |
| 117 | + topTitles: AxisTitles( |
| 118 | + axisNameWidget: Padding( |
| 119 | + padding: EdgeInsets.only(left: screenWidth < 400 ? 15 : 25), |
| 120 | + child: Text( |
| 121 | + timeAxisLabel, |
| 122 | + style: TextStyle( |
| 123 | + fontSize: axisNameFontSize, |
| 124 | + color: Colors.white, |
| 125 | + fontWeight: FontWeight.bold, |
| 126 | + ), |
| 127 | + ), |
| 128 | + ), |
| 129 | + axisNameSize: screenWidth < 400 ? 18 : 20, |
| 130 | + ), |
| 131 | + bottomTitles: AxisTitles( |
| 132 | + sideTitles: SideTitles( |
| 133 | + showTitles: true, |
| 134 | + reservedSize: reservedSizeBottom, |
| 135 | + getTitlesWidget: sideTitleWidgets, |
| 136 | + interval: timeInterval, |
| 137 | + ), |
| 138 | + ), |
| 139 | + leftTitles: AxisTitles( |
| 140 | + axisNameWidget: Text( |
| 141 | + lx, |
| 142 | + style: TextStyle( |
| 143 | + fontSize: axisNameFontSize, |
| 144 | + color: Colors.white, |
| 145 | + fontWeight: FontWeight.bold, |
| 146 | + ), |
| 147 | + ), |
| 148 | + sideTitles: SideTitles( |
| 149 | + reservedSize: reservedSizeLeft, |
| 150 | + showTitles: true, |
| 151 | + getTitlesWidget: (value, meta) { |
| 152 | + return SideTitleWidget( |
| 153 | + meta: meta, |
| 154 | + child: Text( |
| 155 | + value.toInt().toString(), |
| 156 | + style: TextStyle( |
| 157 | + color: Colors.white, |
| 158 | + fontSize: chartFontSize, |
| 159 | + ), |
| 160 | + ), |
| 161 | + ); |
| 162 | + }, |
| 163 | + interval: maxLux > 0 ? (maxLux / 5).ceilToDouble() : 10, |
| 164 | + ), |
| 165 | + ), |
| 166 | + rightTitles: const AxisTitles( |
| 167 | + sideTitles: SideTitles(showTitles: false), |
| 168 | + ), |
| 169 | + ), |
| 170 | + gridData: FlGridData( |
| 171 | + show: true, |
| 172 | + drawHorizontalLine: true, |
| 173 | + drawVerticalLine: true, |
| 174 | + horizontalInterval: maxLux > 0 ? (maxLux / 5).ceilToDouble() : 10, |
| 175 | + verticalInterval: timeInterval, |
| 176 | + ), |
| 177 | + borderData: FlBorderData( |
| 178 | + show: true, |
| 179 | + border: const Border( |
| 180 | + bottom: BorderSide(color: Colors.white38), |
| 181 | + left: BorderSide(color: Colors.white38), |
| 182 | + top: BorderSide(color: Colors.white38), |
| 183 | + right: BorderSide(color: Colors.white38), |
| 184 | + ), |
| 185 | + ), |
| 186 | + minY: 0, |
| 187 | + maxY: maxLux > 0 ? (maxLux * 1.1) : 100, |
| 188 | + maxX: maxTime > 0 ? maxTime : 10, |
| 189 | + minX: minTime, |
| 190 | + clipData: const FlClipData.all(), |
| 191 | + lineBarsData: [ |
| 192 | + LineChartBarData( |
| 193 | + spots: spots, |
| 194 | + isCurved: true, |
| 195 | + color: Colors.cyan, |
| 196 | + barWidth: screenWidth < 400 ? 1.5 : 2.0, |
| 197 | + isStrokeCapRound: true, |
| 198 | + dotData: const FlDotData(show: false), |
| 199 | + belowBarData: BarAreaData(show: false), |
| 200 | + ), |
| 201 | + ], |
| 202 | + ), |
| 203 | + ); |
| 204 | + } |
| 205 | +} |
0 commit comments