andyduke / styled_text_package

Text widget with formatted text using tags. Makes it easier to use formatted text in multilingual applications.
https://pub.dev/packages/styled_text
BSD 3-Clause "New" or "Revised" License
74 stars 48 forks source link

Hopefully you can pass `Context` to `StyledTextCustomTag#StyledTextCustomTagParser` #78

Closed fingerart closed 3 weeks ago

andyduke commented 3 weeks ago

Is it really necessary? What problem will it solve, can you give a code example?

fingerart commented 3 weeks ago

It is very necessary to get the theme through context to adapt the colors of different themes.

  // <font color="theme_color_name|hex_number" size="number" face="font_family" weight="number" />
  'font': StyledTextCustomTag(parse: (context, baseStyle, attributes) {
    final rawColor = attributes['color'];
    Color? color = context.appColors.byName(rawColor) ?? parseColor(rawColor);

    return TextStyle(color: color);
  })
andyduke commented 3 weeks ago

You can use the parent widget context:

  @override
  Widget build(BuildContext context) {
    //                        ^-----
    return Scaffold(
      body: Center(
        child: StyledText(
          text: 'Hello, <font color="accent">World</font>!',
          tags: {
            'font': StyledTextCustomTag(
              parse: (baseStyle, attributes) {
                final rawColor = attributes['color'];
                Color? color = context.appColors.byName(rawColor) ?? parseColor(rawColor);

                return TextStyle(color: color);
              },
            ),
          },
        ),
      ),
    );
  }