Skip to content

Commit f079f21

Browse files
feat!: the last two corner radii take a BorderRadius
DepthChartStyle.radius rounds the readout box and HeatmapLegend.radius the legend bar; both were missed when the rest of the package moved over, so they were the only rectangles left that a caller could not shape.
1 parent 5fd4258 commit f079f21

3 files changed

Lines changed: 31 additions & 40 deletions

File tree

‎lib/src/depth_style.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:material_ui/material_ui.dart' show Color;
1+
import 'package:material_ui/material_ui.dart' show BorderRadius, Color, Radius;
22

33
/// Every colour the depth chart paints with.
44
class DepthChartColors {
@@ -53,7 +53,7 @@ class DepthChartStyle {
5353
/// Creates a style, defaulting to the values the chart ships with.
5454
const DepthChartStyle({
5555
this.lineWidth = 1.0,
56-
this.radius = 4.0,
56+
this.radius = const BorderRadius.all(Radius.circular(4)),
5757
this.strokeWidth = 0.6,
5858
this.space = 2.0,
5959
this.padding = 6.0,
@@ -67,7 +67,9 @@ class DepthChartStyle {
6767
});
6868

6969
final double lineWidth;
70-
final double radius;
70+
71+
/// The rounding of the readout box's corners, as drawn on the screen.
72+
final BorderRadius radius;
7173
final double strokeWidth;
7274

7375
final double space;

‎lib/src/heatmap/heatmap_chart.dart‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ class _HeatmapChartState extends State<HeatmapChart>
349349

350350
@override
351351
void dispose() {
352+
_text.dispose();
352353
_animation.dispose();
353354
super.dispose();
354355
}
@@ -754,7 +755,7 @@ class HeatmapLegend extends StatelessWidget {
754755
this.labelStyle,
755756
this.height = 10,
756757
this.width,
757-
this.radius = 3,
758+
this.radius = const BorderRadius.all(Radius.circular(3)),
758759
this.gap = 6,
759760
});
760761

@@ -776,8 +777,8 @@ class HeatmapLegend extends StatelessWidget {
776777
/// How long the bar is; null lets it take the room it is given.
777778
final double? width;
778779

779-
/// The bar's corner radius.
780-
final double radius;
780+
/// The rounding of the bar's corners, as drawn on the screen.
781+
final BorderRadius radius;
781782

782783
/// Space between the bar and its labels.
783784
final double gap;
@@ -790,7 +791,7 @@ class HeatmapLegend extends StatelessWidget {
790791
final colors = scale.colors;
791792
Widget bar = DecoratedBox(
792793
decoration: BoxDecoration(
793-
borderRadius: BorderRadius.circular(radius),
794+
borderRadius: radius,
794795
// One colour would make a gradient of nothing, so it is doubled.
795796
gradient: LinearGradient(
796797
colors: colors.length >= 2

‎lib/src/renderer/depth_painter.dart‎

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'dart:math';
22

33
import 'package:material_ui/material_ui.dart';
44

5+
import '../corner_radius.dart';
6+
import 'text_painter_cache.dart';
57
import '../depth_mode.dart';
68
import '../depth_style.dart';
79
import '../depth_translations.dart';
@@ -22,6 +24,7 @@ class DepthChartPainter extends CustomPainter {
2224
this.chartStyle,
2325
this.offset,
2426
this.chartTranslations, {
27+
required this.textCache,
2528
this.mode = DepthChartMode.cumulative,
2629
this.scale = DepthScale.linear,
2730
double? zoom,
@@ -313,7 +316,6 @@ class DepthChartPainter extends CustomPainter {
313316
void drawText(Canvas canvas) {
314317
for (int j = 0; j < mLineCount; j++) {
315318
final tp = getTextPainter(_axisLabel(j));
316-
tp.layout();
317319
tp.paint(
318320
canvas,
319321
Offset(mWidth - tp.width, mDrawHeight / mLineCount * j + tp.height / 2),
@@ -323,14 +325,12 @@ class DepthChartPainter extends CustomPainter {
323325
final startText =
324326
NumberUtil.formatFixed(mBuyData!.first.price, quoteUnit) ?? '';
325327
final startTP = getTextPainter(startText);
326-
startTP.layout();
327328
startTP.paint(canvas, Offset(0, getBottomTextY(startTP.height)));
328329

329330
final centerPrice = (mBuyData!.last.price + mSellData!.first.price) / 2;
330331

331332
final center = NumberUtil.formatFixed(centerPrice, quoteUnit) ?? '';
332333
final centerTP = getTextPainter(center);
333-
centerTP.layout();
334334
centerTP.paint(
335335
canvas,
336336
Offset(mDrawWidth - centerTP.width / 2, getBottomTextY(centerTP.height)),
@@ -339,7 +339,6 @@ class DepthChartPainter extends CustomPainter {
339339
final endText =
340340
NumberUtil.formatFixed(mSellData!.last.price, quoteUnit) ?? '';
341341
final endTP = getTextPainter(endText);
342-
endTP.layout();
343342
endTP.paint(
344343
canvas,
345344
Offset(mWidth - endTP.width, getBottomTextY(endTP.height)),
@@ -352,7 +351,6 @@ class DepthChartPainter extends CustomPainter {
352351
) ??
353352
'';
354353
final leftHalfTP = getTextPainter(leftHalfText);
355-
leftHalfTP.layout();
356354
leftHalfTP.paint(
357355
canvas,
358356
Offset(
@@ -368,7 +366,6 @@ class DepthChartPainter extends CustomPainter {
368366
) ??
369367
'';
370368
final rightHalfTP = getTextPainter(rightHalfText);
371-
rightHalfTP.layout();
372369
rightHalfTP.paint(
373370
canvas,
374371
Offset(
@@ -440,6 +437,7 @@ class DepthChartPainter extends CustomPainter {
440437
translations: chartTranslations,
441438
chartColors: chartColors,
442439
chartStyle: chartStyle,
440+
textCache: textCache,
443441
price: NumberUtil.format(entity.price, quoteUnit) ?? '',
444442
amount: NumberUtil.formatCompact(entity.vol, baseUnit),
445443
size: level == null
@@ -456,10 +454,7 @@ class DepthChartPainter extends CustomPainter {
456454
);
457455

458456
final rect = Rect.fromLTWH(dx, dy, popupPainter.width, popupPainter.height);
459-
final boxRect = RRect.fromRectAndRadius(
460-
rect,
461-
Radius.circular(chartStyle.radius),
462-
);
457+
final boxRect = roundedBox(rect, chartStyle.radius);
463458

464459
canvas.drawRRect(boxRect, selectPaint!);
465460
canvas.drawRRect(boxRect, selectBorderPaint!);
@@ -503,6 +498,7 @@ class DepthChartPainter extends CustomPainter {
503498
translations: chartTranslations,
504499
chartColors: chartColors,
505500
chartStyle: chartStyle,
501+
textCache: textCache,
506502
price: NumberUtil.format(entity.price, quoteUnit) ?? '',
507503
amount: NumberUtil.formatCompact(entity.vol, baseUnit),
508504
size: level == null
@@ -523,10 +519,7 @@ class DepthChartPainter extends CustomPainter {
523519
);
524520

525521
final rect = Rect.fromLTWH(dx, dy, popupPainter.width, popupPainter.height);
526-
final boxRect = RRect.fromRectAndRadius(
527-
rect,
528-
Radius.circular(chartStyle.radius),
529-
);
522+
final boxRect = roundedBox(rect, chartStyle.radius);
530523

531524
canvas.drawRRect(boxRect, selectPaint!);
532525
canvas.drawRRect(boxRect, selectBorderPaint!);
@@ -569,12 +562,12 @@ class DepthChartPainter extends CustomPainter {
569562

570563
double getSellX(int position) => position * mSellPointWidth! + mDrawWidth;
571564

572-
TextPainter getTextPainter(String text) => TextPainter(
573-
text: TextSpan(
574-
text: text,
575-
style: TextStyle(color: chartColors.defaultTextColor, fontSize: 10),
576-
),
577-
textDirection: TextDirection.ltr,
565+
/// Holds the laid-out labels between frames, and owns their disposal.
566+
final TextPainterCache textCache;
567+
568+
TextPainter getTextPainter(String text) => textCache.get(
569+
text,
570+
TextStyle(color: chartColors.defaultTextColor, fontSize: 10),
578571
);
579572

580573
double getBottomTextY(double textHeight) =>
@@ -626,18 +619,19 @@ class PopupPainter {
626619
required DepthChartTranslations translations,
627620
required this.chartColors,
628621
required this.chartStyle,
622+
required this.textCache,
629623
required String price,
630624
required String amount,
631625
String? size,
632626
}) {
633627
pricePaint = _getTextPainter(translations.price, price);
634628
amountPaint = _getTextPainter(translations.amount, amount);
635629
sizePaint = size == null ? null : _getTextPainter(translations.size, size);
636-
pricePaint.layout();
637-
amountPaint.layout();
638-
sizePaint?.layout();
639630
}
640631

632+
/// Holds the laid-out labels between frames, and owns their disposal.
633+
final TextPainterCache textCache;
634+
641635
final DepthChartColors chartColors;
642636
final DepthChartStyle chartStyle;
643637

@@ -686,14 +680,8 @@ class PopupPainter {
686680
);
687681
}
688682

689-
TextPainter _getTextPainter(String label, String content) {
690-
return TextPainter(
691-
text: TextSpan(
692-
text: '$label $content',
693-
style: TextStyle(color: chartColors.annotationColor, fontSize: 9),
694-
),
695-
textAlign: TextAlign.start,
696-
textDirection: TextDirection.ltr,
697-
);
698-
}
683+
TextPainter _getTextPainter(String label, String content) => textCache.get(
684+
'$label $content',
685+
TextStyle(color: chartColors.annotationColor, fontSize: 9),
686+
);
699687
}

0 commit comments

Comments
 (0)