What is the Function and Utility of "context" in "Widget build(Buildcontext)"

Asked

Viewed 134 times

2

I see in many examples that context is used only with parameters for other functions. In other cases to get some property from widget father.

After all, what role and best example illustrates good practices in using the context in Widget build(BuildContext context)?

1 answer

2


The Context is just a reference to identify your widget within the structure tree.

Each Context belongs only to a Widget. We can also understand that if a particular Widget "A" has children, the Context of the "A" widget will be the parent context of your children...

Exemplo

We can access this inheritance through of, as an example:

Algumacoisa.of(context)

So it will be returned to us this "Somemacoisa" of the Widget closest to the tree.

A more practical example is when we use the command MediaQuery.of(context).size, which returns us the dimensions of our device, based on the Widget MaterialApp closest to the tree.

There is a question similar to yours at Stackoverflow in English if you want to take a look.

Source: Widget - State - Context - Inheritedwidget

  • 1

    Lots of good answer, man. Congratulations!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.