201

How can I simply set the height of the AppBar in Flutter?

The title of the bar should be staying centered vertically (in that AppBar).

Buğra Ekuklu
  • 3,049
  • 2
  • 17
  • 28
  • 2
    @Mans It's about 'getting' the height of the `AppBar`, not 'setting' it. – Buğra Ekuklu Jun 28 '18 at 19:57
  • 1
    Did you took a look at [this](https://docs.flutter.io/flutter/material/AppBar-class.html)? You could also create your own widget as an AppBar and set it's height. – Bostrot Jun 28 '18 at 21:26
  • @Leviathlon my bad. check my answer for (hopefully) some better help – Mans Jun 29 '18 at 15:15

16 Answers16

318

You can use PreferredSize:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Example',
      home: Scaffold(
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(50.0), // here the desired height
          child: AppBar(
            // ...
          )
        ),
        body: // ...
      )
    );
  }
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Cinn
  • 4,281
  • 2
  • 20
  • 32
73

Use toolbarHeight:

enter image description here


There's no longer a need to use PreferredSize. Use toolbarHeight with flexibleSpace.

AppBar(
  toolbarHeight: 120, // Set this height
  flexibleSpace: Container(
    color: Colors.orange,
    child: Column(
      children: [
        Text('1'),
        Text('2'),
        Text('3'),
        Text('4'),
      ],
    ),
  ),
)
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
54

You can use PreferredSize and flexibleSpace for it:

appBar: PreferredSize(
  preferredSize: Size.fromHeight(100.0),
  child: AppBar(
    automaticallyImplyLeading: false, // hides leading widget
    flexibleSpace: SomeWidget(),
  )
),

This way you can keep the elevation of AppBar for keeping its shadow visible and have custom height, which is what I was just looking for. You do have to set the spacing in SomeWidget, though.

footurist
  • 1,509
  • 2
  • 16
  • 23
32

The easiest way is to use toolbarHeight property in your AppBar Example :

AppBar(
   title: Text('Flutter is great'),
   toolbarHeight: 100,
  ),

You can add flexibleSpace property in your appBar for more flexibility

Output:

enter image description here

For more controls , Use the PreferedSize widget to create your own appBar

Example :

appBar: PreferredSize(
     preferredSize: Size(100, 80), //width and height 
          // The size the AppBar would prefer if there were no other constraints.
     child: SafeArea(
       child: Container(
         height: 100,
         color: Colors.red,
         child: Center(child: Text('Fluter is great')),
       ),
     ),
),

Don't forget to use a SafeArea widget if you don't have a safeArea

Output :

enter image description here

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Kab Agouda
  • 6,309
  • 38
  • 32
  • I already provided `toolBarHeight` solution, second "For more controls , Use the PreferedSize " -- Not necessarily, `toolbarHeight` does the job well. – CopsOnRoad Jun 01 '21 at 05:54
16

At the time of writing this, I was not aware of PreferredSize. Cinn's answer is better to achieve this.

You can create your own custom widget with a custom height:

import "package:flutter/material.dart";

class Page extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Column(children : <Widget>[new CustomAppBar("Custom App Bar"), new Container()],);
  }
}


class CustomAppBar extends StatelessWidget {

  final String title;
  final double barHeight = 50.0; // change this for different heights 

  CustomAppBar(this.title);

  @override
  Widget build(BuildContext context) {
    final double statusbarHeight = MediaQuery
        .of(context)
        .padding
        .top;

    return new Container(
      padding: new EdgeInsets.only(top: statusbarHeight),
      height: statusbarHeight + barHeight,
      child: new Center(
        child: new Text(
          title,
          style: new TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }
}
Mans
  • 2,953
  • 2
  • 23
  • 32
13

In addition to @Cinn's answer, you can define a class like this

class MyAppBar extends AppBar with PreferredSizeWidget {
  @override
  get preferredSize => Size.fromHeight(50);

  MyAppBar({Key key, Widget title}) : super(
    key: key,
    title: title,
    // maybe other AppBar properties
  );
}

or this way

class MyAppBar extends PreferredSize {
  MyAppBar({Key key, Widget title}) : super(
    key: key,
    preferredSize: Size.fromHeight(50),
    child: AppBar(
      title: title,
      // maybe other AppBar properties
    ),
  );
}

and then use it instead of standard one

Pavel
  • 5,374
  • 4
  • 30
  • 55
12

You can simply use toolbarHeight, as follows:

        appBar: AppBar(
        toolbarHeight: 70.0, // add this line
        centerTitle: true,   // add this line
        title: Text('your title'),
        ),

but if you have any actions the code above doesn't work as you want you can use this code

AppBar(
    centerTitle: true,
    title: Padding(
      padding: const EdgeInsets.all(16.0),
      child: Stack(
        alignment: Alignment.center,
        children: [
          Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Chats', style: TextStyle(color:Colors.black),),
              Icon(Icons.add, color: Colors.black,),
            ],
          ),
          Align(
            alignment: Alignment.centerRight,
            child: Icon(Icons.add, color: Colors.black,),
          ),
        ],
      ),
    ),
  )
Omar
  • 456
  • 7
  • 7
5

Cinn's answer is great, but there's one thing wrong with it.

The PreferredSize widget will start immediately at the top of the screen, without accounting for the status bar, so some of its height will be shadowed by the status bar's height. This also accounts for the side notches.

The solution: Wrap the preferredSize's child with a SafeArea

appBar: PreferredSize(
  //Here is the preferred height.
  preferredSize: Size.fromHeight(50.0),
  child: SafeArea(
    child: AppBar(
      flexibleSpace: ...
    ),
  ),
),

If you don't wanna use the flexibleSpace property, then there's no need for all that, because the other properties of the AppBar will account for the status bar automatically.

Tayan
  • 1,707
  • 13
  • 18
  • but `SafeArea` is to reduce status bar height, yet you added it again with `MediaQuery.of(context).padding.top`? I think SafeArea is not needed here. – Ryde Jul 04 '20 at 23:11
  • @Ryde The `SafeArea` is important so that the app bar doesn't overlap with the status bar, but the `MediaQuery.of(context).padding.top` is not needed actually. I've edited the answer, thanks. – Tayan Jul 05 '20 at 21:38
5

simply use toolbar height ...

AppBar(
 title: Text("NASHIR'S APP"),
 toolbarHeight: 100,
),
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
3

You can use the toolbarHeight property of Appbar, it does exactly what you want.

2
class AppBarWidget extends StatelessWidget with PreferredSizeWidget {
  final String title;
  const AppBarWidget({Key? key, required this.title}) : super(key: key);

  @override`enter code here`
  Widget build(BuildContext context) {
   return AppBar(

  title: Text(title),
  centerTitle: true,
  elevation: 0,
  actions: [
    Padding(
      padding: const EdgeInsets.only(right: 10),
      child: IconButton(
        icon: const FaIcon(FontAwesomeIcons.language),
        onPressed: () {},
      ),
    ),
    
  ],
);
  }

 @override

  Size get preferredSize => const Size.fromHeight(40);// change
 }
1

You can use PreferredSize, by this use can set multiple children widget inside their

 appBar: PreferredSize(
    preferredSize: Size(MediaQuery.of(context).size.width, 75),
    child: Column(children: [
      AppBar(
        centerTitle: true,
        toolbarHeight: 74,
        backgroundColor: Colors.white,
        elevation: 0,
        title: Column(
          children: [
            Text(
              viewModel.headingText,
              style: sfDisplay16500Text,
            ),
            SizedBox(
              height: 8.0,
            ),
            Text(
              viewModel.url.toString(),
              style: sfDisplay10400LightBlackText,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            )
          ],
        ),
      ),
    ]),
  ),

or just directly use toolbarHeight property for only increase hight of appBar.

appBar: AppBar(
          title: Text('AppBar Texr'),
          toolbarHeight: 200.0, // double
        ), 
Shirsh Shukla
  • 5,491
  • 3
  • 31
  • 44
1

toolbarHeight Property

AppBar(
   toolbarHeight: 120,
),

Source: Appbar Size Flutter - AmmarJaved

0

Extend AppBar class and override preferredSize

class AppBarCustom extends AppBar {
  @override
  Size get preferredSize => Size.fromHeight(100);
}

then use it as you would use AppBar class

class MyHomePage extends StatelessWidget {
 @override
   Widget build(BuildContext context) {
     return MaterialApp(
       home: Scaffold(
       appBar: AppBarCustom(),
       body:
       ),
     );
   }
 }
0

This is simplest and easiest way to change appbar height without changing original theme.


class AppBarSectionView extends StatefulWidget implements PreferredSizeWidget {
 const AppBarSectionView({Key? key}) : super(key: key);

 @override
 _AppBarSectionViewState createState() => _AppBarSectionViewState();

 @override
 Size get preferredSize => const Size.fromHeight(kToolbarHeight + 20);
}

class _AppBarSectionViewState extends State<AppBarSectionView> {
 @override
 Widget build(BuildContext context) {
   return AppBar(
     toolbarHeight: widget.preferredSize.height ,
     backgroundColor: Colors.red,
     leading: const Icon(Icons.arrow_back_ios_rounded),
     title: const Text("This Is Title"),
   );
 }
}
Wai Han Ko
  • 760
  • 5
  • 16
-14

If you are in Visual Code, Ctrl + Click on AppBar function.

Widget demoPage() {
  AppBar appBar = AppBar(
    title: Text('Demo'),
  );
  return Scaffold(
    appBar: appBar,
    body: /*
    page body
    */,
  );
}

And edit this piece.

app_bar.dart will open and you can find 
    preferredSize = new Size.fromHeight(kToolbarHeight + (bottom?.preferredSize?.height ?? 0.0)),

Difference of height!

sample image

sample image

Avery
  • 2,270
  • 4
  • 33
  • 35
goops17
  • 1,152
  • 10
  • 18