How to remove spaces between icons when middle icons is not shown - flutter

I have 4 icons in column, i do not want to show 2 middle icons in some condition but their space still remained.
I want remove those space:
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
iconsModel[0].selected // decide to show icon or not
? Container(
child:Image.asset(
iconsModel[0].iconAdress,
fit: BoxFit.cover,
color: widget.currentColorRow.withOpacity(0.5),
),
)
: Container(),
iconsModel[1].selected // decide to show icon or not
? Container(
child:Image.asset(
iconsModel[1].iconAdress,
fit: BoxFit.cover,
color: widget.currentColorRow.withOpacity(0.5),
),
)
: Container(),
iconsModel[2].selected // decide to show icon or not
? Container(
child:Image.asset(
iconsModel[2].iconAdress,
fit: BoxFit.cover,
color: widget.currentColorRow.withOpacity(0.5),
),
)
: Container(),
iconsModel[3].selected // decide to show icon or not
? Container(
child:Image.asset(
iconsModel[3].iconAdress,
fit: BoxFit.cover,
color: widget.currentColorRow.withOpacity(0.5),
),
)
: Container(),
How can i remove these space when middle icons are not shown?

Simplest solution is to use MainAxisAlignment.start on Column But if you want the spaceBetween then use List of Widget and add/remove items from it based on iconsModel[any].selected.

Related

Expanded in SingleChildScrollView crashes the application

I'm trying to make an application on Flutter. I ran into a problem: I can't make the images stretch in width.
I need 2 columns, each has an image, each column should be 50% of the width of the screen, and stretch image with column. Everything works as shown in the picture, until I insert it into SingleChildScrollView. After that, the application crashes until I set the exact height of the image.
Question: how to do this WITHOUT specifying the height of the image? I can't set height anywhere inside Row, because I don't know exact size а the image.
try this:
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Row(
children: [
Expanded(
child: Image.asset(
'assets/images/test.jpeg',
fit: BoxFit.contain,
)),
Expanded(
child: Image.asset(
'assets/images/test.jpeg',
fit: BoxFit.contain,
))
],
),
),
Try flexible instead of expanded
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Row(
children: [
Flexible(
child: Image.asset(
'assets/images/test.jpeg',
fit: BoxFit.contain,
)),
Flexible(
child: Image.asset(
'assets/images/test.jpeg',
fit: BoxFit.contain,
))
],
),
),

I would like to remove the white square border behind the ClipOval

I am using ClipOVal. Currently, I would like to remove the white square boarder behind the ClipOval.
Card(fit: BoxFit.cover,
child: Column(
children: [
Flexible(
flex: 5,child:
ClipOval(
child: CachedNetworkImage(
imageUrl: CONSTANTS.server +
"/mytutor/mobile/assets/products/" +
widget.user.userId.toString() +
'.jpg',
width:350,
height:350,
fit: BoxFit.cover,
),
)),
],
)),
You can either remove card and add Container and add color: Colors.transparent or add the following to the card
color: Colors.transparent,
elevation: 0
The color is from the card thats above thr Column

Flutter resize image to fill available space

I would like to have this image fill the available space between the top element and the bottom element. Also I would like the bottom text to be always at the bottom.
This is how I implemented it right now:
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(children: topWidgets),
Container(
color: Colors.red,
child: Column(children: [
Image.asset("assets/empty_state/illustration_01.png", height: 200),
Text("No matches so far")
]),
)
]
)
I have tried to use the fit property of the Image without luck. If I don't use height: 200 I wouldn't be able to see the bottom text because it would go out of the screen (at the bottom)
I don't know if having a Column wrapping text and image is a good approach but I think it is the best among the ones I tried so far.
Container(
height: MediaQuery.of(context).size.height, //for max height
width: MediaQuery.of(context).size.width,//for max width
child: Column(
children: [
Expanded(
flex: 1, // flex is used to expand less/more the contained widget
child: topWidgets),
Expanded(
flex: 3,
child: Container( //for red background
color: Colors.red,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Expanded(
flex: 3, //increase to fill more space of picture
child: FittedBox(
fit: BoxFit.fill, // the picture will acquire all of the parent space.
child: Image.asset("assets/empty_state/illustration_01.png"),
)),
Expanded(
flex: 1, //by default flex is 1. so don't do this and save a line
child: Text("No matches so far"),)
],
),
))
],
),
)

Flutter Container with Network Image and an Aligned Container at the bottom

So, I have to display an image in a container and some description at the bottom of the container. The image is supposed to fill the entire screen. The image is being fetched from the network.
This is my intial approach
Stack(
fit: StackFit.expand,
children: <Widget>[
Container(
child: ClipRRect(
child: Image.network(category.imageURL,
height: maxHeight * 1, fit: BoxFit.cover),
),
),
Align(
alignment: Alignment.bottomLeft,
child: getNameAndDeleteSection(context),
)
],
),
),
where getNameAndDeleteSection is basically this
return Container(
color: Colors.yellow,
margin: EdgeInsets.fromLTRB(5, 0, 5, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
category.name,
maxLines: 1,
style: TextStyle(color: Colors.white),
),
Spacer(),
IconButton(
icon: Icon(Icons.delete, size: 18.0, color: Colors.white,),
onPressed: () {
deleteTap(category);
},
)
],
),
);
}
When the network image is loaded, it hides the yellow container , but keeps the children. What I want is the image to remain at the background and the yellow container to remain above it. The idea is that the yellow container will have a transparent color of sorts , to make it look floating on the image.
The max height is the height provided by the listView. I'm trying to make the UI adaptive. Any help?
Why not puting your image as a BoxDecoration in your container like this:
Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(url), fit: BoxFit.cover,
),
),
),
Align(
alignment: Alignment.bottomLeft,
child: getNameAndDeleteSection(context),
)
],
),
Instead of using a Container that always seems to get hidden by the Network Image, I decided to use Card that will envelop the container. And it worked.
So, the change was done in the getNameAndDeleteSection and basically wrapped the entire code in a Card. And bingo, it worked.

Flutter: Stack ignores Image's alignment property

I have a grid of images and I want an delete icon on the top-right position of each image.
I used this code to achieve it:
return Stack(
alignment: Alignment.topRight,
fit: StackFit.expand,
children: <Widget>[
Image(
image: image,
fit: BoxFit.cover,
),
Icon(Icons.delete, color: Colors.white,),
],
);
What I got:
The problem is, as you can see, the delete icon is on the center of the image not the up-right even though I set Stack's alignment to Alignment.topRight. I know this happens because I set Stack's fit to StackFit.expand but if I remove it then Image's fit property will be ignored and I'll get this:
So what should I do if I want to keep my Images square and be able to move the Icon to borders?
You should set the alignment only for the icon, so wrap the icon in an Align widget and set its alignment.
return Stack(
fit: StackFit.expand,
children: <Widget>[
Image(
image: image,
fit: BoxFit.cover,
),
Align(
alignment: Alignment.topRight,
child: Icon(
Icons.delete,
color: Colors.white,
),
),
],
);