So i like to figure out how to make Card widget become full width in flutter, basically it only expand its width based on its child, i want it to fit the screen, the task fairly simple but im having a really hard time achieving that, in my scaffold basically it contain two card, and have some text in it, the card only expand to the size of the end of the text, i want the card to be full width, how do i achieve this?
I expect it the card to be full width of the screen, but the card only wrap the children text widget
Wrap the Column in a Container widget and set it's width to double.infinity
This way you'll save some lines of code.
Example Code:
Container(
width : double.infinity,
child: Column([...])
)
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
// flex:3,
child: Container(
padding: EdgeInsets.all(20.0),
color: Colors.cyan,
child: Text('1'),
)//Container
),//Expanded
],
)//column
this should do the trick
Try this.
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
width: double.infinity,
child: new Card(child: Text("This Card is Expanded to full width"),),
)
],
)
You can get the screen width with MediaQuery
final screenWidth = MediaQuery.of(context).size.width;
Related
Good morning. I'm trying to create this screen. Two problems with that:
1) I can't stretch the green horizontal line to the entire width. This is my code, more or less:
Scaffold(
Stack(
Center(
Column(
...
),
),
Align(
alignment: Alignment.center,
child: Image(
image: AssetImage('assets/load_line.png'),
fit: BoxFit.fitWidth,
),
),
And this is what my code is giving me. Seems like there is a padding or margin somewhere, but I can't find it.
2) As you can see in my first screenshot, I need the logo and the horizontal line to center one on top of the other, but they obviously miss align. I thought using a single image, but I'm afraid the stretching would ruin it. What can I do to achieve this result?
Thanks, everyone.
You can try this way.
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
appBar: AppBar(title: Text("Title")),
body: Stack(
children: <Widget>[
Align(child: Image.asset("assets/images/profile.jpg")),
Align(child: Container(height: 1, color: Colors.black12)),
Align(
alignment: Alignment(0.0, 0.2),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text("Your first text here!"),
Text("Your second text here"),
Text("Your third text here!"),
],
),
)
],
),
);
}
Output
Is possible change widget layout when app run?
I want change widget position to left or right side of screen if bool change to true or false.
For example inside Row I have widget in centre. But I want change widget on outside to left or right:
return Container(
child: new Row(
children: <Widget>[
// Need change this widget position
new Container(
child:
Text(‘Outside’)
),
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(
‘Middle’
),
],
),
),
// new Container(
// child:
// Text(‘Outside’)
// ),
],
),
);
Is possible change Container position? I have try use alignment but I cannot make work.
Thanks!
I think what you're looking for is Row's parameter textDirection. If you set this to TextDirection.rtl it should switch the order of its children. I don't believe this propagates downwards but you may want to test that to make sure.
I am trying to align something to the right without resizing it's parent. So technically the box size should be dependent on the text object, and the icon should be aligned to the right.
return new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text("this is a test string"),
new Align(
alignment: Alignment.topRight,
child: new Icon(Icons.speaker_notes),
),
]);
Once the alignment is set to Alignment.topRight the box gets drawn with the full screen width. I understand why it gets rendered like this, but can't think of a solution either. Any ideas?
You could try something like this:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Align(
alignment: Alignment.topLeft,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
new Text("this is a test string"),
new Icon(Icons.speaker_notes),
]),
),
);
}
Result
I have a Stack widget with some children. One of those children is an Align which has a Container child which has a Column child. This Column has some children. Now, in my Stack, the Column is way bigger than it should be (it fills the complete screen) although a third of that space would be enough. I'd like to know how I can use something like Flexible so I can use the fit property to tighten up the space. Is this possible?
Here is a representation of my code:
new Stack(
children: <Widget>[
new Container(...),
new Align( // This takes up way more space than needed!
alignment: new Alignment(1.0, 1.0) // This widget should be displayed at the bottom
child: new Container(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget> [
new Container(child: new Center(child: new Text("text"))),
new Row(children: [...]),
new Row(children: [...]),
new Row(children: [...]),
new Row(children: [...]),
]
)
)
)
]
)
I tried playing around with the fit property but without any luck.
By default, Column has mainAxisSize defaulting to MainAxisSize.max.
Which means that Column will take all the available height.
In your case, you don't want that. So you can set that value to MainAxisSize.min, which will do the opposite : fit it's children.
The end result is :
child: new Stack(
children: <Widget>[
new Container(...),
new Align(
alignment: Alignment.bottomCenter,
child: new Container(
color: Colors.purple,
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[new Text("foo"), new Text("hello world")],
),
),
)
],
),
I am a complete newbie in Flutter, but I already like it.
The question is:
why is the column not expanding to the full height?
code is on gist.github
Using CrossAxisAlignment.stretch works for me:
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text("Some title"),
Text("Address"),
Text("Description")
],
),
A Column widget is flexible - it will try to take up as much space as it needs but no more. If you want it to take up all availible space, then wrap it in an Expanded widget.
createChildren() {
return [
Image.network(
testImg,
height: imageSize,
width: imageSize,
),
Padding(padding: EdgeInsets.only(left: 16.0)),
Expanded(child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
boldText("Some title"),
Text("Address"),
Text("Description")
],
),
),
];
}
Wrapping the column with a row with MainAxisSize.max
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Column(
children: <Widget>[
_someWidget(),
_someWidget(),
_someWidget(),
],
),
],
)
Don't use flex=1 inside Column, here you can see example
children: [
MyWidget(),
Expanded(
child: MyWidget(),
),
MyWidget(),
],
Based on your layout I assume that your Column is nested inside a Row. Wrap your Row with IntrinsicHeight and your Column will automatically expand its height to be the same as the Row's tallest child:
IntrinsicHeight(
child: Row(
children: [
Container(width: 40, height: 40, color: Colors.blue),
// tallest child
Container(width: 40, height: 60, color: Colors.blue),
// height will be 60
Column(
...
),
],
),
)
You can wrap the Column inside an infinite width SizedBox.
SizedBox(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [ ... ]
)
)
The above code snippet will place the children at the center of the SizedBox. It'll work especially well if you want to use a Column as the body of your Scaffold. In that case children will show up at the center of the screen.
The code linked in the question is no longer available, so I can't give you a full working example for your case. However, one thing that will likely work is using mainAxisAlignment as follows:
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
Text("Some title"),
Text("Address"),
Text("Description")
],
),
(mainAxisSize: MainAxisSize.max is actually the default value, butthis only works if you do not set it to MainAxisSize.min, so I included it here.)
Setting mainAxisAlignment to MainAxisAlignment.spaceAround or MainAxisAlignment.spaceEvenly will also expand the column, but spacing between/around children will be handled differently. Try it out and see which you like best!
This will cause the column to take all the space it can - if it still doesn't expand then that is due to its parent widget, and we can no longer access the code so I can't speak to that.