refactor: all screens refactor

This commit is contained in:
2026-07-08 20:12:04 +03:30
parent 2496580e86
commit adc2d1fc47
16 changed files with 3546 additions and 3351 deletions
@@ -26,6 +26,8 @@ import '../widgets/confetti.dart';
import '../widgets/flame/hokm_game.dart';
import '../widgets/table_hud.dart';
part 'game_screen.parts.dart';
/// صفحه‌ی میز بازی: صحنه‌ی Flame + اوورلی‌های وضعیت.
class GameScreen extends StatefulWidget {
final int prize;
@@ -158,10 +160,11 @@ class _GameScreenState extends State<GameScreen> {
onExit: () => _confirmLeave(context),
onChat: () => _openChat(context),
),
if (state.connection == WsStatus.disconnected) _connBanner(),
if (state.connection == WsStatus.disconnected) const _ConnBanner(),
if (_tournament != null && !_showSearch(state))
_tournamentBadge(),
if (_showSearch(state)) _searchPanel(state),
_TournamentBadge(title: _tournament!.title),
if (_showSearch(state))
_SearchPanel(state: state, countdown: _countdown),
// دیالوگِ انتخابِ حکم و بنرِ نوبت فقط پس از پایانِ انیمیشنِ
// پخشِ کارت نمایش داده می‌شوند (نه وسطِ بُر زدن/پخش).
if (!_showSearch(state))
@@ -171,15 +174,21 @@ class _GameScreenState extends State<GameScreen> {
if (animating) return const SizedBox.shrink();
return Stack(
children: [
if (_showTrumpPicker(state)) _trumpPicker(context),
if (_isMyPlayTurn(state)) _turnBanner(),
if (_showTrumpPicker(state)) const _TrumpPicker(),
if (_isMyPlayTurn(state)) const _TurnBanner(),
],
);
},
),
if (state.handResult != null && state.gameOver == null)
_handResult(state),
if (state.gameOver != null) _gameOver(context, state),
_HandResult(state: state),
if (state.gameOver != null)
_GameOver(
state: state,
prize: widget.prize,
tournamentTitle: _tournament?.title,
onExit: () => _exitToLobby(context),
),
],
);
},
@@ -207,31 +216,6 @@ class _GameScreenState extends State<GameScreen> {
s.state!.isMyTurn &&
!s.state!.trickDone;
// بنرِ «نوبت شماست» وقتی کاربر باید کارت بیندازد.
Widget _turnBanner() => Align(
alignment: const Alignment(0, 0.3),
child: IgnorePointer(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 8),
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [AppColors.gold, AppColors.goldDark],
),
borderRadius: BorderRadius.circular(20),
boxShadow: const [BoxShadow(color: Colors.black54, blurRadius: 8)],
),
child: const Text(
'نوبت شماست',
style: TextStyle(
color: AppColors.bg,
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
),
),
);
bool _showTrumpPicker(GameUiState s) =>
s.state != null &&
s.state!.phase == 'choose_trump' &&
@@ -276,540 +260,4 @@ class _GameScreenState extends State<GameScreen> {
context.go('/lobby');
}
Widget _connBanner() => Positioned(
top: 0,
left: 0,
right: 0,
child: Material(
color: Colors.orange.shade900,
child: const SafeArea(
bottom: false,
child: Padding(
padding: EdgeInsets.all(8),
child: Text(
'ارتباط با سرور قطع شد، در حال تلاش برای اتصال مجدد…',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
),
),
),
);
Widget _searchPanel(GameUiState state) {
final players = state.state?.players ?? const <GamePlayer>[];
final mySeat = state.state?.yourSeat ?? -1;
final searching = state.state == null;
GamePlayer? at(int seat) {
for (final p in players) {
if (p.seat == seat) return p;
}
return null;
}
return Container(
color: Colors.black.withValues(alpha: 0.78),
alignment: Alignment.center,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 20),
padding: const EdgeInsets.fromLTRB(16, 14, 16, 18),
decoration: BoxDecoration(
color: AppColors.panel,
borderRadius: BorderRadius.circular(20),
border: Border.all(color: AppColors.gold, width: 2.5),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'جستجوی حریف',
style: TextStyle(
color: AppColors.gold,
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 18),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var seat = 0; seat < 4; seat++)
_searchSlot(at(seat), seat == mySeat, seat),
],
),
if (searching) ...[
const SizedBox(height: 16),
const Text(
'در حال یافتن حریفان…',
style: TextStyle(color: Colors.white70, fontSize: 13),
),
] else if (_countdown > 0) ...[
const SizedBox(height: 14),
const Text(
'شروع بازی',
style: TextStyle(color: Colors.white70, fontSize: 13),
),
const SizedBox(height: 6),
TweenAnimationBuilder<double>(
// هر عدد با یک «پاپ» ظاهر می‌شود تا واضح و جلب‌توجه باشد.
key: ValueKey(_countdown),
tween: Tween(begin: 1.6, end: 1.0),
duration: const Duration(milliseconds: 400),
curve: Curves.easeOut,
builder: (_, scale, child) =>
Transform.scale(scale: scale, child: child),
child: Container(
width: 54,
height: 54,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: const LinearGradient(
colors: [AppColors.gold, AppColors.goldDark],
),
border: Border.all(color: Colors.white, width: 2),
),
child: Text(
'$_countdown',
style: const TextStyle(
color: AppColors.bg,
fontSize: 26,
fontWeight: FontWeight.bold,
),
),
),
),
],
],
),
),
);
}
Widget _searchSlot(GamePlayer? p, bool isYou, int seat) {
final found = p != null;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 6),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
found ? p.name : '...',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: isYou ? AppColors.gold : Colors.white70,
fontSize: 12,
),
),
const SizedBox(height: 4),
Container(
width: 58,
height: 58,
padding: const EdgeInsets.all(3),
decoration: BoxDecoration(
color: AppColors.bgDark,
borderRadius: BorderRadius.circular(10),
border: Border.all(
color: isYou ? AppColors.gold : AppColors.goldDark,
width: 1.5,
),
),
child:
!found
? _SearchingAvatar(slot: seat) // آواتارهای متغیر و متفاوت
: p.bot
? const Icon(
Icons.smart_toy,
color: AppColors.gold,
size: 30,
)
: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: RandomAvatar(p.avatarSeed),
),
),
const SizedBox(height: 2),
Text(
found ? (p.bot ? 'ربات' : (isYou ? 'شما' : 'حریف')) : '',
style: const TextStyle(color: Colors.white38, fontSize: 10),
),
],
),
);
}
Widget _trumpPicker(BuildContext context) {
const suits = [
('spades', '', 'پیک', Colors.white),
('hearts', '', 'دل', AppColors.suitRed),
('diamonds', '', 'خشت', AppColors.suitRed),
('clubs', '', 'گشنیز', Colors.white),
];
return Container(
color: Colors.black54,
alignment: Alignment.center,
child: Container(
margin: const EdgeInsets.all(24),
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: AppColors.panel,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: AppColors.gold, width: 2),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'حکم را انتخاب کن',
style: TextStyle(color: AppColors.gold, fontSize: 20),
),
const SizedBox(height: 16),
Wrap(
spacing: 12,
runSpacing: 12,
alignment: WrapAlignment.center,
children: [
for (final (id, sym, name, color) in suits)
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.bgDark,
minimumSize: const Size(120, 64),
),
onPressed: () => context.read<GameBloc>().chooseTrump(id),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(sym, style: TextStyle(fontSize: 26, color: color)),
const SizedBox(width: 8),
Text(
name,
style: const TextStyle(color: AppColors.text),
),
],
),
),
],
),
const SizedBox(height: 16),
const Divider(color: AppColors.goldFaint, height: 1),
const SizedBox(height: 12),
// بُرِ مجدد با مصرفِ یک بلیت (۵ کارتِ تازه، سپس باز هم انتخابِ حکم).
BlocBuilder<WalletBloc, WalletBlocState>(
builder: (context, ws) {
final tickets = ws.walletStatus is WalletLoaded
? (ws.walletStatus as WalletLoaded).wallet.tickets
: 0;
final canReshuffle = tickets > 0;
return Column(
children: [
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF8E1B7A),
disabledBackgroundColor: const Color(0xFF3A2233),
minimumSize: const Size(220, 48),
),
onPressed: canReshuffle
? () {
AppSounds.button();
context.read<GameBloc>().reshuffle();
// پس از مصرفِ بلیت، موجودی را تازه کن (بدون نگه‌داشتنِ context).
final wallet = context.read<WalletBloc>();
Future.delayed(const Duration(milliseconds: 500),
() => wallet.add(LoadWalletEvent()));
}
: null,
icon: const Icon(Icons.casino, color: Colors.white),
label: const Text(
'بُر مجدد (۱ بلیت)',
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
const SizedBox(height: 4),
Text(
canReshuffle
? 'بلیت‌های شما: $tickets'
: 'بلیت کافی ندارید',
style: const TextStyle(
color: Colors.white60, fontSize: 12),
),
],
);
},
),
],
),
),
);
}
Widget _handResult(GameUiState s) {
final r = s.handResult!;
final mySeat = s.state?.yourSeat ?? 0;
final myTeam = mySeat % 2;
final won = r.winnerTeam == myTeam;
final kotWin = won && r.kot; // جشنِ ویژه فقط برای کُتِ بُرده
return IgnorePointer(
child: Stack(
children: [
if (kotWin) const Positioned.fill(child: Confetti(count: 40)),
Center(
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 28, vertical: 18),
decoration: BoxDecoration(
gradient: kotWin
? const LinearGradient(
colors: [AppColors.lapisMid, AppColors.lapisInk])
: null,
color: kotWin ? null : Colors.black87,
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: AppColors.gold, width: kotWin ? 2 : 1),
boxShadow: kotWin
? [
BoxShadow(
color: AppColors.gold.withValues(alpha: 0.4),
blurRadius: 20),
]
: null,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (kotWin)
Text(
r.hakemKot ? 'حاکم‌کُت! 🔥' : 'کُت! 🔥',
style: const TextStyle(
color: AppColors.gold,
fontSize: 30,
fontWeight: FontWeight.bold,
shadows: [Shadow(color: Colors.black, blurRadius: 8)],
),
),
Text(
won ? 'این دست را بردید!' : 'این دست را باختید',
style: TextStyle(
color: won ? AppColors.green : Colors.redAccent,
fontSize: kotWin ? 16 : 20,
fontWeight: FontWeight.bold,
),
),
if (r.kot && !kotWin)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Text(
r.hakemKot
? 'حاکم‌کُت! (${r.points} امتیاز)'
: 'کُت! (${r.points} امتیاز)',
style: const TextStyle(color: AppColors.gold),
),
),
if (kotWin)
Text('+${r.points} امتیاز',
style: const TextStyle(color: AppColors.gold)),
const SizedBox(height: 6),
Text(
'امتیاز: ${r.scores.isNotEmpty ? r.scores[0] : 0} - ${r.scores.length > 1 ? r.scores[1] : 0}',
style: const TextStyle(color: Colors.white70),
),
],
),
),
),
],
),
);
}
// نشانِ درون‌بازیِ تورنومنت: به کاربر می‌فهماند این بازیِ عمومی به تورنومنت
// امتیاز می‌دهد (بالای صحنه، وسط).
Widget _tournamentBadge() => Positioned(
top: 6,
left: 0,
right: 0,
child: IgnorePointer(
child: Center(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Color(0xFF3A2A6E), AppColors.lapisInk],
),
borderRadius: BorderRadius.circular(20),
border: Border.all(color: AppColors.gold, width: 1.2),
boxShadow: [
BoxShadow(
color: AppColors.gold.withValues(alpha: 0.3),
blurRadius: 8),
],
),
child: Row(mainAxisSize: MainAxisSize.min, children: [
const Icon(Icons.emoji_events, color: AppColors.gold, size: 15),
const SizedBox(width: 5),
Text(
'تورنومنت: ${_tournament!.title}',
style: const TextStyle(
color: AppColors.gold,
fontSize: 12,
fontWeight: FontWeight.bold),
),
]),
),
),
),
);
Widget _gameOver(BuildContext context, GameUiState s) {
final g = s.gameOver!;
final mySeat = s.state?.yourSeat ?? 0;
final won = g.winnerTeam == mySeat % 2;
return Stack(
children: [
const Positioned.fill(child: ColoredBox(color: Colors.black87)),
if (won) const Positioned.fill(child: Confetti()),
Center(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// مدالِ متحرک (تاجِ طلایی برای برد، نشانِ ملایم برای باخت).
TweenAnimationBuilder<double>(
tween: Tween(begin: 0, end: 1),
duration: const Duration(milliseconds: 650),
curve: Curves.easeOutBack,
builder: (_, v, __) => Transform.scale(
scale: v.clamp(0.0, 1.2),
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
colors: won
? const [AppColors.gold, AppColors.goldDark]
: [Colors.grey.shade700, Colors.grey.shade900],
),
border: Border.all(
color: won ? Colors.white70 : Colors.white24,
width: 2),
boxShadow: won
? [
BoxShadow(
color: AppColors.gold
.withValues(alpha: 0.55),
blurRadius: 28),
]
: null,
),
child: Icon(
won
? Icons.emoji_events
: Icons.sentiment_dissatisfied_outlined,
color: won ? AppColors.lapisInk : Colors.white54,
size: 54,
),
),
),
),
const SizedBox(height: 16),
Text(
won ? 'بردید! 🎉' : 'باختید',
style: TextStyle(
color: won ? AppColors.gold : Colors.redAccent,
fontSize: 34,
fontWeight: FontWeight.bold,
shadows: const [Shadow(color: Colors.black, blurRadius: 8)],
),
),
const SizedBox(height: 10),
Text(
'نتیجه نهایی: ${g.scores.isNotEmpty ? g.scores[0] : 0} - ${g.scores.length > 1 ? g.scores[1] : 0}',
style: const TextStyle(color: Colors.white70, fontSize: 18),
),
// پاداشِ سکه‌ی برد (جایزه‌ی میز).
if (won && widget.prize > 0) ...[
const SizedBox(height: 16),
_rewardChip(
Icons.monetization_on, '+${widget.prize} سکه'),
],
// امتیازِ کسب‌شده‌ی تورنومنت (برد ۱۰۰، شرکت ۲۵).
if (_tournament != null) ...[
const SizedBox(height: 12),
_rewardChip(Icons.emoji_events,
'+${won ? 100 : 25} امتیاز تورنومنت «${_tournament!.title}»'),
],
const SizedBox(height: 30),
SizedBox(
width: 220,
child: ElevatedButton(
onPressed: () => _exitToLobby(context),
child: const Text('بازگشت به لابی'),
),
),
],
),
),
),
],
);
}
Widget _rewardChip(IconData icon, String text) => Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
decoration: BoxDecoration(
color: AppColors.gold.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(14),
border: Border.all(color: AppColors.gold),
),
child: Row(mainAxisSize: MainAxisSize.min, children: [
Icon(icon, color: AppColors.gold, size: 20),
const SizedBox(width: 8),
Text(text,
style: const TextStyle(
color: AppColors.gold,
fontSize: 14,
fontWeight: FontWeight.bold)),
]),
);
}
/// آواتارِ در حالِ تغییر برای جایگاه‌های خالیِ پنل جستجو (حسِ «در حال یافتن»).
class _SearchingAvatar extends StatefulWidget {
final int slot; // هر جایگاه دامنه‌ی seedِ متفاوت دارد تا یکسان نباشند
const _SearchingAvatar({required this.slot});
@override
State<_SearchingAvatar> createState() => _SearchingAvatarState();
}
class _SearchingAvatarState extends State<_SearchingAvatar> {
Timer? _timer;
int _i = 0;
@override
void initState() {
super.initState();
_i = widget.slot * 13; // شروعِ متفاوت برای هر جایگاه
_timer = Timer.periodic(const Duration(milliseconds: 240), (_) {
if (mounted) setState(() => _i++);
});
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
// پیشوندِ مخصوصِ جایگاه ⇒ آواتارِ هر جایگاه با بقیه فرق دارد.
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: RandomAvatar('s${widget.slot}-$_i'),
);
}
}
@@ -0,0 +1,639 @@
// اوورلی‌های خصوصیِ صفحه‌ی بازی (بنرها، پنلِ جستجو، انتخابِ حکم، نتیجه‌ی دست/بازی).
part of 'game_screen.dart';
/// بنرِ «نوبت شماست» وقتی کاربر باید کارت بیندازد.
class _TurnBanner extends StatelessWidget {
const _TurnBanner();
@override
Widget build(BuildContext context) => Align(
alignment: const Alignment(0, 0.3),
child: IgnorePointer(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 8),
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [AppColors.gold, AppColors.goldDark],
),
borderRadius: BorderRadius.circular(20),
boxShadow: const [
BoxShadow(color: Colors.black54, blurRadius: 8)
],
),
child: const Text(
'نوبت شماست',
style: TextStyle(
color: AppColors.bg,
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
),
),
);
}
/// بنرِ قطعِ ارتباط با سرور (بالای صفحه).
class _ConnBanner extends StatelessWidget {
const _ConnBanner();
@override
Widget build(BuildContext context) => Positioned(
top: 0,
left: 0,
right: 0,
child: Material(
color: Colors.orange.shade900,
child: const SafeArea(
bottom: false,
child: Padding(
padding: EdgeInsets.all(8),
child: Text(
'ارتباط با سرور قطع شد، در حال تلاش برای اتصال مجدد…',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
/// نشانِ درون‌بازیِ تورنومنت: می‌فهماند این بازیِ عمومی به تورنومنت امتیاز می‌دهد.
class _TournamentBadge extends StatelessWidget {
final String title;
const _TournamentBadge({required this.title});
@override
Widget build(BuildContext context) => Positioned(
top: 6,
left: 0,
right: 0,
child: IgnorePointer(
child: Center(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Color(0xFF3A2A6E), AppColors.lapisInk],
),
borderRadius: BorderRadius.circular(20),
border: Border.all(color: AppColors.gold, width: 1.2),
boxShadow: [
BoxShadow(
color: AppColors.gold.withValues(alpha: 0.3),
blurRadius: 8),
],
),
child: Row(mainAxisSize: MainAxisSize.min, children: [
const Icon(Icons.emoji_events, color: AppColors.gold, size: 15),
const SizedBox(width: 5),
Text(
'تورنومنت: $title',
style: const TextStyle(
color: AppColors.gold,
fontSize: 12,
fontWeight: FontWeight.bold),
),
]),
),
),
),
);
}
/// پنلِ «جستجوی حریف»: چهار جایگاه + وضعیتِ جستجو یا شمارشِ معکوسِ شروع.
class _SearchPanel extends StatelessWidget {
final GameUiState state;
final int countdown;
const _SearchPanel({required this.state, required this.countdown});
@override
Widget build(BuildContext context) {
final players = state.state?.players ?? const <GamePlayer>[];
final mySeat = state.state?.yourSeat ?? -1;
final searching = state.state == null;
GamePlayer? at(int seat) {
for (final p in players) {
if (p.seat == seat) return p;
}
return null;
}
return Container(
color: Colors.black.withValues(alpha: 0.78),
alignment: Alignment.center,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 20),
padding: const EdgeInsets.fromLTRB(16, 14, 16, 18),
decoration: BoxDecoration(
color: AppColors.panel,
borderRadius: BorderRadius.circular(20),
border: Border.all(color: AppColors.gold, width: 2.5),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'جستجوی حریف',
style: TextStyle(
color: AppColors.gold,
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 18),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var seat = 0; seat < 4; seat++)
_SearchSlot(
player: at(seat),
isYou: seat == mySeat,
seat: seat,
),
],
),
if (searching) ...[
const SizedBox(height: 16),
const Text(
'در حال یافتن حریفان…',
style: TextStyle(color: Colors.white70, fontSize: 13),
),
] else if (countdown > 0) ...[
const SizedBox(height: 14),
const Text(
'شروع بازی',
style: TextStyle(color: Colors.white70, fontSize: 13),
),
const SizedBox(height: 6),
TweenAnimationBuilder<double>(
// هر عدد با یک «پاپ» ظاهر می‌شود تا واضح و جلب‌توجه باشد.
key: ValueKey(countdown),
tween: Tween(begin: 1.6, end: 1.0),
duration: const Duration(milliseconds: 400),
curve: Curves.easeOut,
builder: (_, scale, child) =>
Transform.scale(scale: scale, child: child),
child: Container(
width: 54,
height: 54,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: const LinearGradient(
colors: [AppColors.gold, AppColors.goldDark],
),
border: Border.all(color: Colors.white, width: 2),
),
child: Text(
'$countdown',
style: const TextStyle(
color: AppColors.bg,
fontSize: 26,
fontWeight: FontWeight.bold,
),
),
),
),
],
],
),
),
);
}
}
/// یک جایگاه در پنلِ جستجو (نام، آواتار/آیکن، برچسبِ نقش).
class _SearchSlot extends StatelessWidget {
final GamePlayer? player;
final bool isYou;
final int seat;
const _SearchSlot({
required this.player,
required this.isYou,
required this.seat,
});
@override
Widget build(BuildContext context) {
final p = player;
final found = p != null;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 6),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
found ? p.name : '...',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: isYou ? AppColors.gold : Colors.white70,
fontSize: 12,
),
),
const SizedBox(height: 4),
Container(
width: 58,
height: 58,
padding: const EdgeInsets.all(3),
decoration: BoxDecoration(
color: AppColors.bgDark,
borderRadius: BorderRadius.circular(10),
border: Border.all(
color: isYou ? AppColors.gold : AppColors.goldDark,
width: 1.5,
),
),
child: !found
? _SearchingAvatar(slot: seat) // آواتارهای متغیر و متفاوت
: p.bot
? const Icon(
Icons.smart_toy,
color: AppColors.gold,
size: 30,
)
: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: RandomAvatar(p.avatarSeed),
),
),
const SizedBox(height: 2),
Text(
found ? (p.bot ? 'ربات' : (isYou ? 'شما' : 'حریف')) : '',
style: const TextStyle(color: Colors.white38, fontSize: 10),
),
],
),
);
}
}
/// دیالوگِ انتخابِ حکم توسطِ حاکم (+ بُرِ مجدد با مصرفِ یک بلیت).
class _TrumpPicker extends StatelessWidget {
const _TrumpPicker();
@override
Widget build(BuildContext context) {
const suits = [
('spades', '', 'پیک', Colors.white),
('hearts', '', 'دل', AppColors.suitRed),
('diamonds', '', 'خشت', AppColors.suitRed),
('clubs', '', 'گشنیز', Colors.white),
];
return Container(
color: Colors.black54,
alignment: Alignment.center,
child: Container(
margin: const EdgeInsets.all(24),
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: AppColors.panel,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: AppColors.gold, width: 2),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'حکم را انتخاب کن',
style: TextStyle(color: AppColors.gold, fontSize: 20),
),
const SizedBox(height: 16),
Wrap(
spacing: 12,
runSpacing: 12,
alignment: WrapAlignment.center,
children: [
for (final (id, sym, name, color) in suits)
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.bgDark,
minimumSize: const Size(120, 64),
),
onPressed: () => context.read<GameBloc>().chooseTrump(id),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(sym, style: TextStyle(fontSize: 26, color: color)),
const SizedBox(width: 8),
Text(
name,
style: const TextStyle(color: AppColors.text),
),
],
),
),
],
),
const SizedBox(height: 16),
const Divider(color: AppColors.goldFaint, height: 1),
const SizedBox(height: 12),
// بُرِ مجدد با مصرفِ یک بلیت (۵ کارتِ تازه، سپس باز هم انتخابِ حکم).
BlocBuilder<WalletBloc, WalletBlocState>(
builder: (context, ws) {
final tickets = ws.walletStatus is WalletLoaded
? (ws.walletStatus as WalletLoaded).wallet.tickets
: 0;
final canReshuffle = tickets > 0;
return Column(
children: [
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF8E1B7A),
disabledBackgroundColor: const Color(0xFF3A2233),
minimumSize: const Size(220, 48),
),
onPressed: canReshuffle
? () {
AppSounds.button();
context.read<GameBloc>().reshuffle();
// پس از مصرفِ بلیت، موجودی را تازه کن.
final wallet = context.read<WalletBloc>();
Future.delayed(const Duration(milliseconds: 500),
() => wallet.add(LoadWalletEvent()));
}
: null,
icon: const Icon(Icons.casino, color: Colors.white),
label: const Text(
'بُر مجدد (۱ بلیت)',
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
const SizedBox(height: 4),
Text(
canReshuffle ? 'بلیت‌های شما: $tickets' : 'بلیت کافی ندارید',
style:
const TextStyle(color: Colors.white60, fontSize: 12),
),
],
);
},
),
],
),
),
);
}
}
/// نتیجه‌ی یک دست (بُرد/باخت/کُت) با جشنِ کُتِ بُرده.
class _HandResult extends StatelessWidget {
final GameUiState state;
const _HandResult({required this.state});
@override
Widget build(BuildContext context) {
final r = state.handResult!;
final mySeat = state.state?.yourSeat ?? 0;
final myTeam = mySeat % 2;
final won = r.winnerTeam == myTeam;
final kotWin = won && r.kot; // جشنِ ویژه فقط برای کُتِ بُرده
return IgnorePointer(
child: Stack(
children: [
if (kotWin) const Positioned.fill(child: Confetti(count: 40)),
Center(
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 28, vertical: 18),
decoration: BoxDecoration(
gradient: kotWin
? const LinearGradient(
colors: [AppColors.lapisMid, AppColors.lapisInk])
: null,
color: kotWin ? null : Colors.black87,
borderRadius: BorderRadius.circular(16),
border:
Border.all(color: AppColors.gold, width: kotWin ? 2 : 1),
boxShadow: kotWin
? [
BoxShadow(
color: AppColors.gold.withValues(alpha: 0.4),
blurRadius: 20),
]
: null,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (kotWin)
Text(
r.hakemKot ? 'حاکم‌کُت! 🔥' : 'کُت! 🔥',
style: const TextStyle(
color: AppColors.gold,
fontSize: 30,
fontWeight: FontWeight.bold,
shadows: [Shadow(color: Colors.black, blurRadius: 8)],
),
),
Text(
won ? 'این دست را بردید!' : 'این دست را باختید',
style: TextStyle(
color: won ? AppColors.green : Colors.redAccent,
fontSize: kotWin ? 16 : 20,
fontWeight: FontWeight.bold,
),
),
if (r.kot && !kotWin)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Text(
r.hakemKot
? 'حاکم‌کُت! (${r.points} امتیاز)'
: 'کُت! (${r.points} امتیاز)',
style: const TextStyle(color: AppColors.gold),
),
),
if (kotWin)
Text('+${r.points} امتیاز',
style: const TextStyle(color: AppColors.gold)),
const SizedBox(height: 6),
Text(
'امتیاز: ${r.scores.isNotEmpty ? r.scores[0] : 0} - ${r.scores.length > 1 ? r.scores[1] : 0}',
style: const TextStyle(color: Colors.white70),
),
],
),
),
),
],
),
);
}
}
/// اوورلیِ پایانِ بازی (مدال، نتیجه، پاداش‌ها، بازگشت به لابی).
class _GameOver extends StatelessWidget {
final GameUiState state;
final int prize;
final String? tournamentTitle;
final VoidCallback onExit;
const _GameOver({
required this.state,
required this.prize,
required this.tournamentTitle,
required this.onExit,
});
@override
Widget build(BuildContext context) {
final g = state.gameOver!;
final mySeat = state.state?.yourSeat ?? 0;
final won = g.winnerTeam == mySeat % 2;
return Stack(
children: [
const Positioned.fill(child: ColoredBox(color: Colors.black87)),
if (won) const Positioned.fill(child: Confetti()),
Center(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// مدالِ متحرک (تاجِ طلایی برای برد، نشانِ ملایم برای باخت).
TweenAnimationBuilder<double>(
tween: Tween(begin: 0, end: 1),
duration: const Duration(milliseconds: 650),
curve: Curves.easeOutBack,
builder: (_, v, __) => Transform.scale(
scale: v.clamp(0.0, 1.2),
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
colors: won
? const [AppColors.gold, AppColors.goldDark]
: [Colors.grey.shade700, Colors.grey.shade900],
),
border: Border.all(
color: won ? Colors.white70 : Colors.white24,
width: 2),
boxShadow: won
? [
BoxShadow(
color:
AppColors.gold.withValues(alpha: 0.55),
blurRadius: 28),
]
: null,
),
child: Icon(
won
? Icons.emoji_events
: Icons.sentiment_dissatisfied_outlined,
color: won ? AppColors.lapisInk : Colors.white54,
size: 54,
),
),
),
),
const SizedBox(height: 16),
Text(
won ? 'بردید! 🎉' : 'باختید',
style: TextStyle(
color: won ? AppColors.gold : Colors.redAccent,
fontSize: 34,
fontWeight: FontWeight.bold,
shadows: const [Shadow(color: Colors.black, blurRadius: 8)],
),
),
const SizedBox(height: 10),
Text(
'نتیجه نهایی: ${g.scores.isNotEmpty ? g.scores[0] : 0} - ${g.scores.length > 1 ? g.scores[1] : 0}',
style: const TextStyle(color: Colors.white70, fontSize: 18),
),
// پاداشِ سکه‌ی برد (جایزه‌ی میز).
if (won && prize > 0) ...[
const SizedBox(height: 16),
_RewardChip(
icon: Icons.monetization_on, text: '+$prize سکه'),
],
// امتیازِ کسب‌شده‌ی تورنومنت (برد ۱۰۰، شرکت ۲۵).
if (tournamentTitle != null) ...[
const SizedBox(height: 12),
_RewardChip(
icon: Icons.emoji_events,
text:
'+${won ? 100 : 25} امتیاز تورنومنت «$tournamentTitle»'),
],
const SizedBox(height: 30),
SizedBox(
width: 220,
child: ElevatedButton(
onPressed: onExit,
child: const Text('بازگشت به لابی'),
),
),
],
),
),
),
],
);
}
}
/// چیپِ پاداش (آیکن + متن) در اوورلیِ پایانِ بازی.
class _RewardChip extends StatelessWidget {
final IconData icon;
final String text;
const _RewardChip({required this.icon, required this.text});
@override
Widget build(BuildContext context) => Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
decoration: BoxDecoration(
color: AppColors.gold.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(14),
border: Border.all(color: AppColors.gold),
),
child: Row(mainAxisSize: MainAxisSize.min, children: [
Icon(icon, color: AppColors.gold, size: 20),
const SizedBox(width: 8),
Text(text,
style: const TextStyle(
color: AppColors.gold,
fontSize: 14,
fontWeight: FontWeight.bold)),
]),
);
}
/// آواتارِ در حالِ تغییر برای جایگاه‌های خالیِ پنل جستجو (حسِ «در حال یافتن»).
class _SearchingAvatar extends StatefulWidget {
final int slot; // هر جایگاه دامنه‌ی seedِ متفاوت دارد تا یکسان نباشند
const _SearchingAvatar({required this.slot});
@override
State<_SearchingAvatar> createState() => _SearchingAvatarState();
}
class _SearchingAvatarState extends State<_SearchingAvatar> {
Timer? _timer;
int _i = 0;
@override
void initState() {
super.initState();
_i = widget.slot * 13; // شروعِ متفاوت برای هر جایگاه
_timer = Timer.periodic(const Duration(milliseconds: 240), (_) {
if (mounted) setState(() => _i++);
});
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
// پیشوندِ مخصوصِ جایگاه ⇒ آواتارِ هر جایگاه با بقیه فرق دارد.
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: RandomAvatar('s${widget.slot}-$_i'),
);
}
}
@@ -13,6 +13,8 @@ import '../bloc/game_bloc.dart';
import '../bloc/game_state.dart';
import 'game_screen.dart';
part 'private_table_screen.parts.dart';
/// میز خصوصی: اتاق انتظار (کد، بازیکنان، شروع) سپس صحنه‌ی بازی (روی همان اتصال).
class PrivateTableScreen extends StatelessWidget {
const PrivateTableScreen({super.key});
@@ -48,434 +50,3 @@ class PrivateTableScreen extends StatelessWidget {
);
}
}
class _LobbyView extends StatefulWidget {
final GameUiState state;
const _LobbyView({required this.state});
@override
State<_LobbyView> createState() => _LobbyViewState();
}
class _LobbyViewState extends State<_LobbyView> {
int _hands = 3; // تعداد دستِ انتخابیِ میزبان
void _leave() {
context.read<GameBloc>().leaveTable();
if (context.canPop()) context.pop();
}
@override
Widget build(BuildContext context) {
final state = widget.state;
final lobby = state.lobby;
final connecting = state.connection != WsStatus.connected || lobby == null;
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, _) {
if (!didPop) _leave();
},
child: Scaffold(
body: GameBackground(
child: SafeArea(
child: Stack(
children: [
Column(
children: [
_topBar(context, lobby),
Expanded(
child:
connecting
? const Center(
child: CircularProgressIndicator(
color: AppColors.gold,
),
)
: _content(context, lobby),
),
],
),
if (state.countdown != null)
_CountdownOverlay(seconds: state.countdown!),
],
),
),
),
),
);
}
// نوار بالا: بازگشت (چپ) و اشتراک‌گذاریِ کد (راست).
Widget _topBar(BuildContext context, TableLobby? lobby) {
Widget btn(IconData icon, VoidCallback onTap) => GestureDetector(
onTap: onTap,
child: Container(
width: 46,
height: 46,
decoration: BoxDecoration(
color: AppColors.panel,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: AppColors.gold, width: 1.5),
),
child: Icon(icon, color: AppColors.gold),
),
);
return Padding(
padding: const EdgeInsets.all(10),
child: Directionality(
textDirection: TextDirection.ltr,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (lobby != null)
btn(Icons.share, () {
Clipboard.setData(ClipboardData(text: lobby.code));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('کد میز کپی شد؛ برای دوستانت بفرست'),
),
);
}),
btn(Icons.arrow_forward, _leave),
],
),
),
);
}
Widget _content(BuildContext context, TableLobby lobby) {
final youSeat = lobby.youSeat;
// نگاشتِ جایگاهِ مطلق → بازیکن (خالی = بات هنگام شروع).
final bySeat = <int, LobbyPlayer>{for (final p in lobby.players) p.seat: p};
// چیدمان از نگاهِ خودِ کاربر: «شما» پایین، هم‌تیمی (روبه‌رو) بالا، بقیه کنار.
LobbyPlayer? seatAt(int rel) => bySeat[(youSeat + rel) % 4];
final me = seatAt(0);
final top = seatAt(2); // هم‌تیمی (روبه‌رو)
final left = seatAt(1);
final right = seatAt(3);
return SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
const Text('شماره میز', style: TextStyle(color: Colors.white70)),
const SizedBox(height: 4),
GlowText(lobby.code, size: 36),
const SizedBox(height: 8),
// میز با چهار جایگاه.
SizedBox(
height: 340,
child: Stack(
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 28,
vertical: 36,
),
child: Container(
decoration: BoxDecoration(
gradient: const RadialGradient(
colors: [AppColors.lapisHi, AppColors.lapisLo],
radius: 0.9,
),
borderRadius: BorderRadius.circular(160),
border: Border.all(
color: const Color(0xFF8A5A12),
width: 6,
),
),
child: const Center(child: GlowText('سلطان حکم', size: 18)),
),
),
Align(alignment: Alignment.topCenter, child: _seat(top, false)),
Align(
alignment: Alignment.centerLeft,
child: _seat(left, false),
),
Align(
alignment: Alignment.centerRight,
child: _seat(right, false),
),
Align(
alignment: Alignment.bottomCenter,
child: _seat(me, true),
),
// چرخشِ جایگاه‌ها (فقط میزبان) — تغییرِ تیم‌بندی.
if (lobby.isHost) ...[
Align(
alignment: const Alignment(-1, -0.35),
child: _rotateBtn(
Icons.rotate_left,
() => context.read<GameBloc>().rotateTable(-1),
),
),
Align(
alignment: const Alignment(1, -0.35),
child: _rotateBtn(
Icons.rotate_right,
() => context.read<GameBloc>().rotateTable(1),
),
),
],
],
),
),
const SizedBox(height: 8),
const Text('تعداد دست', style: TextStyle(color: Colors.white70)),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (final h in [3, 5, 7]) _handChip(h, lobby.isHost),
],
),
const SizedBox(height: 14),
const Text('ورودی (سکه) — برنده کل را می‌برد',
style: TextStyle(color: Colors.white70)),
const SizedBox(height: 10),
Wrap(
alignment: WrapAlignment.center,
spacing: 8,
runSpacing: 8,
children: [
for (final v in const [0, 500, 1000, 5000, 20000])
_stakeChip(v, lobby.isHost, lobby.stake),
],
),
const SizedBox(height: 20),
if (lobby.isHost)
GameButton(
label: lobby.stake > 0
? 'شروع بازی (${lobby.stake} سکه)'
: 'شروع بازی',
icon: Icons.play_arrow,
width: double.infinity,
colors: const [AppColors.success, AppColors.successDark],
onTap: () =>
context.read<GameBloc>().startTable(_hands, stake: lobby.stake),
)
else
const Text(
'در انتظار شروع توسط میزبان…',
style: TextStyle(color: AppColors.gold, fontSize: 15),
),
const SizedBox(height: 6),
if (lobby.isHost)
const Text(
'جای‌های خالی با ربات پر می‌شوند',
style: TextStyle(color: Colors.white54, fontSize: 12),
),
const SizedBox(height: 20),
],
),
);
}
// یک جایگاهِ بازیکن دورِ میز (آواتار + ستاره + نام).
Widget _seat(LobbyPlayer? p, bool isYou) {
final empty = p == null;
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Stack(
clipBehavior: Clip.none,
children: [
Container(
width: 62,
height: 62,
padding: const EdgeInsets.all(3),
decoration: BoxDecoration(
color: AppColors.bgDark,
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: isYou ? AppColors.gold : AppColors.goldDark,
width: 2,
),
),
child:
empty
? const Icon(
Icons.person_add_alt_1,
color: Colors.white24,
)
: ClipRRect(
borderRadius: BorderRadius.circular(9),
child: RandomAvatar(p.avatarSeed),
),
),
if (!empty)
const Positioned(
bottom: -6,
left: -4,
child: Icon(Icons.star, color: AppColors.gold, size: 22),
),
],
),
const SizedBox(height: 6),
Text(
empty ? 'در انتظار' : (isYou ? 'شما' : p.name),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: empty ? Colors.white38 : Colors.white,
fontSize: 12,
fontWeight: empty ? FontWeight.normal : FontWeight.bold,
),
),
if (p?.host == true)
const Text(
'میزبان',
style: TextStyle(color: AppColors.gold, fontSize: 10),
),
],
);
}
// دکمه‌ی چرخشِ جایگاه‌ها (کنارِ میز).
Widget _rotateBtn(IconData icon, VoidCallback onTap) {
return GestureDetector(
onTap: onTap,
child: Container(
width: 44,
height: 44,
decoration: BoxDecoration(
gradient: const LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [AppColors.lapisMid, AppColors.lapisLo],
),
borderRadius: BorderRadius.circular(10),
border: Border.all(color: AppColors.gold, width: 1.5),
),
child: Icon(icon, color: AppColors.gold, size: 26),
),
);
}
Widget _handChip(int h, bool editable) {
const fa = {3: '۳', 5: '۵', 7: '۷'};
final selected = _hands == h;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: GestureDetector(
onTap: editable ? () => setState(() => _hands = h) : null,
child: Container(
width: 52,
height: 52,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient:
selected
? const LinearGradient(
colors: [AppColors.gold, AppColors.goldDark],
)
: null,
color: selected ? null : AppColors.panel,
border: Border.all(
color: selected ? AppColors.gold : AppColors.goldDark,
width: 2,
),
),
child: Text(
fa[h]!,
style: TextStyle(
color: selected ? AppColors.bg : Colors.white,
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
),
),
);
}
// چیپِ شرطِ سکه؛ میزبان با انتخاب، setStake را زنده برای همه پخش می‌کند.
Widget _stakeChip(int value, bool editable, int current) {
final selected = current == value;
final label = value == 0 ? 'رایگان' : '$value';
return GestureDetector(
onTap: editable ? () => context.read<GameBloc>().setStake(value) : null,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: selected
? const LinearGradient(colors: [AppColors.gold, AppColors.goldDark])
: null,
color: selected ? null : AppColors.panel,
border: Border.all(
color: selected ? AppColors.gold : AppColors.goldDark,
width: 1.6,
),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (value > 0)
Icon(Icons.monetization_on,
size: 16, color: selected ? AppColors.bg : AppColors.gold),
if (value > 0) const SizedBox(width: 4),
Text(
label,
style: TextStyle(
color: selected ? AppColors.bg : Colors.white,
fontWeight: FontWeight.bold,
),
),
],
),
),
);
}
}
/// اوورلی شمارش معکوس ۳، ۲، ۱ پیش از شروع بازی.
class _CountdownOverlay extends StatefulWidget {
final int seconds;
const _CountdownOverlay({required this.seconds});
@override
State<_CountdownOverlay> createState() => _CountdownOverlayState();
}
class _CountdownOverlayState extends State<_CountdownOverlay> {
late int _n;
Timer? _timer;
@override
void initState() {
super.initState();
_n = widget.seconds;
_timer = Timer.periodic(const Duration(seconds: 1), (_) {
if (!mounted) return;
setState(() => _n--);
if (_n <= 0) _timer?.cancel();
});
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.black.withValues(alpha: 0.7),
alignment: Alignment.center,
child: TweenAnimationBuilder<double>(
key: ValueKey(_n),
tween: Tween(begin: 0.4, end: 1.2),
duration: const Duration(milliseconds: 700),
curve: Curves.easeOut,
builder:
(context, scale, child) =>
Transform.scale(scale: scale, child: child),
child: GlowText(_n > 0 ? '$_n' : 'شروع!', size: 96),
),
);
}
}
@@ -0,0 +1,433 @@
// نمای لابیِ میزِ خصوصی و اوورلیِ شمارشِ معکوس.
part of 'private_table_screen.dart';
class _LobbyView extends StatefulWidget {
final GameUiState state;
const _LobbyView({required this.state});
@override
State<_LobbyView> createState() => _LobbyViewState();
}
class _LobbyViewState extends State<_LobbyView> {
int _hands = 3; // تعداد دستِ انتخابیِ میزبان
void _leave() {
context.read<GameBloc>().leaveTable();
if (context.canPop()) context.pop();
}
@override
Widget build(BuildContext context) {
final state = widget.state;
final lobby = state.lobby;
final connecting = state.connection != WsStatus.connected || lobby == null;
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, _) {
if (!didPop) _leave();
},
child: Scaffold(
body: GameBackground(
child: SafeArea(
child: Stack(
children: [
Column(
children: [
_topBar(context, lobby),
Expanded(
child:
connecting
? const Center(
child: CircularProgressIndicator(
color: AppColors.gold,
),
)
: _content(context, lobby),
),
],
),
if (state.countdown != null)
_CountdownOverlay(seconds: state.countdown!),
],
),
),
),
),
);
}
// نوار بالا: بازگشت (چپ) و اشتراک‌گذاریِ کد (راست).
Widget _topBar(BuildContext context, TableLobby? lobby) {
Widget btn(IconData icon, VoidCallback onTap) => GestureDetector(
onTap: onTap,
child: Container(
width: 46,
height: 46,
decoration: BoxDecoration(
color: AppColors.panel,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: AppColors.gold, width: 1.5),
),
child: Icon(icon, color: AppColors.gold),
),
);
return Padding(
padding: const EdgeInsets.all(10),
child: Directionality(
textDirection: TextDirection.ltr,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (lobby != null)
btn(Icons.share, () {
Clipboard.setData(ClipboardData(text: lobby.code));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('کد میز کپی شد؛ برای دوستانت بفرست'),
),
);
}),
btn(Icons.arrow_forward, _leave),
],
),
),
);
}
Widget _content(BuildContext context, TableLobby lobby) {
final youSeat = lobby.youSeat;
// نگاشتِ جایگاهِ مطلق → بازیکن (خالی = بات هنگام شروع).
final bySeat = <int, LobbyPlayer>{for (final p in lobby.players) p.seat: p};
// چیدمان از نگاهِ خودِ کاربر: «شما» پایین، هم‌تیمی (روبه‌رو) بالا، بقیه کنار.
LobbyPlayer? seatAt(int rel) => bySeat[(youSeat + rel) % 4];
final me = seatAt(0);
final top = seatAt(2); // هم‌تیمی (روبه‌رو)
final left = seatAt(1);
final right = seatAt(3);
return SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
const Text('شماره میز', style: TextStyle(color: Colors.white70)),
const SizedBox(height: 4),
GlowText(lobby.code, size: 36),
const SizedBox(height: 8),
// میز با چهار جایگاه.
SizedBox(
height: 340,
child: Stack(
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 28,
vertical: 36,
),
child: Container(
decoration: BoxDecoration(
gradient: const RadialGradient(
colors: [AppColors.lapisHi, AppColors.lapisLo],
radius: 0.9,
),
borderRadius: BorderRadius.circular(160),
border: Border.all(
color: const Color(0xFF8A5A12),
width: 6,
),
),
child: const Center(child: GlowText('سلطان حکم', size: 18)),
),
),
Align(alignment: Alignment.topCenter, child: _seat(top, false)),
Align(
alignment: Alignment.centerLeft,
child: _seat(left, false),
),
Align(
alignment: Alignment.centerRight,
child: _seat(right, false),
),
Align(
alignment: Alignment.bottomCenter,
child: _seat(me, true),
),
// چرخشِ جایگاه‌ها (فقط میزبان) — تغییرِ تیم‌بندی.
if (lobby.isHost) ...[
Align(
alignment: const Alignment(-1, -0.35),
child: _rotateBtn(
Icons.rotate_left,
() => context.read<GameBloc>().rotateTable(-1),
),
),
Align(
alignment: const Alignment(1, -0.35),
child: _rotateBtn(
Icons.rotate_right,
() => context.read<GameBloc>().rotateTable(1),
),
),
],
],
),
),
const SizedBox(height: 8),
const Text('تعداد دست', style: TextStyle(color: Colors.white70)),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (final h in [3, 5, 7]) _handChip(h, lobby.isHost),
],
),
const SizedBox(height: 14),
const Text('ورودی (سکه) — برنده کل را می‌برد',
style: TextStyle(color: Colors.white70)),
const SizedBox(height: 10),
Wrap(
alignment: WrapAlignment.center,
spacing: 8,
runSpacing: 8,
children: [
for (final v in const [0, 500, 1000, 5000, 20000])
_stakeChip(v, lobby.isHost, lobby.stake),
],
),
const SizedBox(height: 20),
if (lobby.isHost)
GameButton(
label: lobby.stake > 0
? 'شروع بازی (${lobby.stake} سکه)'
: 'شروع بازی',
icon: Icons.play_arrow,
width: double.infinity,
colors: const [AppColors.success, AppColors.successDark],
onTap: () =>
context.read<GameBloc>().startTable(_hands, stake: lobby.stake),
)
else
const Text(
'در انتظار شروع توسط میزبان…',
style: TextStyle(color: AppColors.gold, fontSize: 15),
),
const SizedBox(height: 6),
if (lobby.isHost)
const Text(
'جای‌های خالی با ربات پر می‌شوند',
style: TextStyle(color: Colors.white54, fontSize: 12),
),
const SizedBox(height: 20),
],
),
);
}
// یک جایگاهِ بازیکن دورِ میز (آواتار + ستاره + نام).
Widget _seat(LobbyPlayer? p, bool isYou) {
final empty = p == null;
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Stack(
clipBehavior: Clip.none,
children: [
Container(
width: 62,
height: 62,
padding: const EdgeInsets.all(3),
decoration: BoxDecoration(
color: AppColors.bgDark,
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: isYou ? AppColors.gold : AppColors.goldDark,
width: 2,
),
),
child:
empty
? const Icon(
Icons.person_add_alt_1,
color: Colors.white24,
)
: ClipRRect(
borderRadius: BorderRadius.circular(9),
child: RandomAvatar(p.avatarSeed),
),
),
if (!empty)
const Positioned(
bottom: -6,
left: -4,
child: Icon(Icons.star, color: AppColors.gold, size: 22),
),
],
),
const SizedBox(height: 6),
Text(
empty ? 'در انتظار' : (isYou ? 'شما' : p.name),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: empty ? Colors.white38 : Colors.white,
fontSize: 12,
fontWeight: empty ? FontWeight.normal : FontWeight.bold,
),
),
if (p?.host == true)
const Text(
'میزبان',
style: TextStyle(color: AppColors.gold, fontSize: 10),
),
],
);
}
// دکمه‌ی چرخشِ جایگاه‌ها (کنارِ میز).
Widget _rotateBtn(IconData icon, VoidCallback onTap) {
return GestureDetector(
onTap: onTap,
child: Container(
width: 44,
height: 44,
decoration: BoxDecoration(
gradient: const LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [AppColors.lapisMid, AppColors.lapisLo],
),
borderRadius: BorderRadius.circular(10),
border: Border.all(color: AppColors.gold, width: 1.5),
),
child: Icon(icon, color: AppColors.gold, size: 26),
),
);
}
Widget _handChip(int h, bool editable) {
const fa = {3: '۳', 5: '۵', 7: '۷'};
final selected = _hands == h;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: GestureDetector(
onTap: editable ? () => setState(() => _hands = h) : null,
child: Container(
width: 52,
height: 52,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient:
selected
? const LinearGradient(
colors: [AppColors.gold, AppColors.goldDark],
)
: null,
color: selected ? null : AppColors.panel,
border: Border.all(
color: selected ? AppColors.gold : AppColors.goldDark,
width: 2,
),
),
child: Text(
fa[h]!,
style: TextStyle(
color: selected ? AppColors.bg : Colors.white,
fontSize: 22,
fontWeight: FontWeight.bold,
),
),
),
),
);
}
// چیپِ شرطِ سکه؛ میزبان با انتخاب، setStake را زنده برای همه پخش می‌کند.
Widget _stakeChip(int value, bool editable, int current) {
final selected = current == value;
final label = value == 0 ? 'رایگان' : '$value';
return GestureDetector(
onTap: editable ? () => context.read<GameBloc>().setStake(value) : null,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: selected
? const LinearGradient(colors: [AppColors.gold, AppColors.goldDark])
: null,
color: selected ? null : AppColors.panel,
border: Border.all(
color: selected ? AppColors.gold : AppColors.goldDark,
width: 1.6,
),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (value > 0)
Icon(Icons.monetization_on,
size: 16, color: selected ? AppColors.bg : AppColors.gold),
if (value > 0) const SizedBox(width: 4),
Text(
label,
style: TextStyle(
color: selected ? AppColors.bg : Colors.white,
fontWeight: FontWeight.bold,
),
),
],
),
),
);
}
}
/// اوورلی شمارش معکوس ۳، ۲، ۱ پیش از شروع بازی.
class _CountdownOverlay extends StatefulWidget {
final int seconds;
const _CountdownOverlay({required this.seconds});
@override
State<_CountdownOverlay> createState() => _CountdownOverlayState();
}
class _CountdownOverlayState extends State<_CountdownOverlay> {
late int _n;
Timer? _timer;
@override
void initState() {
super.initState();
_n = widget.seconds;
_timer = Timer.periodic(const Duration(seconds: 1), (_) {
if (!mounted) return;
setState(() => _n--);
if (_n <= 0) _timer?.cancel();
});
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.black.withValues(alpha: 0.7),
alignment: Alignment.center,
child: TweenAnimationBuilder<double>(
key: ValueKey(_n),
tween: Tween(begin: 0.4, end: 1.2),
duration: const Duration(milliseconds: 700),
curve: Curves.easeOut,
builder:
(context, scale, child) =>
Transform.scale(scale: scale, child: child),
child: GlowText(_n > 0 ? '$_n' : 'شروع!', size: 96),
),
);
}
}