I have this text:
Centered text with no icon next to it
I’d like to place an info icon just on the right, keeping the text centered. When I put both inside a centered and center-aligned row, the text shifts to the left like this:
no centered text with an icon next to it
I want the position of the icon to be relative to the text, so if the text changes, the icon is always close to the right:
Others are not centered text with icon next to it
But in both of this cases, the text is shifted and I need it centered.
Code:
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: (_boolExample ? [const Color(0xffdf1b0c), const Color(0xff6b0a2b)] : [const Color(0xff89ff8e), const Color(0xff45a7f5)]),
stops: const [0, 1],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
(_boolExample ? "TEXT EXAMPLE" : "ANOTHER TEXT EXAMPLE"),
style: const TextStyle(
fontSize: 36,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
color: Color.fromARGB(255, 255, 255, 255),
),
),
const Padding(
padding: EdgeInsets.all(8.0),
child: InfoPopupWidget(
contentTitle: 'Info Popup Details',
child: Icon(Icons.info),
),
)
],
),
),
),
If I place a SizedBox of width 40 on the left, the text keeps centered but it seems pretty inelegant and inflexible: what if I wanted to add more items to the right?
I also tried adding a Container wrapped with Expanded on the left, and wrapping each other item in the row with expanded too:
<Widget>[
Expanded(child: Container()),
Expanded(
child: Text(
(_boolExample ? "TEXT EXAMPLE" : "ANOTHER TEXT EXAMPLE"),
style: const TextStyle(
fontSize: 36,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
color: Color.fromARGB(255, 255, 255, 255),
),
),
),
const Expanded(
child: Padding(
padding: EdgeInsets.all(8.0),
child: InfoPopupWidget(
contentTitle: 'Info Popup Details',
child: Icon(Icons.info),
),
),
)
],
But it just moves everything to the left. The same with Spacer.
I tried using Align inside ot the Widget children but it just aligns them within the "cell" of the row they’re inside.
Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Align(
alignment: Alignment.center,
child: Text(
(_boolExample ? "TEXT EXAMPLE" : "ANOTHER TEXT EXAMPLE"),
style: const TextStyle(
fontSize: 36,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
color: Color.fromARGB(255, 255, 255, 255),
),
),
),
const Padding(
padding: EdgeInsets.all(8.0),
child: InfoPopupWidget(
contentTitle: 'Info Popup Details',
child: Icon(Icons.info),
),
)
],
),
),
Resulting in this:
Using Align
I looked for a way to make the first item centered and the row to grow to the right, but I couldn’t find it. I think that maybe a row is not the right tool for this. What widget could help me achieve this and how?
2
Answers
In your case maybe you can consider using
Stack
Here is the code:
As its name suggests,
Stack
in Flutter allows you to stack widgets together and set their align behaviour and position seperately.In the example code above, we set the default align behaviour to
center
, and we set the position of theCircleAvatar
usingPositioned
. Please check the code comment for more info and explanationI’m also learning flutter now and my solution may not be the best one, but hope this could helps you
As pskink mentioned, you can use
CompositedTransformTarget
withCompositedTransformFollower
for the icon.