Search code examples
flame

met issue when create TextComponent


I tried to create a textRenderer for my TextComponent and met the issue:

The argument type 'TextStyle' can't be assigned to the parameter type 'TextStyle?'.

Here is the code in onLoad:

final style = TextStyle(color: BasicPalette.white.color);
final regular = TextPaint(style: style); // style is error

so this is my first question.

====update=====

Below question is solved since I found TextBoxComponent which met my needs.

My second question is there is no padding value in TextComponent right? So I have to somehow use such method to know the text width:

  Size _textSize(String text, TextStyle style) {
    final TextPainter textPainter = TextPainter(
        text: TextSpan(text: text, style: style), maxLines: 1, textDirection: TextDirection.ltr)
      ..layout(minWidth: 0, maxWidth: double.infinity);
    return textPainter.size;}

Since this method is from flutter widget, I feel maybe it is the wrong way to detect the text width. If there is no good solution in my case maybe I have to use flutter tabView and add this as an overlay? Thank you for any help!


Solution

  • In case someone didnt know, I realise that I need to

    import 'package:flutter/material.dart';
    

    instead of

    import 'dart:ui';
    

    I will mark this as answer after two days