Get size of screen without bottom navigation bar height - flutter

So the basic scaffold in flutter has appbar, body and bottom bar.
in my bottom navigation bar I have 3 items:
Page 1 (Editor Page)
page 2 (Stickers)
page 3 (Text Customization Page)
Each of these pages have their own scaffolds.
Editor page has a widget called LayoutCanvas which is something like:
Widget build(BuildContext context) {
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
CanvasModel.of(context).init(canvasKey!);
});
return Container(
Inside init:
void init(GlobalKey canvasKey) {
_canvasKey = canvasKey;
final context = _canvasKey.currentContext;
if (context == null) return;
final bounds = context.findRenderObject()!.paintBounds;
final screenHeight = MediaQuery.of(context).size.height;
final screenWidth = MediaQuery.of(context).size.width;
var canvasHeight = screenHeight * 0.7;
var canvasWidth = layout.aspectRatio * canvasHeight;
Issue:
As you can see I'm taking 0.7 or 70% of the screen height as the canvas height and calculating the rest from there. But 70% is quite small for something like an editor canvas. But if I make it 80% some of the editor is getting cut by the bottom navigation bar.
Maybe the screenHeight is not taking the bottom navigation bar height into consideration?
I either need to make it dynamic or I need to subtract the bottom nav height from screenHeight.
aspectRatio:
double get aspectRatio => isInitialized ? height / _layout!.height : 0;
How can I solve this issue?

To get the maximum available height you need to distract some values from MediaQuery.of(context).size.height. Assuming that you have a top AppBar and a BottomNavigationBar, you can use this formula:
MediaQuery.of(context).size.height - // total height
kToolbarHeight - // top AppBar height
MediaQuery.of(context).padding.top - // top padding
kBottomNavigationBarHeight // BottomNavigationBar height

you can use MediaQuery.of(context).size.width and
MediaQuery.of(context).size.height

Related

Dynamically position widget in stack Flutter

I am trying to create a view that consist of a background image with widgets positioned dynamically in certain places on this image, each widget will have an x and y that needs to be calculated for different screens and for different image sizes.
what i did so far was showing the image in a stack and made a list of widgets where each widget type is being checked then create a flutter widget that corresponds with it adding it to this list and displaying it over the image in the stack, but i am looking for a cleaner way to create this behavior, also when rotating the screen the widgets are not being placed in a correct position,
Thank you for your time.
You can do something like:
Stack(
children: [
Image.asset('YOUR IMAGE HERE', fit: BoxFit.cover), // this is your bg image,
..List.generate(imagesCollection.length, (index) {
// you could encapsulate all this logic in a separate widget
var img = imagesCollection[index];
var r = Random();
// make sure to stay within the bounds of your screen
var screenWidth = MediaQuery.of(context).size.width;
var screenHeight = MediaQuery.of(context).size.height;
var randomX = r.nextInt(screenWidth);
var randomY = r.nextInt(screenHeight);
// before you position the image, check that is within the bounds
// check if the randomX - IMAGE_WITH is less than the screen width, same for the height
return Positioned(
top: randomY,
left: randomX,
child: Image.asset(img, width: IMAGE_WIDTH, height: IMAGE_HEIGHT)
);
}
]
)
Something around those lines.

Flutter web show popup menu on hover and remove on mouse exit

Is there a way to achieve popup menu on header which expands on hover and closes on mouse exit
You can use the MouseRegion widget to determine the hover changes. Popup in the sense, could you explain what exactly you want to achieve? Some would be possible some wouldn't be. I hope you just need to expand the menu button on hover, which you can use an AnimatedContainer for the menu, warp it with MouseRegion widget, declare a variable that says the width, and height of AnimatedContainer widget. MouseRegion has onEnter and onExit functions, onEnter is when the container has hovered and onExit is when the user stops hovering the container. During onEnter you can increase the width and height and during onExit, you can set it to default.
double _animatedContainerHeight = 30; //Default height
double _animatedContainerWidth = 30; //Default width
MouseRegion(
onEnter: (value) {
setState(() {
_animatedContainerHeight = 50; //OnHover height
_animatedContainerWidth = 50; //OnHover width
});
},
onExit: (value) { setState(() {
_animatedContainerHeight = 30; //Return back to normal height
_animatedContainerWidth = 30; //Return back to normal width
});
},
cursor: SystemMouseCursors.click, //Cursor type on hover
child: AnimatedContainer(
duration: Duration(milliseconds: 200),
height: _animatedContainerHeight, //Animation height control
width: _animatedContainerWidth, //Animation width control
),
)

How to change design according to screen size using NSLayoutConstraint in swift

I have designed a screen using Storyboard constraints for iPhone XR, but that design become big in iPhone7, so I want to change the height and width of my ContainerView using NSLayoutConstraints.
I have taken:
#IBOutlet weak var containerViewheight: NSLayoutConstraint!
This ContainerView actual height in iPhoneXR is 300, so I want to change it in iPhone7
For example:
if (iphone7) {
self.containerViewheight.constant = 240
}
if (iphone SE) {
self.containerViewheight.constant = 280
}
Unfortunately I have very little knowledge in size classes.
You can always ask the UIScreen class method main for the current concrete size, it returns the concrete size of the current device.
let deviceSize = UIScreen.main.bounds.size
But you probably know, that height and width change depending on the orientation the device has (landscape or portrait).
Maybe this extension could help:
import UIKit
extension UIScreen {
/// Retrieve the (small) width from portrait mode
static var portraitWidth : CGFloat { return min(UIScreen.main.bounds.width, UIScreen.main.bounds.size.height) }
/// Retrieve the (big) height from portrait mode
static var portraitHeight : CGFloat { return max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height) }
/// Retrieve the (big) width from landscape mode
static var landscapeWidth : CGFloat { return max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height) }
/// Retrieve the (small) height from landscape mode
static var landscapeHeight : CGFloat { return min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height) }
}
You can manipulate by knowing what ratio you want your container height to be of the screen height.
For eg: if UIScreen.main.bounds.height gives you 1000. And your appropriate container height here is 400. Then the required ratio of height of container to height of screen is 400/1000 or 2/5.
Now, what you can do is write
containerViewheight.constant = (2/5)*UIScreen.main.bounds.height

change view elements size according to view size changing

I have a view with 10 buttons , i want the buttons size to change when the view size changes .
or is there a way to set the buttons as children of the view so all the changes that apply at view will be applied to all the buttons
We can use autolayout to set the height for each of the 10 buttons to have a equal height with superView but with a multiplier of 1:10
Below is a sample screenshot showing the proportional height
I did it by embedding the buttons in Stack and multiplying the stack size with a percentage of screen size , first i get the screen size .
let screenSize : CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
buttonsStack.frame = CGRect(x: screenWidth * 0.75 , y: screenHeight * 0.3 , width: screenWidth * 0.27 , height: screenHeight * 0.67)
X , Y are the position of the stack according to screen size .
The only way I know to change the size of an element and keep it relative to the size of the parent container is to use the (%) size.
// index.html
<body>
<div>
<a class="my-button" href="#">click me</a>
<a class="my-button" href="#">click me</a>
<a class="my-button" href="#">click me</a>
<a class="my-button" href="#">click me</a>
// .... more buttons
</div>
</body>
then
// style.css
.my-button {
width: 5%;
}

How to create a fullscreen size popup in Windows phone

Fullscreen I mean the popup usercontrol covers the whole cellphone screen, maximize. can this be done?
thx!
You can get the current width and height by Content.ActualHeight/Width property
here's a code snippet
public void showPopUp()
{
//get heigth and width
double height=Application.Current.Host.Content.ActualHeight;
double width = Application.Current.Host.Content.ActualWidth;
//child content
StackPanel stk = new StackPanel();
stk.Height = height; //set height
stk.Width = width; //set width
stk.Background = new SolidColorBrush(Colors.Red);
TextBlock txtblock = new TextBlock() { FontSize=40, Text="HELLO WORLD", TextWrapping=TextWrapping.Wrap};
stk.Children.Add(txtblock);
Popup _popup = new Popup();
_popup.Child = stk; //set child content
this.LayoutRoot.Children.Add(_popup);
_popup.IsOpen = true;
}
and then you get this result ;)
For Windows Phone 8.1 you can get the current Application width and height by Window.Current.Bounds
double height = Window.Current.Bounds.Height;
double width = Window.Current.Bounds.Width;