I want to fit a child image to the width of the column father.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Image.network("Some image url"),
Text("Fitted image"),
],
),
I suggest the following chenges
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Image.network("Your image url",
width: double.infinity,
fit: BoxFit.fitWidth,
),
Text("Fitted image"),
],
)
Well, I resolved my problem using MediaQuery.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Image.network("Some image url", fit: BoxFit.fill,),
width: MediaQuery.of(context).size.width,
),
Text("Fitted image"),
],
),
Maybe wrapping your Image with an Expanded Widget and give it BoxFit.fitWidth would also be useful. Or in case the times when you can't use MediaQuery.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
flex: 5,
child: Image.network(
"Some Image Url",
fit: BoxFit.fitWidth,
),
),
Text("Fitted image"),
],
),
If you need to control the size of elements in the column try to wrap all your widgets inside column with expanded widget and adjust elements sizes with flex value.
Related
my code is inside a ListView Widget. i am having a problem with my images, since each one of them have a dimensions which is different than the other ones. i want them to appear all with the same dimension, but some of them is being bigger than the others.
note : i have used Width and Height and it didn't help
i want both of them to be with the same dimension but the second one is bigger !
i have even tried the BoxFit method as well and didn't help.
here is my code sample:
ListView(
children: List.generate(
sandwichFood.length,
(index) => Padding(
padding: EdgeInsets.all(10),
child: Container(
padding: EdgeInsets.only(left: 10),
decoration: _foodDecor(),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
children: [
Text(
sandwichFood[index],
style: TextStyle(fontSize: 25),
textAlign: TextAlign.left,
),
Text(
'Price : ${sandwichPrice[index]}',
style: TextStyle(fontSize: 20),
),
],
),
Image(
image: AssetImage(sandwichImage[index]),
height: 200,
width: 200,
),
],
),
),
),
),
);
Image class has parameter fit. Try to use it:
Image(
image: AssetImage(sandwichImage[index]),
fit: BoxFit.cover,
height: 200,
width: 200,
)
This is my source code:
Scaffold(
body: Center(
child: Container(
color: Colors.green,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
fit: FlexFit.loose, child: Image.asset('images/demo.png')),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Container(
color: Colors.yellow,
child: Text('Cancel'),
),
),
Expanded(
child: Container(
color: Colors.red,
child: Text('Okay'),
),
),
],
),
],
),
),
),
);
This is the result:
I want it to be like this:
without using fixed sizes because the image dimensions may vary.
I already tried to use flexible on the row children. The problem with that solution is that the green container is being expanded like this:
which I don't want to happen.
EDIT, Did a little more searching and found what you seem to be looking for:
The IntrinsicWidth widget makes sure children have uniform width, which will be the width of the widest element (without the overlapping issue like in the Stack widget based solution). So this code accomplishes what you're trying to do:
Scaffold(
body: Center(
child: IntrinsicWidth(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
flex: 1,
fit: FlexFit.tight,
child: Image.asset('images/demo.png'),
),
Row(
children: [
Expanded(
flex: 1,
child: Container(
color: Colors.yellow,
child: Text('Cancel'),
),
),
Expanded(
flex: 1,
child: Container(
color: Colors.red,
child: Text('Okay'),
),
),
],
),
],
),
),
),
);
Screenshot:
OLD ANSWER:
If you don't mind overlapping a little bit of the bottom of the image with the buttons, the stack class sizes itself to its non positioned children, so this would work for you:
Scaffold(
body: Center(
child: Stack(
fit: StackFit.loose,
children: [
Image.asset('images/demo.png'),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
flex: 1,
child: Container(
color: Colors.yellow,
child: Text('Cancel'),
),
),
Expanded(
flex: 1,
child: Container(
color: Colors.red,
child: Text('Okay'),
),
),
],
),
),
],
),
),
);
Screenshot:
Maybe this will help.
You can use Spacer(); between the two text Cancel & Okay. Spacer() will put second text to end of row.
Row(
children[
Container(child: Text('Cancel', style...)),
Spacer(),
Container(Text('Okay', style...)),
]
);
maybe you can get the width of the image and pass it to the container that wraps the column:
import 'dart:io';
File image = new File('image.png'); // Or any other way to get a File instance.
var decodedImage = await decodeImageFromList(image.readAsBytesSync());
final width = decodedImage.width;
How do I put a image.network() inside a card?
new Card(
child: new Container(
Image.network(data[index]['image'], width:200.0, height: 200.0,fit: BoxFit.cover),
child: new Text(data[index]['title']),
padding: const EdgeInsets.all(20.0),
),
)
It works fine if I remove the Image.network(...) line.
new Card(
child: new Container(
child: new Text(data[index]['title']),
padding: const EdgeInsets.all(20.0),
),
)
I solved it myself but will put the code here just in case anyone else has this issue. As there is no documentation on how to do what I wanted to do. I am sure there maybe better ways to get the outcome like (text over the bottom of the image, but this question was how to get an image from network to show in a card)
new Card(
child: new Container(
child: new Column(
children: <Widget>[
Column(
children: <Widget>[
Image.network(data[index]['image'], fit: BoxFit.fitWidth),
Text(data[index]['title'], style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)),
]
),
],
),
),
)
I solved it myself but will put the code here just in case anyone else has this issue. As there is no documentation on how to do what I wanted to do. I am sure there maybe better ways to get the outcome like (text over the bottom of the image, but this question was how to get an image from network to show in a card)
new Card(
child: new Container(
child: new Column(
children: <Widget>[
Column(
children: <Widget>[
Image.network(data[index]['image'], fit: BoxFit.fitWidth),
Text(data[index]['title'], style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)),
]
),
],
),
),
)
I have tried these
1. DecorationImage
2. Image
3. CircleAvatar
I have used Stack as there will be a background image.
But I am not able to understand why the image is expanding like that.
I need a circular image for the profile pic.
If I use Column instead of ListView then the images doesn't expand like this ...but I need a ListView.
The code:-
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 80.0,
width: 80.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
image: AssetImage(Assets.cool),
fit: BoxFit.cover,
),
color: Colors.red,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 50.0,
width: 50.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
),
child: Image(
image: AssetImage(Assets.cool),
fit: BoxFit.cover,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 50.0,
backgroundImage: AssetImage(Assets.cool),
),
)
],
)
],
),
);
}
From my understanding you want the image in a circle not a ellipse. You can achieve that by wrapping the items in your listview with a Row element.
return Scaffold(
body: Stack(
children: <Widget>[
ListView(
children: <Widget>[
// Wrap with a row
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 80.0,
width: 80.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
image: AssetImage("a.jpg"),
fit: BoxFit.cover,
),
color: Colors.red,
),
),
),
//...
],
),
],
)
],
),
);
To get a circular image, Just wrap all your widget of ListView in a Column widget and place the Column in ListView. You'll get a scrollable column.
ListView(
children: <Widget>[
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 80.0,
width: 80.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
image: AssetImage(Assets.cool),
fit: BoxFit.cover,
),
color: Colors.red,
),
),
),
//.....
],
)
],
),s
I have this Code:
new Container(
padding: new EdgeInsets.only(
top: 20.0,
),
alignment: FractionalOffset.centerRight,
child: new GestureDetector(
child: new Text('Forgot password?', style: new TextStyle(color: Color(0xFF2E3233))),
),
),
new Container(
width: 300.0,
height: 45.0,
alignment: FractionalOffset.center,
decoration: new BoxDecoration(
color: const Color(0xFF2E3233),
borderRadius: new BorderRadius.all(const Radius.circular(30.0)),
),
child: new Text("Sign in",style: new TextStyle(color: Colors.white,fontWeight: FontWeight.bold,letterSpacing: 0.3,),),
),
new Container(
padding: new EdgeInsets.only(
top: 20.0,
),
alignment: FractionalOffset.center,
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new GestureDetector(
child: new Text('Don\'t have an account?', style: new TextStyle(color: Color(0xFF2E3233))),
),
new GestureDetector(
child: new Text('Register.', style: new TextStyle(color: Color(0xFF84A2AF), fontWeight: FontWeight.bold),),
onTap: moveToRegister,
),
],
),
),
Thats the result:
https://www.dropbox.com/s/bqu2tv6rej0ejlt/Screenshot_20181104-171155.png?dl=0
But I want to achieve this:
https://www.dropbox.com/s/754hempszq8xw5k/Screenshot_20181104-171202.png?dl=0
EDIT:
build method:
#override
Widget build(BuildContext context) {
return new Stack(
children: <Widget>[
// The containers in the background
new Column(
children: <Widget>[
new Container(
height: MediaQuery.of(context).size.height * .44,
color: const Color(0xFF84A2AF),
padding: new EdgeInsets.only(
top: 50.0,
),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
new Image.asset('assets/app_logo_transparent_bg.png',
height: 100.0,
width: 100.0,
),
],
),
),
new Container(
height: MediaQuery.of(context).size.height * .56,
color: Colors.white,
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Align(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: buildSubmitButtons(),
),
),
],
)
/*child: new Align(
alignment: Alignment.bottomCenter,
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: buildSubmitButtons(),
),
),*/
),
],
),
// The card widget with top padding,
// incase if you wanted bottom padding to work,
// set the `alignment` of container to Alignment.bottomCenter
new Container(
alignment: Alignment.topCenter,
padding: new EdgeInsets.only(
top: MediaQuery.of(context).size.height * .33,
right: 15.0,
left: 15.0),
child: new Container(
height: 130.0,
width: MediaQuery.of(context).size.width,
child: new Card(
color: Colors.white,
elevation: 2.0,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: buildInputs(),
),
),
),
)
],
);
}
I tried so much but nothing seems to help? How I can align the Layout to the bottom...
Just a few changes on your container (.56) , remove the Align widget, stretch your Column and add an Expanded item inside your Column to fill the space available.
new Container(
height: MediaQuery.of(context).size.height * .56,
color: Colors.white,
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: Container(),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.end,
children: buildSubmitButtons(),
),
),
],
)
/*child: new Align(
alignment: Alignment.bottomCenter,
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: buildSubmitButtons(),
),
),*/
),
Probably you will need to move your 'Forgot password' above the Expanded widget inside your Column.