Skip to content

Commit dc23be5

Browse files
committed
refactor: move normalizeBorder to the utils class for reusability
1 parent b799343 commit dc23be5

3 files changed

Lines changed: 41 additions & 15 deletions

File tree

lib/src/chart/bar_chart/bar_chart_data.dart

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -299,20 +299,6 @@ class BarChartGroupData with EquatableMixin {
299299
];
300300
}
301301

302-
Border? _normalizeBorder(Border? border, double width) {
303-
if (border == null) {
304-
return null;
305-
}
306-
307-
final utils = Utils();
308-
return Border(
309-
top: utils.normalizeBorderSide(border.top, width),
310-
right: utils.normalizeBorderSide(border.right, width),
311-
bottom: utils.normalizeBorderSide(border.bottom, width),
312-
left: utils.normalizeBorderSide(border.left, width),
313-
);
314-
}
315-
316302
/// Holds data about rendering each rod (or bar) in the [BarChart].
317303
class BarChartRodData with EquatableMixin {
318304
/// [BarChart] renders rods vertically from zero to [toY],
@@ -364,7 +350,7 @@ class BarChartRodData with EquatableMixin {
364350
width = width ?? 8,
365351
borderRadius = Utils().normalizeBorderRadius(borderRadius, width ?? 8),
366352
borderSide = Utils().normalizeBorderSide(borderSide, width ?? 8),
367-
border = _normalizeBorder(border, width ?? 8),
353+
border = Utils().normalizeBorder(border, width ?? 8),
368354
backDrawRodData = backDrawRodData ?? BackgroundBarChartRodData(),
369355
rodStackItems = rodStackItems ?? const [];
370356

lib/src/utils/utils.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ class Utils {
107107
return borderSide.copyWith(width: borderWidth);
108108
}
109109

110+
/// Decreases each side of [border] to <= width / 2.
111+
Border? normalizeBorder(Border? border, double width) {
112+
if (border == null) {
113+
return null;
114+
}
115+
116+
return Border(
117+
top: normalizeBorderSide(border.top, width),
118+
right: normalizeBorderSide(border.right, width),
119+
bottom: normalizeBorderSide(border.bottom, width),
120+
left: normalizeBorderSide(border.left, width),
121+
);
122+
}
123+
110124
/// Returns an efficient interval for showing axis titles, or grid lines or ...
111125
///
112126
/// If there isn't any provided interval, we use this function to calculate an interval to apply,

test/utils/utils_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,32 @@ void main() {
115115
expect(Utils().normalizeBorderSide(input2, 40), output2);
116116
});
117117

118+
test('normalizeBorder()', () {
119+
const input1 = Border(
120+
top: BorderSide(width: 4),
121+
right: BorderSide(width: 8),
122+
bottom: BorderSide(width: 10),
123+
left: BorderSide(width: 2),
124+
);
125+
const output1 = input1;
126+
expect(Utils().normalizeBorder(input1, 40), output1);
127+
128+
const input2 = Border(
129+
top: BorderSide(width: 24),
130+
right: BorderSide(width: 30),
131+
bottom: BorderSide(width: 21),
132+
left: BorderSide(),
133+
);
134+
const output2 = Border(
135+
top: BorderSide(width: 20),
136+
right: BorderSide(width: 20),
137+
bottom: BorderSide(width: 20),
138+
left: BorderSide(),
139+
);
140+
expect(Utils().normalizeBorder(input2, 40), output2);
141+
expect(Utils().normalizeBorder(null, 40), isNull);
142+
});
143+
118144
test('lerp gradient', () {
119145
expect(
120146
lerpGradient(

0 commit comments

Comments
 (0)