I want to create a `Spacer` with the size of an `I...
# compose
f
I want to create a
Spacer
with the size of an
Icon
but there doesn't seem to be adDefault value for this (like we have for many other standard composables). So what should I use here?
Copy code
Icon(
     Icons.Default.Check,
     contentDescription = stringResource(R.string.task_completed),
     tint = MaterialTheme.colors.primary,
)
d
I'm guessing you want to remove the Icon but still take up the same space?
f
Yep. Are you suggesting setting the alpha to 0?
d
(Yeah I'm also not sure how I feel about compos-material hiding all the measurements.)
Ha, close!
No, I was going to suggest using
Modifier.onSizeChanged
to remember the size, then you can create a spacer using it. (That's what I do in my app)
f
I'll try that, thank you!
d
Although, looking at your alpha idea. You could use one of the canvas modifier to just skip drawing the icon. So the measurements will still be done but it just won't be drawn. (I don't remember what the modifier is called but it might be
Modifier.drawWithContent
) (Idk which approach is better)
f
yea that sounds good too
or just actually set the alpha to 0
although that feels a bit hacky
but maybe not
d
Might as well skip the draw since that's what you really want. Alpha does feel hacky to me too.
s
Yep, changing alpha channel is hacky in order to hide ui element. You could try to use
drawWithContent
modifier, inside lambda block just call function
drawContent
under your visibility condition.
l
All material icons are 24.dp by default, so you can just use this. Might be a reasonable request to expose this as api somewhere
💯 1
f
Thank you everyone!