@@ -3,46 +3,29 @@ import 'package:fl_chart_app/util/extensions/color_extensions.dart';
33import 'package:fl_chart/fl_chart.dart' ;
44import 'package:flutter/material.dart' ;
55
6- class _BarChart extends StatelessWidget {
7- const _BarChart ( );
6+ class BarChartSample3 extends StatefulWidget {
7+ const BarChartSample3 ({ super .key} );
88
99 @override
10- Widget build (BuildContext context) {
11- return BarChart (
12- BarChartData (
13- barTouchData: barTouchData,
14- titlesData: titlesData,
15- borderData: borderData,
16- barGroups: barGroups,
17- gridData: const FlGridData (show: false ),
18- alignment: BarChartAlignment .spaceAround,
19- maxY: 20 ,
20- ),
21- );
22- }
10+ State <StatefulWidget > createState () => BarChartSample3State ();
11+ }
12+
13+ class BarChartSample3State extends State <BarChartSample3 > {
14+ int ? touchedGroupIndex;
2315
2416 BarTouchData get barTouchData => BarTouchData (
25- enabled: false ,
26- touchTooltipData: BarTouchTooltipData (
27- getTooltipColor: (group) => Colors .transparent,
28- tooltipPadding: EdgeInsets .zero,
29- tooltipMargin: 8 ,
30- getTooltipItem: (
31- BarChartGroupData group,
32- int groupIndex,
33- BarChartRodData rod,
34- int rodIndex,
35- ) {
36- return BarTooltipItem (
37- rod.toY.round ().toString (),
38- const TextStyle (
39- color: AppColors .contentColorCyan,
40- fontWeight: FontWeight .bold,
41- ),
42- );
43- },
44- ),
45- );
17+ enabled: true ,
18+ handleBuiltInTouches: false ,
19+ touchCallback: (event, response) {
20+ setState (() {
21+ final groupI = response? .spot? .touchedBarGroupIndex;
22+ if (event.isInterestedForInteractions && groupI != null ) {
23+ touchedGroupIndex = groupI;
24+ } else {
25+ touchedGroupIndex = null ;
26+ }
27+ });
28+ });
4629
4730 Widget getTitles (double value, TitleMeta meta) {
4831 final style = TextStyle (
@@ -100,93 +83,47 @@ class _BarChart extends StatelessWidget {
10083 end: Alignment .topCenter,
10184 );
10285
103- List <BarChartGroupData > get barGroups => [
104- BarChartGroupData (
105- x: 0 ,
106- barRods: [
107- BarChartRodData (
108- toY: 8 ,
109- gradient: _barsGradient,
110- )
111- ],
112- showingTooltipIndicators: [0 ],
113- ),
114- BarChartGroupData (
115- x: 1 ,
116- barRods: [
117- BarChartRodData (
118- toY: 10 ,
119- gradient: _barsGradient,
120- )
121- ],
122- showingTooltipIndicators: [0 ],
123- ),
124- BarChartGroupData (
125- x: 2 ,
126- barRods: [
127- BarChartRodData (
128- toY: 14 ,
129- gradient: _barsGradient,
130- )
131- ],
132- showingTooltipIndicators: [0 ],
133- ),
134- BarChartGroupData (
135- x: 3 ,
86+ List <BarChartGroupData > get barGroups =>
87+ [8 , 10 , 14 , 15 , 13 , 10 , 16 ].asMap ().entries.map ((entry) {
88+ int i = entry.key;
89+ int value = entry.value;
90+ final isTouched = i == touchedGroupIndex;
91+ return BarChartGroupData (
92+ x: i,
13693 barRods: [
13794 BarChartRodData (
138- toY: 15 ,
95+ toY: value. toDouble () ,
13996 gradient: _barsGradient,
140- )
141- ],
142- showingTooltipIndicators: [0 ],
143- ),
144- BarChartGroupData (
145- x: 4 ,
146- barRods: [
147- BarChartRodData (
148- toY: 13 ,
149- gradient: _barsGradient,
150- )
151- ],
152- showingTooltipIndicators: [0 ],
153- ),
154- BarChartGroupData (
155- x: 5 ,
156- barRods: [
157- BarChartRodData (
158- toY: 10 ,
159- gradient: _barsGradient,
160- )
161- ],
162- showingTooltipIndicators: [0 ],
163- ),
164- BarChartGroupData (
165- x: 6 ,
166- barRods: [
167- BarChartRodData (
168- toY: 16 ,
169- gradient: _barsGradient,
170- )
97+ label: BarChartRodLabel (
98+ text: value.toString (),
99+ style: TextStyle (
100+ color: AppColors .contentColorCyan,
101+ fontWeight: FontWeight .bold,
102+ fontSize: isTouched ? 40 : 18 ,
103+ ),
104+ ),
105+ ),
171106 ],
172- showingTooltipIndicators: [0 ],
173- ),
174- ];
175- }
176-
177- class BarChartSample3 extends StatefulWidget {
178- const BarChartSample3 ({super .key});
107+ );
108+ }).toList ();
179109
180- @override
181- State <StatefulWidget > createState () => BarChartSample3State ();
182- }
183-
184- class BarChartSample3State extends State <BarChartSample3 > {
185110 @override
186111 Widget build (BuildContext context) {
187- return const AspectRatio (
112+ return AspectRatio (
188113 aspectRatio: 1.6 ,
189- child: _BarChart (),
114+ child: BarChart (
115+ duration: Duration (milliseconds: 100 ),
116+ curve: Curves .easeOutQuad,
117+ BarChartData (
118+ barTouchData: barTouchData,
119+ titlesData: titlesData,
120+ borderData: borderData,
121+ barGroups: barGroups,
122+ gridData: const FlGridData (show: false ),
123+ alignment: BarChartAlignment .spaceAround,
124+ maxY: 20 ,
125+ ),
126+ ),
190127 );
191128 }
192129}
0 commit comments