Aidra Connect 10.0.2+16
Aidra Connect Mobile Application
Loading...
Searching...
No Matches
co2_stats_chart.dart
Go to the documentation of this file.
1import '/core/ui/widgets/custom_card.dart';
2import 'package:flutter/material.dart';
3import 'package:fl_chart/fl_chart.dart';
4import 'package:flutter_screenutil/flutter_screenutil.dart';
5
6class Co2StatisticsChart extends StatefulWidget {
7 final List<Co2Data> data;
8 final String timeFrame;
9
11 super.key,
12 required this.data,
13 required this.timeFrame,
14 });
15
16 @override
17 State<Co2StatisticsChart> createState() => _Co2StatisticsChartState();
18}
19
20class _Co2StatisticsChartState extends State<Co2StatisticsChart> {
22
23 @override
24 Widget build(BuildContext context) {
25 return CustomCard(
26 padding: EdgeInsets.all(12.sp),
27 child: Column(
28 children: [
29 Row(
30 mainAxisAlignment: MainAxisAlignment.spaceBetween,
31 children: [
32 Text(
33 'Co2 Statistics',
34 style: Theme.of(context).textTheme.displaySmall?.copyWith(
35 fontWeight: FontWeight.bold,
36 ),
37 ),
38 Container(
39 padding:
40 const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
41 decoration: BoxDecoration(
42 color:
43 Theme.of(context).colorScheme.secondary.withOpacity(0.11),
44 borderRadius: BorderRadius.circular(20),
45 ),
46 child: Row(
47 children: [
48 Text(
49 widget.timeFrame,
50 style: Theme.of(context).textTheme.bodySmall?.copyWith(
51 fontSize: 10.sp,
52 ),
53 ),
54 SizedBox(width: 3.sp),
55 Icon(
56 Icons.keyboard_arrow_down,
57 size: 13.sp,
58 ),
59 ],
60 ),
61 ),
62 ],
63 ),
64 SizedBox(height: 20.sp),
65 SizedBox(
66 height: 200.sp,
67 child: BarChart(
68 BarChartData(
69 barTouchData: BarTouchData(
70 touchTooltipData: BarTouchTooltipData(
71 getTooltipColor: (group) =>
72 Theme.of(context).colorScheme.secondary,
73 tooltipPadding: const EdgeInsets.all(8),
74 tooltipMargin: 8,
75 getTooltipItem: (group, groupIndex, rod, rodIndex) {
76 return BarTooltipItem(
77 '${widget.data[groupIndex].value.toStringAsFixed(0)} kg',
78 TextStyle(
79 color: Theme.of(context).colorScheme.onPrimary,
80 ),
81 );
82 },
83 ),
84 touchCallback: (FlTouchEvent event, barTouchResponse) {
85 setState(() {
86 if (event is! FlTapUpEvent &&
87 event is! FlPanEndEvent &&
88 event is! FlLongPressEnd) {
89 touchedIndex =
90 barTouchResponse?.spot?.touchedBarGroupIndex;
91 } else {
92 touchedIndex = -1;
93 }
94 });
95 },
96 ),
97 titlesData: FlTitlesData(
98 show: true,
99 rightTitles:
100 AxisTitles(sideTitles: SideTitles(showTitles: false)),
101 topTitles:
102 AxisTitles(sideTitles: SideTitles(showTitles: false)),
103 bottomTitles: AxisTitles(
104 sideTitles: SideTitles(
105 showTitles: true,
106 getTitlesWidget: (value, meta) {
107 return Padding(
108 padding: const EdgeInsets.only(top: 8.0),
109 child: Text(
110 widget.data[value.toInt()].month,
111 style:
112 Theme.of(context).textTheme.bodySmall?.copyWith(
113 fontSize: 9.5.sp,
114 ),
115 ),
116 );
117 },
118 ),
119 ),
120 leftTitles: const AxisTitles(
121 sideTitles: SideTitles(
122 showTitles: false,
123 ),
124 ),
125 ),
126 borderData: FlBorderData(show: false),
127 gridData: FlGridData(
128 show: true,
129 drawVerticalLine: false,
130 horizontalInterval: 5000,
131 getDrawingHorizontalLine: (value) {
132 return FlLine(
133 color: Theme.of(context).hintColor.withOpacity(0.2),
134 strokeWidth: 1,
135 dashArray: [5, 5],
136 );
137 },
138 ),
139 barGroups: widget.data.asMap().entries.map((entry) {
140 final index = entry.key;
141 final data = entry.value;
142 return BarChartGroupData(
143 x: index,
144 barRods: [
145 BarChartRodData(
146 toY: data.value,
147 color: touchedIndex == index
148 ? Theme.of(context).colorScheme.secondary
149 : Theme.of(context).colorScheme.primary,
150 width: 24,
151 borderRadius: const BorderRadius.only(
152 topLeft: Radius.circular(4),
153 topRight: Radius.circular(4),
154 ),
155 ),
156 ],
157 );
158 }).toList(),
159 ),
160 ),
161 ),
162 ],
163 ),
164 );
165 }
166}
167
168class Co2Data {
169 final String month;
170 final double value;
171
172 Co2Data({required this.month, required this.value});
173}
final String month
final double value
Co2Data({required this.month, required this.value})
final String timeFrame
const Co2StatisticsChart({ super.key, required this.data, required this.timeFrame, })
override State< Co2StatisticsChart > createState()
final List< Co2Data > data
class Co2StatisticsChart extends StatefulWidget touchedIndex
const CustomCard({ super.key, required this.child, this.padding, this.bgColor, })
final Widget child
final EdgeInsets padding
final Color color
Definition failures.dart:1
override Widget build(BuildContext context)
final double value