Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does BuildContext do in Flutter?

Tags:

flutter

What does BuildContext do, and what information do we get out of it?

https://docs.flutter.io/flutter/widgets/BuildContext-class.html is just not clear.

https://flutter.io/widgets-intro/#basic-widgets on the 9th instance of the term BuildContext there is an example, but it's not clear how it is being used. It's part of a much larger set of code that loses me, and so I am having a hard time understanding just what BuildContext is.

Can someone explain this in simple/very basic terms?

like image 536
richalot Avatar asked Mar 04 '18 20:03

richalot


People also ask

Is every widget have its own BuildContext?

The BuildContext is used to locate a particular widget in a widget tree and every widget has its own BuidContext i.e. a particular BuildContext is associated with only one widget.

What is meant by context in Flutter?

– Context is a link to the location of a widget in the tree structure of widgets. – Context can belong to only one widget. – If a widget has child widgets, then the context of the parent widget becomes the parent context for the contexts of direct child elements.

How build () method works how it rebuild itself?

The build method is called any time you call setState , your widget's dependencies update, or any of the parent widgets are rebuilt (when setState is called inside of those). Your widget will depend on any InheritedWidget you use, e.g. Theme. of(context) , MediaQuery. of(context) etc.

What's the job of the build () method?

build method Null safetyDescribes the part of the user interface represented by this widget. The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes).


1 Answers

BuildContext is, like it's name is implying, the context in which a specific widget is built.

If you've ever done some React before, that context is kind of similar to React's context (but much smoother to use) ; with a few bonuses.

Generally speaking, there are 2 use cases for context :

  • Interact with your parents (get/post data mostly)
  • Once rendered on screen, get your screen size and position

The second point is kinda rare. On the other hand, the first point is used nearly everywhere.

For example, when you want to push a new route, you'll do Navigator.of(context).pushNamed('myRoute').

Notice the context here. It'll be used to get the closest instance of NavigatorState widget above in the tree. Then call the method pushNamed on that instance.


Cool, but when do I want to use it ?

BuildContext is really useful when you want to pass data downward without having to manually assign it to every widgets' configurations for example ; you'll want to access them everywhere. But you don't want to pass it on every single constructor.

You could potentially make a global or a singleton ; but then when confs change your widgets won't automatically rebuild.

In this case, you use InheritedWidget. With it you could potentially write the following :

class Configuration extends InheritedWidget {   final String myConf;    const Configuration({this.myConf, Widget child}): super(child: child);    @override   bool updateShouldNotify(Configuration oldWidget) {     return myConf != oldWidget.myConf;   } } 

And then, use it this way :

void main() {   runApp(     new Configuration(       myConf: "Hello world",       child: new MaterialApp(         // usual stuff here       ),     ),   ); } 

Thanks to that, now everywhere inside your app, you can access these configs using the BuildContext. By doing

final configuration = context.inheritFromWidgetOfExactType(Configuration); 

And even cooler is that all widgets who call inheritFromWidgetOfExactType(Configuration) will automatically rebuild when the configurations change.

Awesome right ?

like image 57
Rémi Rousselet Avatar answered Oct 02 '22 16:10

Rémi Rousselet