Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display body under the AppBar of scaffold widget and not below?

Tags:

flutter

I'm using a transparent AppBar and I would like to display body behind the transparent AppBar and not bellow. How to do that ?

like image 702
fvisticot Avatar asked Apr 25 '18 22:04

fvisticot


People also ask

What do you call the widget that is displayed at the bottom of the scaffold where you can navigate?

bottomNavigationBar is used to display a navigation bar at the bottom of the scaffold. It is rendered below the persistentFooterButtons and the body.

How do I add AppBar to scaffolding?

Wrap the AppBar in a MediaQuery widget, and adjust its padding such that the animation is smooth. If the leading widget is omitted, but the AppBar is in a Scaffold with a Drawer, then a button will be inserted to open the drawer.

How do I remove space between AppBar and body flutter?

You just need to wrap the Column(Which is inside SingleChildScrollView) with Container and give its height and width.

What is extendBodyBehindAppBar?

extendBodyBehindAppBar property Null safety bool extendBodyBehindAppBar. If true, and an appBar is specified, then the height of the body is extended to include the height of the app bar and the top of the body is aligned with the top of the app bar. This is useful if the app bar's AppBar.


2 Answers

The parameter extendBodyBehindAppBar extend the body to include the height of the AppBar, if specified. Scaffold from Flutter.

Scaffold(     extendBodyBehindAppBar: true, ) 

Available in Flutter stable 1.12+

like image 144
Sergey Zabelnikov Avatar answered Sep 18 '22 03:09

Sergey Zabelnikov


To place the body under the AppBar and make the AppBar transparent requires a Stack as the body. The Stack must contain the AppBar not the scaffold.

body: Stack(         children: <Widget>[...] ), 

The first item in the stack is at the bottom and subsequent items are above it. If the AppBar is transparent it will appear to be working, but it is not. Making the AppBar green will show you why.

return Scaffold(       body: Stack(         children: <Widget>[           Container(             color: Colors.blue,           ),           AppBar(title: Text('Hello world'),             backgroundColor: Colors.green,           )         ],); 

As you can see the AppBar takes up the entire screen and will consume any touch events.

Full Screen App Bar

To fix this use a Position Widget,

body: Stack(         children: <Widget>[           Container(             color: Colors.blue,           ),           new Positioned(           top: 0.0,           left: 0.0,           right: 0.0,           child: AppBar(title: Text('Hello world'),             backgroundColor: Colors.green,           ),),         ], ) 

And you will get this:

App Bar fixed

Ok now make the AppBar transparent and remove the shadow:

body: Stack(         children: <Widget>[           Container( //My container or any other widget             color: Colors.blue,           ),           new Positioned( //Place it at the top, and not use the entire screen           top: 0.0,           left: 0.0,           right: 0.0,           child: AppBar(title: Text('Hello world'),             backgroundColor: Colors.transparent, //No more green             elevation: 0.0, //Shadow gone           ),),         ], ) 

Transparent App Bar with full screen container below

Hope this helps...

like image 32
Jason Avatar answered Sep 20 '22 03:09

Jason