Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 912 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sizing a container to exact half the screen size in flutter

#1
I am trying to get a container to be exactly half the screen height[after considering the AppBar height] and half the screen width.

This is what I came up with...

class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Flexible(
child: Container(
width: MediaQuery.of(context).size.width / 2,
color: Colors.red,
),
),
Flexible(
flex: 1,
child: Container(),
)
],
),
);
}
}

Is there a better way?
[![current layout][1]][1]


[1]:
Reply

#2
You are on the right track about using MediaQuery, but your code can be a lot simpler:

Scaffold(
appBar: AppBar(),
body: Align(
alignment: Alignment.topCenter,
child: Container(
height: MediaQuery.of(context).size.height / 2,
width: MediaQuery.of(context).size.width / 2,
color: Colors.red,
),
),
);
Reply

#3
If you want the container's height to be the half of the available space, you can use LayoutBuilder widget. With LayoutBuilder widget, you can know inside the builder function what the max available width and height would be. The example usage in your case would be like this:

Scaffold(
appBar: AppBar(),
body: Align(
alignment: Alignment.topCenter,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
height: constraints.maxHeight / 2,
width: MediaQuery.of(context).size.width / 2,
color: Colors.red,
);
},
),
),
);
Reply

#4
You can deduct `AppBar`'s height to configure `Container` size.

@override
Widget build(BuildContext context) {
var appBar = AppBar();
return Scaffold(
appBar: appBar,
body: Align(
alignment: Alignment.topCenter,
child: Container(
height: (MediaQuery.of(context).size.height - appBar.preferredSize.height) / 2,
width: MediaQuery.of(context).size.width / 2,
color: Colors.red,
),
),
);
}

[![][1]][1]



[1]:
Reply

#5
The best way and simplest route to this and similar problems is simply by using `FractionallySizedBox` widget ; e.g

FractionallySizedBox(
alignment: Alignment.topCenter,
widthFactor: 0.5,
child: Container(
height: 100,
),
),

This widget (`FractionallySizedBox`) lets you build up your `child` by specifying the fraction of the parent width (or height) which is available to the `child` widget. After that , by specifying the `alignment` , you can specify the manner the rest of the space would be allocated.

For more help on this widget , please visit the [offical help page of this widget][1].


[1]:

[To see links please register here]

Reply

#6
Or just simply...

SizedBox(
height: MediaQuery.of(context).size.height*0.5,
child: ...,
),
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through