Skip to content

Commit 18007d7

Browse files
committed
Use new switch-case return feature in our chart samples
1 parent 77c8111 commit 18007d7

15 files changed

+284
-596
lines changed

example/lib/presentation/samples/bar/bar_chart_sample1.dart

Lines changed: 42 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,19 @@ class BarChartSample1State extends State<BarChartSample1> {
137137
);
138138
}
139139

140-
List<BarChartGroupData> showingGroups() => List.generate(7, (i) {
141-
switch (i) {
142-
case 0:
143-
return makeGroupData(0, 5, isTouched: i == touchedIndex);
144-
case 1:
145-
return makeGroupData(1, 6.5, isTouched: i == touchedIndex);
146-
case 2:
147-
return makeGroupData(2, 5, isTouched: i == touchedIndex);
148-
case 3:
149-
return makeGroupData(3, 7.5, isTouched: i == touchedIndex);
150-
case 4:
151-
return makeGroupData(4, 9, isTouched: i == touchedIndex);
152-
case 5:
153-
return makeGroupData(5, 11.5, isTouched: i == touchedIndex);
154-
case 6:
155-
return makeGroupData(6, 6.5, isTouched: i == touchedIndex);
156-
default:
157-
return throw Error();
140+
List<BarChartGroupData> showingGroups() => List.generate(
141+
7,
142+
(i) => switch (i) {
143+
0 => makeGroupData(0, 5, isTouched: i == touchedIndex),
144+
1 => makeGroupData(1, 6.5, isTouched: i == touchedIndex),
145+
2 => makeGroupData(2, 5, isTouched: i == touchedIndex),
146+
3 => makeGroupData(3, 7.5, isTouched: i == touchedIndex),
147+
4 => makeGroupData(4, 9, isTouched: i == touchedIndex),
148+
5 => makeGroupData(5, 11.5, isTouched: i == touchedIndex),
149+
6 => makeGroupData(6, 6.5, isTouched: i == touchedIndex),
150+
_ => throw Error(),
158151
}
159-
});
152+
);
160153

161154
BarChartData mainBarData() {
162155
return BarChartData(
@@ -167,32 +160,16 @@ class BarChartSample1State extends State<BarChartSample1> {
167160
tooltipHorizontalAlignment: FLHorizontalAlignment.right,
168161
tooltipMargin: -10,
169162
getTooltipItem: (group, groupIndex, rod, rodIndex) {
170-
String weekDay;
171-
switch (group.x) {
172-
case 0:
173-
weekDay = 'Monday';
174-
break;
175-
case 1:
176-
weekDay = 'Tuesday';
177-
break;
178-
case 2:
179-
weekDay = 'Wednesday';
180-
break;
181-
case 3:
182-
weekDay = 'Thursday';
183-
break;
184-
case 4:
185-
weekDay = 'Friday';
186-
break;
187-
case 5:
188-
weekDay = 'Saturday';
189-
break;
190-
case 6:
191-
weekDay = 'Sunday';
192-
break;
193-
default:
194-
throw Error();
195-
}
163+
String weekDay = switch (group.x) {
164+
0 => 'Monday',
165+
1 => 'Tuesday',
166+
2 => 'Wednesday',
167+
3 => 'Thursday',
168+
4 => 'Friday',
169+
5 => 'Saturday',
170+
6 => 'Sunday',
171+
_ => throw Error(),
172+
};
196173
return BarTooltipItem(
197174
'$weekDay\n',
198175
const TextStyle(
@@ -260,37 +237,20 @@ class BarChartSample1State extends State<BarChartSample1> {
260237
fontWeight: FontWeight.bold,
261238
fontSize: 14,
262239
);
263-
Widget text;
264-
switch (value.toInt()) {
265-
case 0:
266-
text = const Text('M', style: style);
267-
break;
268-
case 1:
269-
text = const Text('T', style: style);
270-
break;
271-
case 2:
272-
text = const Text('W', style: style);
273-
break;
274-
case 3:
275-
text = const Text('T', style: style);
276-
break;
277-
case 4:
278-
text = const Text('F', style: style);
279-
break;
280-
case 5:
281-
text = const Text('S', style: style);
282-
break;
283-
case 6:
284-
text = const Text('S', style: style);
285-
break;
286-
default:
287-
text = const Text('', style: style);
288-
break;
289-
}
240+
String text = switch (value.toInt()) {
241+
0 => 'M',
242+
1 => 'T',
243+
2 => 'W',
244+
3 => 'T',
245+
4 => 'F',
246+
5 => 'S',
247+
6 => 'S',
248+
_ => '',
249+
};
290250
return SideTitleWidget(
291251
meta: meta,
292252
space: 16,
293-
child: text,
253+
child: Text(text, style: style),
294254
);
295255
}
296256

@@ -327,61 +287,15 @@ class BarChartSample1State extends State<BarChartSample1> {
327287
borderData: FlBorderData(
328288
show: false,
329289
),
330-
barGroups: List.generate(7, (i) {
331-
switch (i) {
332-
case 0:
333-
return makeGroupData(
334-
0,
335-
Random().nextInt(15).toDouble() + 6,
336-
barColor: widget.availableColors[
337-
Random().nextInt(widget.availableColors.length)],
338-
);
339-
case 1:
340-
return makeGroupData(
341-
1,
342-
Random().nextInt(15).toDouble() + 6,
343-
barColor: widget.availableColors[
344-
Random().nextInt(widget.availableColors.length)],
345-
);
346-
case 2:
347-
return makeGroupData(
348-
2,
349-
Random().nextInt(15).toDouble() + 6,
350-
barColor: widget.availableColors[
351-
Random().nextInt(widget.availableColors.length)],
352-
);
353-
case 3:
354-
return makeGroupData(
355-
3,
356-
Random().nextInt(15).toDouble() + 6,
357-
barColor: widget.availableColors[
358-
Random().nextInt(widget.availableColors.length)],
359-
);
360-
case 4:
361-
return makeGroupData(
362-
4,
363-
Random().nextInt(15).toDouble() + 6,
364-
barColor: widget.availableColors[
365-
Random().nextInt(widget.availableColors.length)],
366-
);
367-
case 5:
368-
return makeGroupData(
369-
5,
370-
Random().nextInt(15).toDouble() + 6,
371-
barColor: widget.availableColors[
372-
Random().nextInt(widget.availableColors.length)],
373-
);
374-
case 6:
375-
return makeGroupData(
376-
6,
377-
Random().nextInt(15).toDouble() + 6,
378-
barColor: widget.availableColors[
379-
Random().nextInt(widget.availableColors.length)],
380-
);
381-
default:
382-
return throw Error();
383-
}
384-
}),
290+
barGroups: List.generate(
291+
7,
292+
(i) => makeGroupData(
293+
i,
294+
Random().nextInt(15).toDouble() + 6,
295+
barColor: widget
296+
.availableColors[Random().nextInt(widget.availableColors.length)],
297+
),
298+
),
385299
gridData: const FlGridData(show: false),
386300
);
387301
}

example/lib/presentation/samples/bar/bar_chart_sample3.dart

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,16 @@ class _BarChart extends StatelessWidget {
5050
fontWeight: FontWeight.bold,
5151
fontSize: 14,
5252
);
53-
String text;
54-
switch (value.toInt()) {
55-
case 0:
56-
text = 'Mn';
57-
break;
58-
case 1:
59-
text = 'Te';
60-
break;
61-
case 2:
62-
text = 'Wd';
63-
break;
64-
case 3:
65-
text = 'Tu';
66-
break;
67-
case 4:
68-
text = 'Fr';
69-
break;
70-
case 5:
71-
text = 'St';
72-
break;
73-
case 6:
74-
text = 'Sn';
75-
break;
76-
default:
77-
text = '';
78-
break;
79-
}
53+
String text = switch (value.toInt()) {
54+
0 => 'Mn',
55+
1 => 'Te',
56+
2 => 'Wd',
57+
3 => 'Tu',
58+
4 => 'Fr',
59+
5 => 'St',
60+
6 => 'Sn',
61+
_ => '',
62+
};
8063
return SideTitleWidget(
8164
meta: meta,
8265
space: 4,

example/lib/presentation/samples/bar/bar_chart_sample4.dart

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,14 @@ class BarChartSample4 extends StatefulWidget {
1717
class BarChartSample4State extends State<BarChartSample4> {
1818
Widget bottomTitles(double value, TitleMeta meta) {
1919
const style = TextStyle(fontSize: 10);
20-
String text;
21-
switch (value.toInt()) {
22-
case 0:
23-
text = 'Apr';
24-
break;
25-
case 1:
26-
text = 'May';
27-
break;
28-
case 2:
29-
text = 'Jun';
30-
break;
31-
case 3:
32-
text = 'Jul';
33-
break;
34-
case 4:
35-
text = 'Aug';
36-
break;
37-
default:
38-
text = '';
39-
break;
40-
}
20+
String text = switch (value.toInt()) {
21+
0 => 'Apr',
22+
1 => 'May',
23+
2 => 'Jun',
24+
3 => 'Jul',
25+
4 => 'Aug',
26+
_ => '',
27+
};
4128
return SideTitleWidget(
4229
meta: meta,
4330
child: Text(text, style: style),

example/lib/presentation/samples/bar/bar_chart_sample5.dart

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,16 @@ class BarChartSample5State extends State<BarChartSample5> {
3131

3232
Widget bottomTitles(double value, TitleMeta meta) {
3333
const style = TextStyle(color: Colors.white, fontSize: 10);
34-
String text;
35-
switch (value.toInt()) {
36-
case 0:
37-
text = 'Mon';
38-
break;
39-
case 1:
40-
text = 'Tue';
41-
break;
42-
case 2:
43-
text = 'Wed';
44-
break;
45-
case 3:
46-
text = 'Thu';
47-
break;
48-
case 4:
49-
text = 'Fri';
50-
break;
51-
case 5:
52-
text = 'Sat';
53-
break;
54-
case 6:
55-
text = 'Sun';
56-
break;
57-
default:
58-
text = '';
59-
break;
60-
}
34+
String text = switch (value.toInt()) {
35+
0 => 'Mon',
36+
1 => 'Tue',
37+
2 => 'Wed',
38+
3 => 'Thu',
39+
4 => 'Fri',
40+
5 => 'Sat',
41+
6 => 'Sun',
42+
_ => '',
43+
};
6144
return SideTitleWidget(
6245
meta: meta,
6346
child: Text(text, style: style),
@@ -66,32 +49,16 @@ class BarChartSample5State extends State<BarChartSample5> {
6649

6750
Widget topTitles(double value, TitleMeta meta) {
6851
const style = TextStyle(color: Colors.white, fontSize: 10);
69-
String text;
70-
switch (value.toInt()) {
71-
case 0:
72-
text = 'Mon';
73-
break;
74-
case 1:
75-
text = 'Tue';
76-
break;
77-
case 2:
78-
text = 'Wed';
79-
break;
80-
case 3:
81-
text = 'Thu';
82-
break;
83-
case 4:
84-
text = 'Fri';
85-
break;
86-
case 5:
87-
text = 'Sat';
88-
break;
89-
case 6:
90-
text = 'Sun';
91-
break;
92-
default:
93-
return Container();
94-
}
52+
String text = switch (value.toInt()) {
53+
0 => 'Mon',
54+
1 => 'Tue',
55+
2 => 'Wed',
56+
3 => 'Thu',
57+
4 => 'Fri',
58+
5 => 'Sat',
59+
6 => 'Sun',
60+
_ => '',
61+
};
9562
return SideTitleWidget(
9663
meta: meta,
9764
child: Text(text, style: style),

0 commit comments

Comments
 (0)