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
75 stars 48 forks source link

Ability to insert a WidgetSpan in a StyledText #28

Closed apalala-dev closed 3 years ago

apalala-dev commented 3 years ago

This package is very usefull, especially for internationalization, however I have an use case where I can't use it : WidgetSpans.

I was using RichText (without translations) before and I added a sort of Chip with an avatar and a name in a WidgetSpan.

I didn't find a way to use StyledText with WidgetSpan but that would be really cool !

Is there a way that I missed ?

andyduke commented 3 years ago

@Kobatsu Now there is no such possibility. In the near future I will think about how this could be done.

apalala-dev commented 3 years ago

I made a PR ( #29 ) since I had an idea on how this could be achieved. It might not be perfect but could be a base for this !

Zabadam commented 3 years ago

I made a PR ( #29 ) since I had an idea on how this could be achieved. It might not be perfect but could be a base for this !

I think this a great proposal!

apalala-dev commented 3 years ago

If you want to experiment with it, you can add it to your pubspec.yaml :

styled_text: #^2.0.0-nullsafety.0
    git:
      url: https://github.com/Kobatsu/styled_text_package.git
      path: styled_text/

I am using it in my own projet but I don't have enough users / tests on it to be sure it's free of bugs.

andyduke commented 3 years ago

I think it's time to slightly change the way that tag handlers are specified for more flexible extensibility.

I am planning to do as follows:

StyledText(
  'test <b>bold</b> <red>text</red>, <link to="url">link</link>',
  tags: {
    'b': StyledTextTag(style: TextStyle(fontWeight: FontWeight.w500)),
    'red': StyledTextTag(style: TextStyle(color: Colors.red)),
    'link': StyledTextTagAction(
      (args) => _openLink(args['to']),
      style: TextStyle(color: Colors.blue),
    ),
  },
);

This will allow for more flexibility in creating tag handlers, including embedding widgets in the text.

The styles parameter will be retained for a while for compatibility, but will be deprecated, styles and tags cannot be used at the same time.

Zabadam commented 3 years ago

Do you plan to have any parameters others than [StyledTextTag.style] or [StyledTextTagAction] callback?
(That is a mouthful for a small arguments class.)

I think it's time to slightly change the way that tag handlers are specified for more flexible extensibility.

StyledText(
  'test <b>bold</b> <red>text</red>, <link to="url">link</link>',
  tags: {
    'b': StyledTextTag(style: TextStyle(fontWeight: FontWeight.w500)),
    'red': StyledTextTag(style: TextStyle(color: Colors.red)),
    'link': StyledTextTagAction(
      (args) => _openLink(args['to']),
      style: TextStyle(color: Colors.blue),
    ),
  },
);

The styles parameter will be retained for a while for compatibility, but will be deprecated, styles and tags cannot be used at the same time.

andyduke commented 3 years ago

@Zabadam I thought about the fact that just to specify the text style, I would have to wrap the style in an additional class, so from the very beginning it was necessary to specify the styles in StyledText. But with the increase in functionality, this became a limiting factor, because to implement link handling, insert widgets, etc., I have to make proxy classes in the TextStyle and constantly monitor and update them when the TextStyle class is extended in Flutter.

Now at least there is a plan to create a new tag class for inserting widgets. Perhaps in the future there will be some more tag classes. Also, other developers can easily create inheritors from the StyledTextTag base class to create the functionality they need.

andyduke commented 3 years ago

@Kobatsu @Zabadam The new beta version with a different way of specifying the styles and behavior of tags, as well as inserting widgets, can be tried right now https://pub.dev/packages/styled_text/versions/3.0.0-beta+2