英文:
How to get this UI in flutter?
问题
I have been trying to generate a sort of Podium in leaderboards page, the idea is that the number one on the LeaderBoards will have a circleAvatar with a crown on top, the other two should be a little bit down the first. One on the left, the other on the right, both with a slightly smaller CircleAvatar.
我一直在尝试在排行榜页面生成一种领奖台,想法是排行榜上的第一名会有一个带有皇冠的CircleAvatar,另外两个应该在第一名的下方,一个在左边,另一个在右边,都带有稍小一点的CircleAvatar。
But this is what I want it to look like.
但这是我想要的样子。
I don't want to hardcode any numbers, like sizes of Containers or anything of the sort. I have tried different options, with Align, Spacer, using Stack, but so far I always find some problem to get to the final result. I wonder what is the best approach here to what I want to accomplish. Is there maybe another Widget that I am not considering for this task?
我不想硬编码任何数字,比如容器的大小或任何其他东西。我尝试了不同的选项,包括Align、Spacer、使用Stack,但到目前为止,我总是遇到一些问题,无法得到最终的结果。我想知道在这里实现我想要的目标的最佳方法是什么。也许还有其他的Widget我没有考虑到吗?
I made the #2 and #3 places smaller by using Transform.scale on the CustomWidget.
我通过在CustomWidget上使用Transform.scale来使第2和第3名的位置变小。
PODIUM AVATAR WIDGET CODE.
领奖台头像小部件代码。
PODIUM WIDGET (This is where I want to arrange the 3 items)
领奖台小部件(这是我想要排列3个项目的地方)。
英文:
I have been trying to generate a sort of Podium in leaderboards page, the idea is that the number one on the LeaderBoaards will have a circleAvatar with a crown on top, the other two should be a little bit down the first. One on the left, the other on the right, both with a slightly smaller CircleAvatar.
I have done it in a Row. And this is what it looks like
But this is what I want it to look like.
(edited in Photoshop)
I don't want to hardcode any numbers, like sizes of Containers or anything of the sort. I have tried different options, with Align, Spacer, using Stack, but so far I always find some problem to get to the final result. I wonder what is the best approach here to what I want to accomplish. Is there maybe another Widget that I am not considering for this task?
I made the #2 and #3 places smaller by using Transform.scale on the CustomWidget.
PODIUM AVATAR WIDGET CODE.
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
class PodiumAvatar extends StatelessWidget {
final String profileImage;
final int rank;
const PodiumAvatar(
{required this.profileImage, required this.rank, super.key});
@override
Widget build(BuildContext context) {
return Container(
height: 150,
width: 110,
child: Stack(children: [
const Positioned(
top: 25,
child: CircleAvatar(
backgroundColor: Colors.amber,
radius: 55,
child: CircleAvatar(
foregroundImage: AssetImage(
'assets/images/profile_1.jpg',
),
radius: 50,
),
),
),
Positioned(
left: 42,
bottom: 10,
child: CircleAvatar(
backgroundColor: Colors.amberAccent,
radius: 12,
child: Text(rank.toString()),
)),
if (rank == 1)
Positioned(
left: 38,
top: -3,
child: SvgPicture.asset('assets/images/crown1.svg', height: 32))
]));
}
}
PODIUM WIDGET (This is where I want to arrange the 3 items)
import 'package:flutter/material.dart';
import 'package:trivia_app/widgets/podium_avatar.dart';
class Podium extends StatelessWidget {
final String profileImage;
final int rank1;
const Podium({required this.profileImage, required this.rank1, super.key});
@override
Widget build(BuildContext context) {
return Container(
color: Colors.lightBlue,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end, //add this line
mainAxisAlignment: MainAxisAlignment.center,
children: [
Transform.scale(
scale: 0.65,
child: PodiumAvatar(profileImage: profileImage, rank: rank1)),
PodiumAvatar(profileImage: profileImage, rank: rank1),
Transform.scale(
scale: 0.65,
child: PodiumAvatar(profileImage: profileImage, rank: rank1)),
],
),
);
}
}
答案1
得分: 1
Row需要的crossAxisAlignment属性:
Row(
crossAxisAlignment: CrossAxisAlignment.end, //添加这一行
mainAxisAlignment: MainAxisAlignment.center,
children: const [
CircleAvatar(radius: 50,),
CircleAvatar(radius: 75,),
CircleAvatar(radius: 50,),
],
),
<details>
<summary>英文:</summary>
Row needs crossAxisAlignment:
Row(
crossAxisAlignment: CrossAxisAlignment.end, //add this line
mainAxisAlignment: MainAxisAlignment.center,
children: const [
CircleAvatar(radius: 50,),
CircleAvatar(radius: 75,),
CircleAvatar(radius: 50,),
],
),
[![demo][1]][1]
[1]: https://i.stack.imgur.com/E0OUJ.png
</details>
# 答案2
**得分**: 1
以下是您要翻译的内容:
我认为这可以解决您的问题,附上了来自Dart Pad的截图
```dart
Container(
padding: EdgeInsets.only(top: 15, bottom: 5),
color: Colors.redAccent,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end, //add this line
mainAxisAlignment: MainAxisAlignment.center,
children: const [
CircleAvatar(radius: 40,),
SizedBox(width: 30),
Padding(
padding: EdgeInsets.only(bottom: 10),
child: CircleAvatar(radius: 75,),
),
SizedBox(width: 30),
CircleAvatar(radius: 40,),
],
)
)
英文:
I think this solves your problem a screenshot from dart pad is attached
Container(
padding:EdgeInsets.only(top:15,bottom:5),
color:Colors.redAccent,
child:Row(
crossAxisAlignment: CrossAxisAlignment.end, //add this line
mainAxisAlignment: MainAxisAlignment.center,
children: const [
CircleAvatar(radius: 40,),
SizedBox(width:30),
Padding(
padding:EdgeInsets.only(bottom:10),
child: CircleAvatar(radius: 75,),
),
SizedBox(width:30),
CircleAvatar(radius: 40,),
],
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论