fix(mobile): 简化火柴人位置计算逻辑
使用简单直接的比例计算: - 起点 = 昵称区域右边 (70px) - 终点 = 红旗左边 (containerWidth - 62px) - 火柴人位置 = 起点 + (终点 - 起点) * 进度 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
71304dbb69
commit
3e59d56f92
|
|
@ -183,159 +183,150 @@ class _StickmanRaceWidgetState extends State<StickmanRaceWidget>
|
||||||
|
|
||||||
return SizedBox(
|
return SizedBox(
|
||||||
height: raceTrackHeight,
|
height: raceTrackHeight,
|
||||||
child: Stack(
|
child: LayoutBuilder(
|
||||||
children: [
|
builder: (context, constraints) {
|
||||||
// 背景赛道线
|
final containerWidth = constraints.maxWidth;
|
||||||
Positioned.fill(
|
|
||||||
child: CustomPaint(
|
|
||||||
painter: _TrackPainter(
|
|
||||||
trackCount: sortedRankings.length,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
|
|
||||||
// 每个跑道的终点红旗
|
return Stack(
|
||||||
...List.generate(trackCount, (index) {
|
clipBehavior: Clip.none,
|
||||||
final verticalPosition = index * trackHeight + 10 + trackHeight / 2 - 16;
|
children: [
|
||||||
return Positioned(
|
// 背景赛道线
|
||||||
right: 8,
|
Positioned.fill(
|
||||||
top: verticalPosition,
|
child: CustomPaint(
|
||||||
child: const Icon(
|
painter: _TrackPainter(
|
||||||
Icons.flag,
|
trackCount: sortedRankings.length,
|
||||||
color: Colors.red,
|
),
|
||||||
size: 24,
|
),
|
||||||
),
|
),
|
||||||
);
|
|
||||||
}),
|
|
||||||
|
|
||||||
// 火柴人们
|
// 每个跑道的终点红旗
|
||||||
...sortedRankings.asMap().entries.map((entry) {
|
...List.generate(trackCount, (index) {
|
||||||
final index = entry.key;
|
final verticalPosition = index * trackHeight + 10 + trackHeight / 2 - 16;
|
||||||
final data = entry.value;
|
return Positioned(
|
||||||
return _buildStickman(data, index, sortedRankings.length, raceTrackHeight);
|
right: 8,
|
||||||
}),
|
top: verticalPosition,
|
||||||
],
|
child: const Icon(
|
||||||
|
Icons.flag,
|
||||||
|
color: Colors.red,
|
||||||
|
size: 24,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
|
||||||
|
// 火柴人们
|
||||||
|
...sortedRankings.asMap().entries.map((entry) {
|
||||||
|
final index = entry.key;
|
||||||
|
final data = entry.value;
|
||||||
|
return _buildStickman(data, index, sortedRankings.length, raceTrackHeight, containerWidth);
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 构建单个火柴人
|
/// 构建单个火柴人
|
||||||
Widget _buildStickman(StickmanRankingData data, int rank, int total, double raceTrackHeight) {
|
Widget _buildStickman(StickmanRankingData data, int rank, int total, double raceTrackHeight, double containerWidth) {
|
||||||
// 计算水平位置 (根据进度)
|
// 进度 (0.0 - 1.0)
|
||||||
final horizontalProgress = data.progress;
|
final progress = data.progress;
|
||||||
|
|
||||||
// 计算垂直位置 (不同排名在不同跑道)
|
// 计算垂直位置 (不同排名在不同跑道)
|
||||||
// 预留顶部和底部空间
|
|
||||||
final usableHeight = raceTrackHeight - 40;
|
final usableHeight = raceTrackHeight - 40;
|
||||||
final trackHeight = usableHeight / total;
|
final trackHeight = usableHeight / total;
|
||||||
final verticalPosition = rank * trackHeight + 10;
|
final verticalPosition = rank * trackHeight + 10;
|
||||||
|
|
||||||
|
// 简单直接的计算:
|
||||||
|
// 起点:昵称区域右边 (70)
|
||||||
|
// 终点:红旗左边 (containerWidth - 8(红旗right) - 24(红旗宽) - 30(火柴人半宽))
|
||||||
|
// 火柴人中心位置 = 起点 + (终点 - 起点) * 进度
|
||||||
|
const double startX = 70.0;
|
||||||
|
final double endX = containerWidth - 62.0;
|
||||||
|
final double stickmanCenterX = startX + (endX - startX) * progress;
|
||||||
|
|
||||||
return AnimatedBuilder(
|
return AnimatedBuilder(
|
||||||
animation: _bounceController,
|
animation: _bounceController,
|
||||||
builder: (context, child) {
|
builder: (context, child) {
|
||||||
// 添加上下弹跳效果
|
|
||||||
final bounce = _bounceController.value * 3;
|
final bounce = _bounceController.value * 3;
|
||||||
|
|
||||||
// 计算可用宽度,让火柴人在100%时正好到达红旗位置
|
return Stack(
|
||||||
final screenWidth = MediaQuery.of(context).size.width;
|
clipBehavior: Clip.none,
|
||||||
final containerWidth = screenWidth - 32 - 32; // 页面padding + 容器padding
|
children: [
|
||||||
final stickmanColumnWidth = 60.0; // 火柴人+数量标签列的宽度
|
// 昵称标签 - 固定在左边
|
||||||
final flagRightMargin = 8.0; // 红旗的right值
|
Positioned(
|
||||||
final flagWidth = 24.0; // 红旗图标宽度
|
left: 0,
|
||||||
final nicknameAreaWidth = 65.0; // 昵称区域宽度
|
top: verticalPosition + 15 - bounce,
|
||||||
final gap = 4.0; // 昵称和火柴人之间的最小间距
|
child: SizedBox(
|
||||||
|
width: 65,
|
||||||
// 100%时火柴人列的右边缘应该对齐红旗左边缘
|
child: Container(
|
||||||
// 红旗左边位置 = containerWidth - flagRightMargin - flagWidth
|
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
||||||
// 火柴人列右边 = stickmanLeftPosition + stickmanColumnWidth
|
decoration: BoxDecoration(
|
||||||
// 所以 100% 时: stickmanLeftPosition = containerWidth - flagRightMargin - flagWidth - stickmanColumnWidth
|
color: data.isCurrentUser
|
||||||
final endPosition = containerWidth - flagRightMargin - flagWidth - stickmanColumnWidth;
|
? const Color(0xFFD4AF37).withValues(alpha: 0.2)
|
||||||
final startPosition = nicknameAreaWidth + gap;
|
: Colors.white.withValues(alpha: 0.8),
|
||||||
final availableWidth = endPosition - startPosition;
|
borderRadius: BorderRadius.circular(8),
|
||||||
final stickmanLeftPosition = startPosition + availableWidth * horizontalProgress;
|
border: data.isCurrentUser
|
||||||
|
? Border.all(color: const Color(0xFFD4AF37), width: 1)
|
||||||
return Positioned(
|
: null,
|
||||||
left: 0,
|
|
||||||
top: verticalPosition - bounce,
|
|
||||||
child: SizedBox(
|
|
||||||
width: containerWidth,
|
|
||||||
child: Row(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
// 昵称标签 - 固定在左边,与数量标签同一水平高度
|
|
||||||
SizedBox(
|
|
||||||
width: nicknameAreaWidth,
|
|
||||||
child: Container(
|
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: data.isCurrentUser
|
|
||||||
? const Color(0xFFD4AF37).withValues(alpha: 0.2)
|
|
||||||
: Colors.white.withValues(alpha: 0.8),
|
|
||||||
borderRadius: BorderRadius.circular(8),
|
|
||||||
border: data.isCurrentUser
|
|
||||||
? Border.all(color: const Color(0xFFD4AF37), width: 1)
|
|
||||||
: null,
|
|
||||||
),
|
|
||||||
child: Text(
|
|
||||||
data.nickname,
|
|
||||||
style: TextStyle(
|
|
||||||
fontSize: 9,
|
|
||||||
fontFamily: 'Inter',
|
|
||||||
fontWeight:
|
|
||||||
data.isCurrentUser ? FontWeight.w600 : FontWeight.w400,
|
|
||||||
color: data.isCurrentUser
|
|
||||||
? const Color(0xFFD4AF37)
|
|
||||||
: const Color(0xFF5D4037),
|
|
||||||
),
|
|
||||||
overflow: TextOverflow.ellipsis,
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
child: Text(
|
||||||
// 间隔 + 火柴人位置
|
data.nickname,
|
||||||
SizedBox(
|
style: TextStyle(
|
||||||
width: stickmanLeftPosition - nicknameAreaWidth,
|
fontSize: 9,
|
||||||
),
|
fontFamily: 'Inter',
|
||||||
// 火柴人和数量标签
|
fontWeight: data.isCurrentUser ? FontWeight.w600 : FontWeight.w400,
|
||||||
Column(
|
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
children: [
|
|
||||||
// 完成数量标签
|
|
||||||
Container(
|
|
||||||
constraints: const BoxConstraints(maxWidth: 60),
|
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: data.isCurrentUser
|
|
||||||
? const Color(0xFFD4AF37)
|
|
||||||
: const Color(0xFF8B5A2B),
|
|
||||||
borderRadius: BorderRadius.circular(8),
|
|
||||||
),
|
|
||||||
child: FittedBox(
|
|
||||||
fit: BoxFit.scaleDown,
|
|
||||||
child: Text(
|
|
||||||
'${_formatNumber(data.completedCount)}棵',
|
|
||||||
style: const TextStyle(
|
|
||||||
fontSize: 9,
|
|
||||||
fontFamily: 'Inter',
|
|
||||||
fontWeight: FontWeight.w600,
|
|
||||||
color: Colors.white,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 2),
|
|
||||||
// 火柴人动画
|
|
||||||
RunningStickman(
|
|
||||||
size: 36,
|
|
||||||
color: data.isCurrentUser
|
color: data.isCurrentUser
|
||||||
? const Color(0xFFD4AF37)
|
? const Color(0xFFD4AF37)
|
||||||
: const Color(0xFF5D4037),
|
: const Color(0xFF5D4037),
|
||||||
),
|
),
|
||||||
],
|
overflow: TextOverflow.ellipsis,
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
),
|
||||||
),
|
),
|
||||||
),
|
// 火柴人和数量标签
|
||||||
|
Positioned(
|
||||||
|
left: stickmanCenterX - 30, // 火柴人宽度60的一半
|
||||||
|
top: verticalPosition - bounce,
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
// 完成数量标签
|
||||||
|
Container(
|
||||||
|
constraints: const BoxConstraints(maxWidth: 60),
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: data.isCurrentUser
|
||||||
|
? const Color(0xFFD4AF37)
|
||||||
|
: const Color(0xFF8B5A2B),
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
),
|
||||||
|
child: FittedBox(
|
||||||
|
fit: BoxFit.scaleDown,
|
||||||
|
child: Text(
|
||||||
|
'${_formatNumber(data.completedCount)}棵',
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 9,
|
||||||
|
fontFamily: 'Inter',
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 2),
|
||||||
|
// 火柴人动画
|
||||||
|
RunningStickman(
|
||||||
|
size: 36,
|
||||||
|
color: data.isCurrentUser
|
||||||
|
? const Color(0xFFD4AF37)
|
||||||
|
: const Color(0xFF5D4037),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue