2000calories / flutter_easy_rich_text

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

Custom paragraph spacing breaks with "selectable: true" #47

Closed Lootwig closed 10 months ago

Lootwig commented 1 year ago
EasyRichText(
      'First paragraph\n\nSecond paragraph',
      // selectable: true,
      patternList: [
        EasyRichTextPattern(
          targetString: '\n',
          matchWordBoundaries: false,
          style: const TextStyle(fontSize: 2),
        ),
      ],
    )

In the code above, the pattern is used to create a more natural spacing between paragraphs. However, when uncommenting the selectable line, the pattern doesn't work anymore - the double line break gets rendered at full font size.

flutter doctor ``` Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.13.2, on macOS 13.5.1 22G90 darwin-arm64, locale en-US) [!] Android toolchain - develop for Android devices (Android SDK version 34.0.0) ✗ cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. ✗ Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/macos#android-setup for more details. [✓] Xcode - develop for iOS and macOS (Xcode 15.0) [✓] Chrome - develop for the web [!] Android Studio (version unknown) ✗ Unable to determine Android Studio version. ✗ Unable to find bundled Java version. [✓] IntelliJ IDEA Ultimate Edition (version 2023.2.2) [✓] VS Code (version 1.79.2) [✓] VS Code (version 1.79.0-insider) [✓] Connected device (3 available) [✓] Network resources ! Doctor found issues in 2 categories. ```
2000calories commented 1 year ago

EasyRichText uses SelectableText.rich for the selectable feature. Here is the actual widget behind. Somehow the TextStyle in TextSpan does not work when text is "\n". If you can find a workaround, then I can implement it.

const SelectableText.rich(
  TextSpan(
    children: [
      TextSpan(
        text: "First paragraph",
      ),
      TextSpan(
        text: "\n",
        style: TextStyle(
          fontSize: 2,
        ),
      ),
      TextSpan(
        text: "\n",
        style: TextStyle(
          fontSize: 2,
        ),
      ),
      TextSpan(
        text: "Second paragraph",
      ),
    ],
    // style: const TextStyle(fontSize: 2),
  ),
)
Lootwig commented 1 year ago

hey @2000calories I've played around a bit with the props and setting strutStyle seems to do the trick:

SelectableText.rich(
          TextSpan(
            children: [
              TextSpan(text: "First paragraph"),
              TextSpan(
                text: "\n\n",
                style: TextStyle(fontSize: 8, height: 1),
              ),
              TextSpan(text: "Second paragraph"),
            ],
          ),
          strutStyle: StrutStyle(height: 0),
        )
image

From https://api.flutter.dev/flutter/painting/StrutStyle-class.html:

Defines the strut, which sets the minimum height a line can be relative to the baseline.

Strut applies to all lines in the paragraph. Strut is a feature that allows minimum line heights to be set. The effect is as if a zero width space was included at the beginning of each line in the paragraph.

This also solves the issue when applied to the EasyRichText widget.