@@ -2,6 +2,8 @@ import 'dart:math';
22
33import 'package:material_ui/material_ui.dart' ;
44
5+ import '../corner_radius.dart' ;
6+ import 'text_painter_cache.dart' ;
57import '../depth_mode.dart' ;
68import '../depth_style.dart' ;
79import '../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