How to get the height for the system navigation bar? - dart

I'm trying to get the height of the system navigation bar so that I can calculate the height of other widgets accordingly.
I was able to get the height of the screen. Is there a way to get the usable height?
usable_height = total_height-(status_bar_height +
navigation_bat_height)
I'm using this method to get the total screen size:
double getScreenHeight(BuildContext context) {
return MediaQuery.of(context).size.height;
}

If your Widgets need to know the size available to them then you can wrap them in LayoutBuilder which will have the width and height available to you Widget
Watch official sample here:
https://www.youtube.com/watch?v=IYDVcriKjsw
Some examples:
How can I layout widgets based on the size of the parent?
https://flutterwidgets.io/widget-of-the-week-layoutbuilder.html
But if want to know the AppBar height then AppBar height in Flutter

Related

Dynamic height for custom dialog in iOS

I'm trying to create a custom dialog with a UITableView inside.
What I want to achieve is to have the lowest height possible, meaning it should be only wrapping the tableview.
BUT when the tableview has too many items (meaning its height is bigger than the screen), I want the dialog to have 20 px margin from the screen top and bottom.
So if the tableview has 2 items, the height of the dialog should be for example 20 px. But if the tableview has 200 items, the height of the dialog should take almost the entire screen height and have its content scrollable.Dialog with few items
Dialog with multiple items
Currently if the tableview has multiple items, I can only see some items and the top and bottom of the dialog disappear.
Thanks.
EDIT: I forgot to mention I intend to achieve this using the storyboard only, using constraints and changing priority in content hugging and compression.
GOT IT WORKING!
All I had to do was change the top and bottom constraints from "=" to ">=".
So now it allows the dialog to have a smaller height but not bigger than the screen. No code needed.
Thanks everybody for your help.
My answer will include several parts. They are solving a more general question of how to efficiently work with dialogs.
To create dialog use UIViewControllerTransitioningDelegate, UIPresentationController and UIViewControllerAnimatedTransitioning. It's a long way, but it will make your dialogs presentation reusable.
To determine size of the dialog with table inside of it, set peferredContentSize property of your dialog view controller. This property will be used by UIPresentationController to set dialog height. Before setting peferredContentSize you can adjust the height to the margins you like.
To calculate size of table view you can use several options. table.layoutIfNeeded() worked for me.
Update: check out the article I wrote about this approach.
You need to set the tableView height dynamically, so give it two position constraints (ie centerX, centerY), a fixed width constraint, and then a height constraint set to some arbitrary constant (0). Take an outlet to the height constraint. Whenever you update the data that backs the tableview you take the min of the calculated tableview height and the superview height and set the height constraint constant to that value, then call setNeedsLayout on the tableView's superview. This way the tableview will either be the height of its content, or if the content is too big, the height of the superview and it will scroll.
tableViewHeightConstraint.constant = min(superview?.bounds.size.height ?? 0, numberOfCells * heightPerCell + headerAndFooterSize)
superview?.setNeedsLayout()
I guess that you know how many items you have before present the dialog and I guess that you dialog is a custom view (xib)
if this premises are true, you can resolve whit this line.
let dialog = CustomDialog(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: (heighCell * elements) + (heightButtonClose) + (heighTitle))
dialog.center = self.view.center
let me know if you have doubts

flutter change bottomNavigationBar(TabBar) height

I just found a way to change the height of AppBar with PreferredSize().
But I still not found a way modify the height of BottomNavigationBar and TabBar. Such widgets all hard coded, is there a way can change the widget height for thinner BottomBar and TabBar.
_BottomNavigationBarState class is private, and in it's build method there is string BoxConstraints(minHeight: kBottomNavigationBarHeight + additionalBottomPadding). There is no way to change this height without customization this class

Horizontal full width scroll in flutter

Flutter Listview component uses scrollDirection to do horizontal scrolling.
How do i set the width of the individual items to window width and scroll one page at a time?
You may want to use a PageView instead of a ListView as it works just like you described. It fits better your purpose because you don't have to write all the logic to manage the threshold of the pages and it automatically allocates the entire screen width for each element.

Wrap content of UILabel inside a row cell in swift

Being an android developer, when I want the width and the height of something to wrap its content I simply change the width and height value in the inspector to "wrap content" and it's done. it does not seem as simple as that in Xcode. What I'm trying to achieve is the design of a messaging app, i.e.:
As you see each row has it own height and width of the UILabel according to the text size.
I tried to look for solutions using auto layout and stuff but with no success!
I was able to have dynamic height but the width of the UILabel is always stretched when used and I'm kinda lost. any straightforward solution in order to achieve this or a hint on how to use the auto layout to get the desired results ? Thank you.
labelName.sizeToFit()will change the frame of the label to fit the text

Fluid UI layout on iPhone

I have an Android app with a UI like this for viewing emails:
I'm trying to port this to iOS and need it to work with iOS 5.0 and above (so can't use auto-layout in iOS 6.0). Hopefully you can tell how the layout should adjust/flow based on the example.
What would be the best way to handle this type of layout? The From and Re lines need to be variable height as shown (actually the To: line as well). The message body needs to be variable height of course.
My only attempt so far has been trying to use UITableViewController with static cells. I am able to get the variable height that way, by using sizeWithFont inside heightForRowAtIndexPath, to return the required height for each row. Using that method I'm having a heck of a time trying to get the style I want (rounded corners and background only for the top part).
So is there a better way? Maybe something that uses Collection View or Container View? On some other screens I need to port I have similar issues, but they have more levels of nesting (rounded blue section inside a white section inside a rounded blue section). Or would I be better off not using IB and building the entire UI in code from just basic label elements and generic views?
The easiest way I can think of is to manually compute for the label frames inside viewDidLayoutSubviews. Here's some pseudo-code:
On creation:
In IB, put all labels as subviews of the blue area. Check that the autoresizing mask of the container sticks to the top, left, and right, as well as have stretchable width. We'll fix the height and the subview frames in code. The message body can be a label or a textview as a separate view.
In viewDidLoad, set the containing view's layer cornerRadius, borderColor, etc. as appropriate.
In viewDidLayoutSubviews:
Time label: Easy. Just set the width to the superview width minus some padding, set the height with sizeWithFont:
For To:, From:, and Re:; call sizeToFit. Get the max width and hold on to that.
To: label: Set the x to 0 and y to the time label's bottom.
Receiver's name label: Set x to the width you got from (2.) and y to same as (3.). Set width to (container width - (2.)) and height with sizeWithFont:.
Do the same steps from (3.) to (4.) for the From: and Re: rows.
Set the blue view height to the frame bottom of the subject label.
Fill the rest of the frame with the body textview/label.
You have to add paddings on your own because sizeToFit and sizeWithFont: won't do that for you. Also, the body UITextView can scroll on it's own, but if you are expecting long subject titles then you should wrap the whole thing in another UIScrollView (or in IB just set the main view's class to UIScrollView)

Resources