simc / auto_size_text

Flutter widget that automatically resizes text to fit perfectly within its bounds.
https://pub.dev/packages/auto_size_text
MIT License
2.06k stars 241 forks source link

AutoSizeText.rich only working when style is defined both in AutoSizeText and TextSpan #7

Closed Koridev closed 5 years ago

Koridev commented 5 years ago

When using AutoSizeText.rich, it seems that I can only have correct autosizing when the fontSize is declared both in the AutoSizeText's style and in the TextSpan's, unlike what is said in the documentation.

A short sample to test it:

class MyHomePage extends StatelessWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      appBar: AppBar(
        title: TextField(),
      ),
      body: SafeArea(
        child: Column(children: <Widget>[
          Expanded(child: Center(child: SizedBox(width: 200, height: 200, child: AutoSizeText.rich(TextSpan(text: "Style is defined only in AutoSizeText",), style: TextStyle(fontSize: 200),),),),),
          Expanded(child: Center(child: SizedBox(width: 200, height: 200, child: AutoSizeText.rich(TextSpan(text: "Style is defined only in TextSpan", style: TextStyle(fontSize: 200)),),),),),
          Expanded(child: Center(child: SizedBox(width: 200, height: 200, child: AutoSizeText.rich(TextSpan(text: "Style is define in both AutoSizeText and TextSpan", style: TextStyle(fontSize: 200)), style: TextStyle(fontSize: 200),),),),),
        ],),
      ),
    );
  }
}

Only the last widget of the Column will be auto sized.

Using auto_size_text: ^1.1.0

simc commented 5 years ago

Thanks for reporting this. I'll fix it :+1:

simc commented 5 years ago

The case where only AutoSizeText has a style was a bug that I fixed.

The other case wasn't: Since your AutoSizeText doesn't have a style, the default style is used (probably fontSize = 14). To fit the large span, AutoSizeText has to scale the text down. But the default minFontSize is 12 and since the "reference fontSize" is 14, the text is not scaled down very far. Just set minFontSize to 0.1 and it will work. Note: You probably also have to use a smaller stepGranularity (like 0.1) because the steps will be very large.

It would be very helpful if you could verify that the first case is fixed before I release a new version.

Just add this to your dependecies:

dependencies:
  auto_size_text:
    git:
      url: git://github.com/leisim/auto_size_text.git
      ref: style-fix

Edit: I added a troubleshooting guide for the second case.

Koridev commented 5 years ago

Yes, the fix works!