what is the Widget get _appBar meaning in flutter

Viewed 268

I am reading a flutter code like this:

  Widget get _appBar {
    return Column(
      children: <Widget>[
        Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [Color(0x66000000), Colors.transparent],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
            ),
          ),
          child: Container(
            padding: EdgeInsets.fromLTRB(14, 20, 0, 0),
            height: 86.0,
            decoration: BoxDecoration(
                color:
                    Color.fromARGB((appBarAlpha * 255).toInt(), 255, 255, 255),
                boxShadow: [
                  BoxShadow(
                    color: appBarAlpha == 1.0
                        ? Colors.black12
                        : Colors.transparent,
                    offset: Offset(2, 3),
                    blurRadius: 6,
                    spreadRadius: 0.6,
                  ),
                ]),
            child: SearchBar(
              searchBarType: appBarAlpha > 0.2
                  ? SearchBarType.homeLight
                  : SearchBarType.home,
              inputBoxClick: _jumpToSearch,
              defaultText: SEARCH_BAR_DEFAULT_TEXT,
              leftButtonClick: () {},
              speakClick: _jumpToSpeak,
              rightButtonClick: _jumpToUser,
            ),
          ),
        ),
        Container(
            height: appBarAlpha > 0.2 ? 0.5 : 0,
            decoration: BoxDecoration(
                boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 0.5)]))
      ],
    );
  }

what is the meaning of Widget get _appBar? is it get a appBar control? why not return a AppBar directly?

2 Answers

It is a "getter":

Getters and setters are special methods that provide read and write access to an object’s properties. Recall that each instance variable has an implicit getter, plus a setter if appropriate. You can create additional properties by implementing getters and setters, using the get and set keywords

(Dart Documentation)

So Widget get _appBar is a getter for a property called _appBar that is of type Widget. You could have just made it a function: Widget _appBar().

I guess your code uses this app bar in multiple places and so has found a way to put the code in one place to call it from all the other places instead of duplicating all of it.

The get keyword is used in dart code to provide read access to an object's property. Often this is done with methods (like your case). Check this out for more on getters and setters. In Widget get _appBar we see the Widget return type, the get keyword (indicating it's a getter) and the name of the field/getter - _appBar.

In this example, a field getter _appBar is defined as an interface for returning a widget. Getters can sort of disguise a method as a field. so the way you'll access this field looks like this:

var someObject = SomeObject();

// here we're accessing the field and assigning it to a different object
var widget = someObject._appBar;

Now there is some interesting things going on here with the name of the getter. It starts with an underscore (_), indicating it's a private member. This could be intentional for only the library/package/class's use internally - rather than accessing it in another location.

Likely that the code isn't returning an AppBar directly is because this is a custom implementation for an app bar. You don't need to provide an AppBar widget to a Scaffold's appBar parameter, you only need to provide a PreferredSizeWidget to the parameter. Check out the docs on it here (Scaffold), here (appBar property), and here(PreferredSizeWidget).

Related