Flutter BuildContext

| Page Views: 344

Flutter BuildContext Tutorial

In this tutorial we want to look at and explain an important class we will be using in each and every one of flutter applications, the BuildContext.

What is BuildContext?

BuildContext is an abstract class acting as a handle to the location of a widget in the widget tree.

abstract class BuildContext

It's defined in the flutter package.

It's role as an abstract class is to hold a set of methods. These methods can be used from StatelessWidget.build methods and from methods on State objects.

Normally when you are using widget builder functions like the build() method of the StatelessWidget class.

For instance here's a BuildContext object being passed to the build() method of a StatelessWidget subclass:

  Widget build(BuildContext context) {
    return MaterialApp()

Most components in Flutter are widgets. And these widgets, each of them, has it's own BuildContext. This BuildContext object will be used as the parent of the widget that is returned by the StatelessWidget.build() or the State.build() method.

  Widget build(BuildContext context) {
    // here, Scaffold.of(context) returns null
    return new Scaffold(
      appBar: new AppBar(title: new Text('Demo')),
      body: new Builder(
        builder: (BuildContext context) {
          return new FlatButton(
            child: new Text('BUTTON'),
            onPressed: () {
              // here, Scaffold.of(context) returns the locally created Scaffold
              Scaffold.of(context).showSnackBar(new SnackBar(
                content: new Text('Hello.')

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post