From 66ff0f72d9948f55494efe6489ff1c4d43f1b94f Mon Sep 17 00:00:00 2001 From: Yong Shean Date: Thu, 12 Oct 2023 23:15:06 +0800 Subject: [PATCH] Add FontWeight support --- .../lib/src/text_element.dart | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/community_charts_flutter/lib/src/text_element.dart b/community_charts_flutter/lib/src/text_element.dart index 7cda020..be62c9c 100644 --- a/community_charts_flutter/lib/src/text_element.dart +++ b/community_charts_flutter/lib/src/text_element.dart @@ -22,7 +22,7 @@ import 'package:community_charts_common/community_charts_common.dart' as common TextMeasurement, TextStyle; import 'package:flutter/rendering.dart' - show Color, TextBaseline, TextPainter, TextSpan, TextStyle; + show Color, TextBaseline, TextPainter, TextSpan, TextStyle, FontWeight; /// Flutter implementation for text measurement and painter. class TextElement implements common.TextElement { @@ -152,6 +152,7 @@ class TextElement implements common.TextElement { color: color, fontSize: textStyle?.fontSize?.toDouble(), fontFamily: textStyle?.fontFamily, + fontWeight: _effectiveFontWeight, height: textStyle?.lineHeight))) ..textDirection = TextDirection.ltr // TODO Flip once textAlign works @@ -183,4 +184,17 @@ class TextElement implements common.TextElement { _painterReady = true; } + + FontWeight? get _effectiveFontWeight { + final name = textStyle?.fontWeight?.toLowerCase(); + if (name == null) return null; + final value = int.tryParse(name); + if (value == null) { + if (name == 'normal') return FontWeight.normal; + if (name == 'bold') return FontWeight.bold; + } else { + return FontWeight.values[value.clamp(100, 900) ~/ 100 - 1]; + } + return null; + } }