I'm currently working on a button, which has 3 elements: An icon (with a fixed size), a title (f.e Buy Now!) and the price of the item. The price which should be displayed is adaptive, this could be €2,00 or €2000,00. The title is supposed to be centered, based on the Button itself, rather than the area it can occupy.
The price of object has the priority within the button, and should always be fully displayed with a set style. Due to this, the size of this object is variable, and can not be determined beforehand.
When the length of the price object increases, naturally the available space of the title decreases. However, when attempting to center the text, I could only get it to center based on the available space, which resulted in the text being off-center.
How could one approach this issue, allowing for the text to be centered based on the parent (button), rather than the available text size?
You can try this:
The
Button
has a content parameter you can use to set its content, in this case we use aRow
to set contents in the horizontal axis.Note that each of the components,
Icon
Text
andText
are wrapped inside aBox
with aweight
of1f
, making each those boxes as their container that also takes equal divided space of the parentRow
.The middle
Box
positions its child in the center, while the first and lastBox
positions their children (i.eIcon
andText
) inTopStart
andTopEnd
alignment, though you don't need to worry the "top" positioning as its neglected here because the parentRow
aligns all its childrencenter-vertically
If we put a
background
color on eachBox
,we can clearly see their equal
width