is flutter SliverAppbar height same for every device? - flutter

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;

Related

How to place banner ad on top of App Bar in 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'),
),

Is there a bootstrap container equivalent in Flutter?

I'm trying to build an app in flutter and I'm using
Scaffold(
appBar: AppBar(
title: Text("Login"),
),
body: Text("Hello there")
);
the issues i'm facing is that the body text doesn't have the same padding as the appbar one has.
So how can i find the default padding value to use it so i can have aligned text or widget with the same padding?
Thanks in advance.
Well i don't think app bar have default padding values which you can get but you can probably get its height value by using its property. Below i have written a code which extracts appbar height. And using that height i made a container and added text as its child and aligned it to leftCenter. Then applied 16 padding on left and right. So the output is same as the appBar.
Widget build(BuildContext context) {
AppBar appbar = AppBar(
title: Text('Login'),
);
double appBarHeight = appbar.preferredSize.height;
return Scaffold(
appBar: appbar,
body: Container(
padding: EdgeInsets.only(left: 16, right: 16),
color: Colors.blue[100],
alignment: Alignment.centerLeft,
height: appBarHeight,
child: Text(
'Login',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
),
),
);
you can always use the container widget
A convenience widget that combines common painting, positioning, and sizing widgets.
A container first surrounds the child with padding (inflated by any borders present in the decoration) and then applies additional constraints to the padded extent (incorporating the width and height as constraints, if either is non-null). The container is then surrounded by additional empty space described from the margin.
Container(
padding:EdgeInsets.all(8)
child:Text("Hello there"))
From this you can play with padding ,margin.
also there is a padding widget
A widget that insets its child by the given padding.

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"),
),
),
),
],
);
}

Screen compatible to all devices in flutter

How to Adjust the screen to all android devices screens in flutter. I have checked in stackoverflow but unfortunately i haven't got any satisfying answer.
Basically, Flutter automatically sets the height and width depending on the device sizes wherever possible. Example - if you have use a list view having 100 items then some devices may show 5 items at a time and some may show 6 items. It is automatically done by Flutter.
The problem comes only when you specify an absolute value for height and/or width. Let's say you've a widget and you specify width as 450. Now, it may fit on the bigger screens but on the small screen (e.g. width 400 points)then you'll see pixel overflow error in the UI.
Now, to solve this problem, you can use MediaQuery.of(context).size.height/width as suggested by LGM.
I think you have some questions about responsive layouts, so i will give you some examples with MediaQuery:
With MediaQuery you can get the device screen width:
double width = MediaQuery.of(context).size.width;
The height:
double height = MediaQuery.of(context).size.height;
as well as the orientation:
Orientation orientation = MediaQuery.of(context).orientation;
and also various information about the device, and then, you can base the layout on orientation, size etc.
Here's an example:
Container(
width: MediaQuery.of(context).orientation == Orientation.portrait ?
MediaQuery.of(context).size.width / 1.3 : MediaQuery.of(context).size.width / 2.1,
child: RaisedButton(
elevation: 0.0,
color: Colors.green,
child: Text("CONTINUE", style: TextStyle(color: Colors.white),),
onPressed: (){
//code of onPressed
);
}
),
),
You can use too Align, to define where the widget should be, Positioned, the properties of Column, Center etc.
One more example, using FittedBox to deal with texts:
#override
Widget build(BuildContext context) {
return Material(
child: SafeArea(
child: Scaffold(
body: ListView(
children: <Widget>[
FittedBox(child: Text("Test"),),
FittedBox(child: Text("Flutter Flutter Flutter"),)
],
),
),
),
);
}
The result:
Portrait:
Landscape:

Permanent (non-hiding) drawer

I would like to present a different view depending on MediaQuery output. Hiding/sliding drawer on smaller devices like phones and a permanent/docked drawer on larger devices like tablets.
I see that drawer is part of the Scaffold but the existing infrastructure does not allow for a permanent drawer, or at least I don't see it.
What you should do is create a different layout for tablets since the drawer will opaque de view when is it open. So create a new widget for the drawer (it will be used in both screens) then check the width of it and depending of the size, put it as the drawer parameter or inside the body, within a stack
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(backgroundColor: Colors.orange),
drawer: size.width < 500 ? Drawer() : null,
body: Stack(
children: <Widget>[
if (size.width >= 500) Drawer(),
],
),
);
}