[英]How to align the Stack widget to the center of the screen?
Faced a problem I can not align the widget to the center of the screen.遇到一个问题,我无法将小部件与屏幕中心对齐。 I have everything in a column, I tried to set the alignment of the column (
mainAxisAlignment
, crossAxisAlignment
) and tried to align it to the center through the Alignment
widget - it did not help.我将所有内容都放在一列中,我尝试设置列的对齐方式(
mainAxisAlignment
、 crossAxisAlignment
)并尝试通过Alignment
小部件将其对齐到中心 - 它没有帮助。 I understand the reason is that I use the Stack
widget.我知道原因是我使用了
Stack
小部件。 But how can you still align this widget to the center of the screen?但是你怎么还能把这个小部件对齐到屏幕的中心呢?
body身体
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Column(
children: [
const SizedBox(height: 121),
StarWidget(rating: rating),
AvatarWidget(),
],
),
);
AvatarWidget头像小部件
return SizedBox(
height: 32,
child: Stack(
children: List.generate(
userPhoto.length > 3 ? 3 : userPhoto.length,
(index) => Positioned(
left: 22.0 *
((userPhoto.length > 3 ? 3 : userPhoto.length) - index),
child: index == 0 && userPhoto.length > 3
? Container(
width: 32,
height: 32,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: constants.Colors.blackLight, width: 0.7),
),
alignment: Alignment.center,
child: Container(
width: 30,
height: 30,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: constants.Colors.greyXDark.withOpacity(0.5),
),
alignment: Alignment.center,
child: Text('+${userPhoto.length - 2}',
style: constants.Styles.smallerHeavyTextStyleWhite),
),
)
: Container(
width: 32,
height: 32,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: constants.Colors.blackLight, width: 0.7),
),
alignment: Alignment.center,
child: Container(
width: 30,
height: 30,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(userPhoto[index])),
),
),
)),
),
),
);
While we know the size of item, we can calculate the width and provide on SizedBox(width:renderItemCount*singleChildWidth)
虽然我们知道项目的大小,但我们可以计算宽度并提供
SizedBox(width:renderItemCount*singleChildWidth)
SizedBox(
height: 32,
width: (itemCount > 3 ? 3 : itemCount) * 32, //inner container width
child: Stack(
you can use this function to get screen width and give your widget margin like thise :您可以使用此功能获取屏幕宽度并为您的小部件提供如下边距:
double getScreenWidth(BuildContext context) {
return MediaQuery.of(context).size.width;
}
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(left: getScreenWidth - 100),
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Column(
children: [
const SizedBox(height: 121),
StarWidget(rating: rating),
AvatarWidget(),
],
),
);
I hope that gonna helps you我希望这会帮助你
try it like this像这样试试
return SizedBox.expand(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 121),
StarWidget(rating: rating),
AvatarWidget(),
],
),
),
);
Try "Alignment.center" like below.试试下面的“Alignment.center”。
Stack(
alignment: Alignment.center,
children: [])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.