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 240 forks source link

AutoSizeGroupBuilder widget would be a good convenience feature #87

Open ravenblackx opened 3 years ago

ravenblackx commented 3 years ago

Every time someone is new to AutoSizeText, they initially make this mistake:

class MyWidget extends StatelessWidget {
  final _autoSizeGroup = AutoSizeGroup();
  @override Widget build() {
    return Column(
      children: [
        AutoSizeText('hello', autoSizeGroup: _autoSizeGroup),
        AutoSizeText('goodbye', autoSizeGroup: _autoSizeGroup),
      ],
    );
  }
}

The non-obvious mistake being that the AutoSizeGroup will keep being reconstructed any time the widget rebuilds, and lose its state, which can cause flickering and performance issues.

The addition of a standard AutoSizeGroupBuilder to capture the AutoSizeGroup in its own stateful widget would make it much simpler to use it correctly, like:

class MyWidget extends StatelessWidget {
  @override Widget build() {
    return AutoSizeGroupBuilder(
      builder: (_, autoSizeGroup) => Column(
        children: [
          AutoSizeText('hello', autoSizeGroup: autoSizeGroup),
          AutoSizeText('goodbye', autoSizeGroup: autoSizeGroup),
        ],
      ),
    );
  }
}
simc commented 3 years ago

I think that's a great idea 👍