Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bone.text does not respect TextStyle.height #45

Open
zamahaka opened this issue Nov 8, 2024 · 0 comments
Open

Bone.text does not respect TextStyle.height #45

zamahaka opened this issue Nov 8, 2024 · 0 comments

Comments

@zamahaka
Copy link

zamahaka commented Nov 8, 2024

Hi, thanks for an awesome package!

I found one caveat with Bote.text though. When using TextStyle with height set to not 1, bone does not use height matching to height of corresponing Text widget.

Example:
image

Example code:

class TextBoneLineHeightExample extends StatelessWidget {
  const TextBoneLineHeightExample({super.key});

  @override
  Widget build(BuildContext context) {
    final containedText = Container(
      color: Colors.blue.withOpacity(0.25),
      child: Text('Sample text'),
    );

    return Skeletonizer.zone(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('height: 1.5'),
          DefaultTextStyle(
            style: TextStyle(fontSize: 40, height: 1.5, color: Colors.red),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Bone.text(words: 2),
                containedText,
              ],
            ),
          ),
          Text('height: 0.5'),
          DefaultTextStyle(
            style: TextStyle(fontSize: 40, height: 0.5, color: Colors.red),
            child: Row(
              children: [
                Bone.text(words: 2),
                containedText,
              ],
            ),
          ),
          Text('height: 1.5 | With hack'),
          DefaultTextStyle(
            style: TextStyle(fontSize: 40, height: 1.5, color: Colors.red),
            child: Row(
              children: [
                TextBoneLineHeightHack(words: 2),
                containedText,
              ],
            ),
          ),
        ],
      ),
    );
  }
}

/// Bone.text should size itself based on font size and height, but it does not
/// This widget overrides incoming [TextStyle], overriding its fontSize to multiple of height.
/// This approach is not ideal, as it messes with words width
class TextBoneLineHeightHack extends StatelessWidget {
  const TextBoneLineHeightHack({
    super.key,
    this.words = 3,
    this.style,
  });

  final int words;
  final TextStyle? style;

  @override
  Widget build(BuildContext context) {
    TextStyle style = this.style ?? DefaultTextStyle.of(context).style;

    if (style.fontSize != null && style.height != null) {
      style = style.copyWith(
        fontSize: style.fontSize! * style.height!,
        height: 1,
      );
    }

    return Bone.text(
      style: style,
      words: words,
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant