Play Flutter Quiz. Click here .

AppBar Widget

This Tutorials is posted by niebin312@gmail.com at 04-03-2019 05:11:49




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

Before start

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

import "package:flutter/material.dart";
import 'package:flutter_widgets/const/_const.dart';
class AppBarPage extends StatefulWidget {
_AppBarState createState() => _AppBarState();
}
class _AppBarState extends State<AppBarPage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(PageName.APP_BAR),
),
body: Text("body"));
}
}

We have used the AppBar in our code above, just a simple one. It will show a app bar with a title. It is easy to use. It usually use 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 use the AppBar above, So let's look at its constructor.

AppBar({
Key key,
this.leading,
this.automaticallyImplyLeading = true,
this.title,
this.actions,
this.flexibleSpace,
this.bottom,
this.elevation = 4.0,
this.backgroundColor,
this.brightness,
this.iconTheme,
this.textTheme,
this.primary = true,
this.centerTitle,
this.titleSpacing = NavigationToolbar.kMiddleSpacing,
this.toolbarOpacity = 1.0,
this.bottomOpacity = 1.0,
})

So let's look at the 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.

AppBar _autoLeadBar() => AppBar(
title: Text(PageName.APP_BAR),
automaticallyImplyLeading: false,
);

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 the space is strict. I often use the InkWell with a Icon as below.

AppBar _leadingBar() => AppBar(
title: Text(PageName.APP_BAR),
leading: InkWell(
child: Icon(Icons.menu),
onTap: () {
print("click menu");
},
),
);

actions

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

AppBar _actionBar() => AppBar(
title: Text(PageName.APP_BAR),
actions: <Widget>[
InkWell(
child: Icon(Icons.search),
onTap: () {
print("click search");
},
),
SizedBox(width: 10),
InkWell(
child: Icon(Icons.more_vert),
onTap: () {
print("click more");
},
),
SizedBox(width: 20)
],
);

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.

final PreferredSizeWidget bottom;

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

Widget _bottomBar() => AppBar(
title: Text(PageName.APP_BAR),
bottom: PreferredSize(
child: Container(
alignment: Alignment.center,
color: RED,
constraints: BoxConstraints.expand(height: 50),
child: Text(
"bottom",
style: TextStyle(fontSize: 30),
),
),
preferredSize: Size(50, 50),
),
);

It will show as follows.

elevation

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

Widget _elevationBar() => AppBar(
title: Text(PageName.APP_BAR),
elevation: 10,
);

This will show as 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.

Widget _backgroundBar() => AppBar(
title: Text(PageName.APP_BAR),
backgroundColor: PURPLE,
);

brightness

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

Widget _brightBar() => AppBar(
title: Text(PageName.APP_BAR),
brightness: Brightness.dark,
);

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.

IconThemeData({this.color, double opacity, this.size})

Let's use it.

Widget _iconThemeBar() => AppBar(
title: Text(PageName.APP_BAR),
iconTheme: IconThemeData(color: Colors.white, size: 20, opacity: 0.9),
);

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.

Widget _textThemeBar() => AppBar(
title: Text(PageName.APP_BAR),
textTheme: TextTheme(title: TextStyle(color: Colors.white,fontSize: 30)),
);

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.

Widget _primaryBar() => AppBar(
title: Text(PageName.APP_BAR),
primary: false,
);

centerTitle

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

Widget _centerBar() => AppBar(
title: Text(PageName.APP_BAR),
centerTitle: true,
);

titleSpacing

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

Widget _spaceBar() => AppBar(
title: Text(PageName.APP_BAR),
titleSpacing: 20,
);

toolbarOpacity

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

Widget _toolBar() =>
AppBar(
title: Text(PageName.APP_BAR),
toolbarOpacity: 0.5,
);

bottomOpacity

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

Widget _bottomOpBar() => AppBar(
title: Text(PageName.APP_BAR),
bottomOpacity: 0.5,
bottom: PreferredSize(
child: Container(
alignment: Alignment.center,
color: RED,
constraints: BoxConstraints.expand(height: 50),
child: Text(
"bottom with bottomOpacity = 0.5 ",
style: TextStyle(fontSize: 20),
),
),
preferredSize: Size(50, 50),
),
);

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.

Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
// title: Text("hello"),
expandedHeight: 100,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(PageName.APP_BAR),
centerTitle: false,
collapseMode: CollapseMode.parallax,
background: Container(
color: RED,
constraints: BoxConstraints.expand(height: 100),
child: Image.asset(
PageImage.FLUTTER_OPEN,
fit: BoxFit.cover,
),
),
),
),
SliverList(
delegate: SliverChildListDelegate([
SizedBox(
height: 1000,
),
]),
)
],
),
);

We should replace all code of the Scaffold with these code. 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.

Whole code in GitHub,star to support.



Tags:




0 Comments
Login to comment.

Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.







© 2018 - Fluttercentral | Email to me - Seven.srikanth@gmail.com

Terms | Privacy