Home > database >  How to create Class.of(context) type of class
How to create Class.of(context) type of class

Time:06-26

I want to create custom class for getting custom textStyles, and i want this class to be static. But i need to pass context too for it. I've seen flutter has been using Theme.of(context) or Scaffold.of(context) methods, and i want to make same one like CustomTextStyles.of(context).h1. How this class should be declared ?

CodePudding user response:

The .of method must be handcrafted for each class that uses it. It uses the InheritedWidget mechanism to walk up the context tree to find a widget of an appropriate class. See the docs for InheritedWidget.

  • Related