2000calories / flutter_easy_rich_text

The EasyRichText widget provides an easy way to use RichText.
https://pub.dev/packages/easy_rich_text
MIT License
79 stars 34 forks source link

Allow easier extension of EasyRichText widgets #60

Closed leewrigg closed 5 months ago

leewrigg commented 5 months ago

Hi @2000calories 👋

I have a use case where I need to integrate AutoSizeText with EasyRichText. I appreciate that not all users of this package will need that, so I didn't think it necessary to add AutoSizeText as a dependency for the whole package.

I did however make a change that makes it easier to extend EasyRichText widgets by creating a new method which is only responsible for building the widget.

For my example, with this change I was able to use this in my app to build an AutoSizeEasyRichText widget with very little boilerplate code:

import 'package:auto_size_text/auto_size_text.dart';
import 'package:easy_rich_text/easy_rich_text.dart';
import 'package:flutter/widgets.dart';

 class AutoSizeEasyRichText extends EasyRichText {
  AutoSizeEasyRichText(
    super.text, {
    super.key,
    super.defaultStyle,
    super.textAlign,
    super.maxLines,
    super.patternList,
    this.minFontSize,
  });

  final double? minFontSize;

  @override
  Widget buildTextWidget(BuildContext context, List<InlineSpan> textSpans) {
    return AutoSizeText.rich(
      TextSpan(
        style: defaultStyle ?? DefaultTextStyle.of(context).style,
        children: textSpans,
      ),
      locale: locale,
      minFontSize: minFontSize ?? 1,
      maxLines: maxLines,
      overflow: overflow,
      softWrap: softWrap,
      strutStyle: strutStyle,
      textAlign: textAlign,
      textDirection: textDirection,
    );
  }
}