-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
bar_chart_sample7.dart
219 lines (206 loc) · 6.51 KB
/
bar_chart_sample7.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import 'dart:math' as math;
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
class BarChartSample7 extends StatefulWidget {
BarChartSample7({super.key});
final shadowColor = const Color(0xFFCCCCCC);
final dataList = [
const _BarData(AppColors.contentColorYellow, 18, 18),
const _BarData(AppColors.contentColorGreen, 17, 8),
const _BarData(AppColors.contentColorOrange, 10, 15),
const _BarData(AppColors.contentColorPink, 2.5, 5),
const _BarData(AppColors.contentColorBlue, 2, 2.5),
const _BarData(AppColors.contentColorRed, 2, 2),
];
@override
State<BarChartSample7> createState() => _BarChartSample7State();
}
class _BarChartSample7State extends State<BarChartSample7> {
BarChartGroupData generateBarGroup(
int x,
Color color,
double value,
double shadowValue,
) {
return BarChartGroupData(
x: x,
barRods: [
BarChartRodData(
toY: value,
color: color,
width: 6,
),
BarChartRodData(
toY: shadowValue,
color: widget.shadowColor,
width: 6,
),
],
showingTooltipIndicators: touchedGroupIndex == x ? [0] : [],
);
}
int touchedGroupIndex = -1;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(24),
child: AspectRatio(
aspectRatio: 1.4,
child: BarChart(
BarChartData(
alignment: BarChartAlignment.spaceBetween,
borderData: FlBorderData(
show: true,
border: Border.symmetric(
horizontal: BorderSide(
color: AppColors.borderColor.withOpacity(0.2),
),
),
),
titlesData: FlTitlesData(
show: true,
leftTitles: AxisTitles(
drawBelowEverything: true,
sideTitles: SideTitles(
showTitles: true,
reservedSize: 30,
getTitlesWidget: (value, meta) {
return Text(
value.toInt().toString(),
textAlign: TextAlign.left,
);
},
),
),
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 36,
getTitlesWidget: (value, meta) {
final index = value.toInt();
return SideTitleWidget(
axisSide: meta.axisSide,
child: _IconWidget(
color: widget.dataList[index].color,
isSelected: touchedGroupIndex == index,
),
);
},
),
),
rightTitles: const AxisTitles(),
topTitles: const AxisTitles(),
),
gridData: FlGridData(
show: true,
drawVerticalLine: false,
getDrawingHorizontalLine: (value) => FlLine(
color: AppColors.borderColor.withOpacity(0.2),
strokeWidth: 1,
),
),
barGroups: widget.dataList.asMap().entries.map((e) {
final index = e.key;
final data = e.value;
return generateBarGroup(
index,
data.color,
data.value,
data.shadowValue,
);
}).toList(),
maxY: 20,
barTouchData: BarTouchData(
enabled: true,
handleBuiltInTouches: false,
touchTooltipData: BarTouchTooltipData(
getTooltipColor: (group) => Colors.transparent,
tooltipMargin: 0,
getTooltipItem: (
BarChartGroupData group,
int groupIndex,
BarChartRodData rod,
int rodIndex,
) {
return BarTooltipItem(
rod.toY.toString(),
TextStyle(
fontWeight: FontWeight.bold,
color: rod.color,
fontSize: 18,
shadows: const [
Shadow(
color: Colors.black26,
blurRadius: 12,
)
],
),
);
},
),
touchCallback: (event, response) {
if (event.isInterestedForInteractions &&
response != null &&
response.spot != null) {
setState(() {
touchedGroupIndex = response.spot!.touchedBarGroupIndex;
});
} else {
setState(() {
touchedGroupIndex = -1;
});
}
},
),
),
),
),
);
}
}
class _BarData {
const _BarData(this.color, this.value, this.shadowValue);
final Color color;
final double value;
final double shadowValue;
}
class _IconWidget extends ImplicitlyAnimatedWidget {
const _IconWidget({
required this.color,
required this.isSelected,
}) : super(duration: const Duration(milliseconds: 300));
final Color color;
final bool isSelected;
@override
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() =>
_IconWidgetState();
}
class _IconWidgetState extends AnimatedWidgetBaseState<_IconWidget> {
Tween<double>? _rotationTween;
@override
Widget build(BuildContext context) {
final rotation = math.pi * 4 * _rotationTween!.evaluate(animation);
final scale = 1 + _rotationTween!.evaluate(animation) * 0.5;
return Transform(
transform: Matrix4.rotationZ(rotation).scaled(scale, scale),
origin: const Offset(14, 14),
child: Icon(
widget.isSelected ? Icons.face_retouching_natural : Icons.face,
color: widget.color,
size: 28,
),
);
}
@override
void forEachTween(TweenVisitor<dynamic> visitor) {
_rotationTween = visitor(
_rotationTween,
widget.isSelected ? 1.0 : 0.0,
(dynamic value) => Tween<double>(
begin: value as double,
end: widget.isSelected ? 1.0 : 0.0,
),
) as Tween<double>?;
}
}