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

Create an DefaultAutoSizeGroup #121

Open
jamesblasco opened this issue Aug 22, 2022 · 1 comment
Open

Create an DefaultAutoSizeGroup #121

jamesblasco opened this issue Aug 22, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@jamesblasco
Copy link

jamesblasco commented Aug 22, 2022

Describe the solution you'd like

I would like to make use of AutoSizeGroup without the need to create an stateful widget time.

DefaultAutoSizeGroup(
 child: Column(
      children: [
           AutoSizeText('hey'),
           AutoSizeText('hey')
      ]
   )
)

This would behave similar to what any other DefaultFoo widgets behave. It would inject an inherited widget that contains a AutoSizeGroup that is used by any AutoSizeText if no autoSizeGroup is passed. Similar cases are DefaultTabController or DefaultTextStyle.

Implementation:

class DefaultAutoSizeGroup extends StatefulWidget {
  const DefaultAutoSizeGroup({super.key, required this.child}) : super();

  static AutoSizeGroup? of(BuildContext context) {
    return context
        .dependOnInheritedWidgetOfExactType<_InheritedAutoSizeGroup>()
        ?.autoSizeGroup;
  }

  final Widget child;

  @override
  State<DefaultAutoSizeGroup> createState() => _DefaultAutoSizeGroupState();
}

class _DefaultAutoSizeGroupState extends State<DefaultAutoSizeGroup> {
  final AutoSizeGroup autoSizeGroup = AutoSizeGroup();

  @override
  Widget build(BuildContext context) {
    return _InheritedAutoSizeGroup(
      autoSizeGroup: autoSizeGroup,
      child: widget.child,
    );
  }
}

class _InheritedAutoSizeGroup extends InheritedWidget {
  const _InheritedAutoSizeGroup({
    required super.child,
    required this.autoSizeGroup,
  }) : super();

  final AutoSizeGroup autoSizeGroup;

  @override
  bool updateShouldNotify(_InheritedAutoSizeGroup oldWidget) {
    return autoSizeGroup != oldWidget.autoSizeGroup;
  }
}

Version

  • Flutter version: [e.g. 1.5.4]
  • auto_size_text version: [e.g. 1.2.1]
@jamesblasco jamesblasco added the enhancement New feature or request label Aug 22, 2022
@jamesblasco
Copy link
Author

I can create a PR if this looks something interesting to have in the package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant