Flutter Widgets 07 | AppBar

NieBin
flutteropen
Published in
5 min readJan 21, 2019

In this tutorial, you will learn more detail about the AppBar in the flutter.

Before start

In every tutorial, we need to create a page to contain our code to show. So let’s look at our code.

We have used the AppBar in our code above, just a simple one. It will show an app bar with a title. It is easy to use. It usually uses with the appBar parameter of the Scaffold . But in this tutorial, I will try to use it as a common widget to describe its parameter. Hope you do not misunderstand my style.

Constructor

We have used the AppBar above, So let's look at its constructor.

So let’s look at its parameters as follows.

automaticallyImplyLeading

If you don’t want the left-arrow on the app bar. you can use this parameter to hide it.

But you should know, if you the leading is not null, then this parameter hasn’t effect.

leading

This parameter controls the space as the left-arrow. So if you want to use some special function here. You can use this parameter, it is just a widget, but space is strict. I often use the InkWell with a Icon as below.

actions

If you want to add some widget in the right, the actions parameter you should know to use.

We have added the search and more icon at the right of the app bar.

bottom

When we look at the definition of the bottom parameter. we can see it is PreferredSizeWidget.

It is an abstract class. So we need to use its subclass. The PreferredSize is a good choice to use. The child is a widget, the preferredSize is the Size.

It will show as follows.

elevation

This parameter controls the shadow of the AppBar. It is similar with RaiseButton I have told before.

This will show as to right in the picture below. The left one is elevation = 4.

backgroundColor

If you want to change the color of the AppBar, you can use the backgroundColor as follows.

brightness

The parameter will change the text, icon color of the status bar.

iconTheme

When we look at the parameter carefully, we can see it is IconThemeData, its constructor is below. We have used the color, size several times, so I do not explain it. The opacity controls the transparency of the Icon. The value is from 0 to 1.

Let’s use it.

The left-arrow will change to white.

But you should know, if you change the size, it doesn’t have an effect.

textTheme

This will control the title of the Text.

It will change the color of the title to white and the fontSize to 30.

primary

The default value of this parameter is true. When we set to false, the icon and title will move up.

centerTitle

This parameter is bool, the default value is false. It controls the title whether or not show in the center of the AppBar.

titleSpacing

This parameter will change the distance of the title from left. So the direction is horizontal and the default value is zero.

toolbarOpacity

It will change the transparency of the elements of the AppBar.

bottomOpacity

We have used the bottom above, so the bottomOpacity will control the transparency of the part of the bottom.

flexibleSpace

I put this parameter in the last because it is a bit difficult to use. But if you use it in the AppBar, it is not so useful. So I will use a SliverAppBar to replace the AppBar to describe this parameter.

We should replace all code of the Scaffold with these codes. It will show as below.

default

When you scroll to top, It will show as follows.

That’s nice!

Conclusion

We have learned how to use the AppBar in the flutter. It is so common and important to us. So just keep going, we can learn very well about the widgets in the flutter.

Thanks for your reading.

The End.

The whole code in Github: https://github.com/FlutterOpen/ebook

--

--