How to place banner ad on top of App Bar in Flutter? - flutter

How can I place a banner ad on the top of the top App Bar in Flutter? I have the banner ad at the bottom of the bottom app bar as shown below. I want to move it to the top, so that it becomes the first widget of the screen.

Try preferredSizeAppbar widget and increase the app bar height and use you banner.

You can use PreferredSize or flexibleSpace,
PreferredSize
appBar: PreferredSize(
preferredSize: Size.fromHeight(120), // change height of ads as you like
child: Container(
child: // here is ads
)
)
or
flexibleSpace
appBar: AppBar(
toolbarHeight: 120, // Set this appbar height
flexibleSpace: Container(
child: Text("this is add"), // make container of your ads
),
title: Text('Material App Bar'),
),

Related

How i can hide and show app bar with animation flutter

Here is my code
return Scaffold(
body: SafeArea(
appBar:getIsShowAppBar(layoutProvider.currentIndex,
realEstatesProvider) ? AppBar (),
child: IndexedStack(
children: layoutProvider.screens,
index: layoutProvider.currentIndex),
),
bottomNavigationBar: BottomNavigationBar(), );
If you want to show an app bar and hide it when you want you would want to use the SliverAppBar widget witch allows you to make an app bar of your choice and allows to dissapear it when scrolling and other options with animations. https://api.flutter.dev/flutter/material/SliverAppBar-class.html

is flutter SliverAppbar height same for every device?

I am trying to add a container with certain height to title of SliverAppBar with a certain height. I am trying to figure out how can I determine the height of the sliverAppBar and give height to the container based on that. I don't want to mess up the view with different screen sized devices. Currently height of 40 for the Iphone 12 works like how I want it to be, however I am worried that it may not look same across different devices. So my question is that is app bar height same across all devices? If not how can I dynamically get the height of the appbar ?
SliverAppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.blue,
title: Container(
color: Colors.black,
width: size.width * 0.85,
height: 40,
)),
No app bar size is not same across all devices,
you can get the size of app by doing this
Define your app separately like this
Widget demoPage() {
AppBar appBar = AppBar(
title: Text('Demo'),
);
return Scaffold(
appBar: appBar,
body: /*
page body
*/,
);
}
And then get it's height like this
double height = appBar.preferredSize.height;

Can I increase height of app bar in flutter?

Is there any way of increasing height of app bar according to safe area in flutter cause the text in Center looks weird.
you can use toolbarHeight
AppBar(
toolbarHeight: 100,
title: Text('Main Page')
)
Yes you can do this like using below code. Put PreferredSize widget as a parent of AppBar and set a height.
appBar: PreferredSize(
preferredSize: Size.fromHeight(150.0),
child: AppBar(
title: Text('appbar'),
),
),

flutter custom appbar vertical position relative to status bar

I used the example in the following link to create a custom appBar.
The code works, however the appbar is positioned at the top of the screen behind the Android status bar
How can I position the appBar such that it starts rendering under the status bar?
version used:
Flutter 1.26.0-8.0.pre • channel dev
Android R30
There is a widget called SafeArea that set the padding of your widget to avoid the intrusion of the status bar. You can use it as:
// Use the SafeArea here to position the AppBar under the status bar
SafeArea(
child: Scaffold(
appBar: PreferredSize(
preferredSize: Size(double.infinity, 100),
child: Container(
decoration: BoxDecoration(
// ... other lines

Transparent overlapping appbar

I'm trying to find a way to have a transparent appbar that overlaps the page content. I know I can use a SliverAppBar to show the appbar initially, and then have it slide out of view when scrolling, which is good. I can even give it a transparent background color. However, when you scroll to the top of the page, it always makes room for the bar to sit above the page content.
What I want is for the page content to be flush with the top of the page, as if there is no appbar, and then have a transparent appbar slide into view like a SliverAppBar does, so that I just have some action buttons overlapping the top of the page.
How can I pull that off? Is there a way to stack the appbar or change margins so it doesn't take space?
You can just put a scaffold and your "page" in a stack(), make sure the scaffold is the last item in the stack. You can get creative an add animations for the app bars yourself or even use the sliver app bar, just make sure you use the same scroll controller for your app bar and content.
eg:
#override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
// Your content
Container(
color: Colors.pink,
),
Scaffold(
appBar: AppBar(
title: Text("heading"),
bottom: AppBar(
title: Text("footer"),
),
),
),
],
);
}