Alignment with Stack in flutter - flutter

Project
Hi,
I was trying to align some widgets inside a Stack in Flutter. My end result was something like:
I know that this can be easily achieved with a Row but I want to animate the position ot the two children so I'm forced to use stack.
My problem is simple:
Example code
return Container(
child: Stack(
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: _buildSign(),
),
Align(
alignment: Alignment.centerRight,
child: _buildSign(),
),
],
),
);
This will not render as I expected. No matter what I put in the alignment field: Alignment.centerRight and Alignment.centerLeft will always place the child to the center left.
The problem is solved only if I give a fixed width to the wrapping container. Now I understand why this happend but what if I want the container to be the size of his childrens? Label is a dynamic text so his width is unpredictable for me
Any ideas?
Thanks

Hy justAnotherOverflowUser,
In your case, you have to use Positioned Widget inside Stack Widget,
it will give you what you want.
as example:
Positioned(
left: 20.0,
child: Icon(
Icons.monetization_on,
size: 36.0,
color: const Color.fromRGBO(218, 165, 32, 1.0)
)
)

The more flexible way for such alignment is to wrap Align with Positioned.fill
The original answer was posted here Flutter: bottom center widget in stack
return Container(
child: Stack(
children: <Widget>[
Positioned.fill(
child: Align(
alignment: Alignment.centerLeft,
child: _buildSign(),
),),
Positioned.fill(
child:Align(
alignment: Alignment.centerRight,
child: _buildSign(),
),),
],
),
);

Related

Center-Align one flutter widget in column and position the rest around it

How do I align one widget of a column in the center and put the rest around it?
If I had a Container or so that holds the three input fields and the button and another one that is the box above. How do I align the Container in the middle and put the box in the remaining space without moving the center container?
I tried using Expanded or Flexible but couldnt figure out how to align the widgets in that kind of way.
You can use Column like this:
Column(
children: [
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 40,
width: 40,
color: Colors.yellow,
),
)),
Container(
color: Colors.red,
height: 100,
width: double.infinity,
),
Expanded(
child: Align(
alignment: Alignment.topCenter,
child: Container(
height: 40,
width: 40,
color: Colors.green,
),
)),
],
),

Using Align widget in the Stack widget without it expanding

I'm trying to create a custom widget and it makes use of the Stack widget. When I wrap the Stack's children using the Positined widget, the stack hugs the children that are not positioned. When I try to use the Align widget for more precise placement the stack expands to fill all the available space.
I'm trying to avoid using any widget with a fixed size, i.e. Container or SizedBox, as the parent for the Stack widget.
Is there a way to prevent the stack from filling all the available space or work around this that archives the same result?
I'm trying to position a child to the top center of the Stack as an example and this is where I am right now:
Using the Positined widget (I have to try multiple time to get the center position and sometimes its still off):
Positioned(
top: 0,
left: 80,
child: child,
);
Using the Align widget:
Align(
alignment: Alignment.topCenter,
child: child,
);
This should be the result using the Align widget:
Align(
alignment: Alignment.topCenter,
child: child,
);
You can set Stack's alignment to topCenter, Try this:
Stack(
alignment: Alignment.topCenter, // <=== add this
children: [
Container(
color: Colors.grey,
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisSize: MainAxisSize.min, // <=== add this
children: [
Container(
color: Colors.amber,
height: 30,
width: 30,
),
Container(
color: Colors.green,
height: 30,
width: 30,
),
Container(
color: Colors.blue,
height: 30,
width: 30,
),
Container(
color: Colors.yellow,
height: 30,
width: 30,
),
],
),
),
Container(
color: Colors.red,
height: 30,
width: 30,
)
],
)
Note: make sure you set Row's mainAxisSize to min.

Vertically centering an overflowing widget

I have a bit of user generated content that is restricted to a specific height. Anything over that height should be overflowing and clipped off.
I've achieved that by wrapping the content with a Wrap widget, however the extra requirement i have is that the overflowed content should always be centered in its parent, see image below
The current behaviour is the first red box, but what I'm looking for is to vertically center the content similarly to box #2
Example code I've used is:
Container(
constraints: BoxConstraints(minHeight: 40, maxHeight: 40),
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.red,
),
clipBehavior: Clip.hardEdge,
child: Wrap(
children: [
Column(
children: [
Text('Line1'),
Text('Line2'),
Text('Line3'),
Text('Line4'),
Text('Line5'),
Text('Line6'),
Text('Line8'),
Text('Line9'),
Text('Line10'),
],
),
],
),
)
I've tried the various alignment properties the Wrap widget offers, however none of them did the trick. Is there a specific flutter widget I can use to achieve this?
You can use a FittedBox (https://api.flutter.dev/flutter/widgets/FittedBox-class.html) with BoxFit.none (https://api.flutter.dev/flutter/painting/BoxFit.html#none) to center the child in the available space while clipping everything outside.
Container(
constraints: BoxConstraints(minHeight: 40, maxHeight: 40),
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.red,
),
clipBehavior: Clip.hardEdge,
child: FittedBox(
fit: BoxFit.none,
child: Column(
children: [
Text('Line4'),
Text('Line5'),
Text('Line6'),
Text('Line8'),
Text('Line9'),
Text('Line10'),
],
),
),
),

Is there an easier way to adjust the size of an SVG?

I am looking to cut the image of the SVG in half. The SVG is the lighter wave at the bottom of the circle. It is currently too high, and I need to reduce the size by about half. I placed the SVG in a container of its own, but modifying the size of the container isn't exactly modifying the size of the image in ways I would expect. Any suggestion are appreciated.
return Container(
decoration: bubbleBoxDecoration,
height: bubbleDiameter.toDouble(),
width: bubbleDiameter.toDouble(),
child: Stack(
children: [
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'Upper Body',
style: labelTextStyle,
),
Text(
'45',
style: weightTextStyle,
),
Text(
'lbs',
style: unitTextStyle,
),
],
),
),
Container(
height: bubbleDiameter.toDouble(),
width: bubbleDiameter.toDouble(),
child: SvgPicture.asset(
assetName,
alignment: Alignment.bottomCenter,
),
),
],
),
);
}
}
The key here is the fit parameter. Also, since you want to cut off the bottom and see the top, use Alignment.topCenter.
Container(
height: bubbleDiameter.toDouble() / 2,
width: bubbleDiameter.toDouble(),
child: SvgPicture.asset(
assetName,
fit: BoxFit.fitWidth,
alignment: Alignment.topCenter,
),
),
You can trick the UI while using Stack and make the image size the way you want. You need to resize the image and align properly. The only reason your code-snippet is not reflecting the size because Stack child require a positional widget like Positioned / Align to reflect custom size.
Positioned(
bottom: -100,// bubbleDiameter.toDouble() the way you like to align
child: SvgPicture.asset(
assetName,
height: bubbleDiameter.toDouble() * 2,
width: bubbleDiameter.toDouble() *2,
alignment: Alignment.bottomCenter,
),
),
You cannot modify an asset image in flutter. even if you can, it won't be ideal, because it's an asset, you can remove , edit and add it back, rather than modifying the asset every time you run your app.

Incorrect use of ParentDataWidget. I don't think I have much of a choice here, do I?

IgnorePointer(
child: cProtractor & darkModeOn
? Positioned.fill(
child: Align(
alignment: Alignment.center,
child: Center(
child:
Image.asset('lib/assets/img/cprot2white.png',
width: 500.0, height: 500.0),
)))
: SizedBox())
]));
}
}
The above function switches on and off a protractor overlay for my Google Map. Its parent is a body: Stack(children: [. Is there any way to fix this or get rid of the error? The function is working.
You have to remove Ignore pointer as parent widget of Positioned Widget as Positioned widgets are placed directly inside Stack widgets.
You can achieve the same using below code
Positioned.fill(
child: Align(
alignment: Alignment.center,
child: IgnorePointer(
child: Center(
child: Image.asset('lib/assets/img/cprot2white.png',
width: 500.0, height: 500.0),
),
))),
Here just the hierarchy of the widgets are changed