331 lines
10 KiB
Dart
331 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../../providers.dart';
|
|
import '../../game/models.dart';
|
|
import '../../game/enums.dart';
|
|
import '../../game/config.dart';
|
|
import '../common/item_card_widget.dart';
|
|
|
|
class InventoryGridWidget extends StatelessWidget {
|
|
const InventoryGridWidget({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Consumer<BattleProvider>(
|
|
builder: (context, battleProvider, child) {
|
|
final player = battleProvider.player;
|
|
return Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 16.0,
|
|
vertical: 8.0,
|
|
),
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Text(
|
|
"${AppStrings.bag} (${player.inventory.length}/${player.maxInventorySize})",
|
|
style: const TextStyle(
|
|
fontSize: ThemeConfig.fontSizeHeader,
|
|
fontWeight: ThemeConfig.fontWeightBold,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: GridView.builder(
|
|
padding: const EdgeInsets.all(16.0),
|
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
|
crossAxisCount: 4,
|
|
crossAxisSpacing: 8.0,
|
|
mainAxisSpacing: 8.0,
|
|
),
|
|
itemCount: player.maxInventorySize,
|
|
itemBuilder: (context, index) {
|
|
if (index < player.inventory.length) {
|
|
final item = player.inventory[index];
|
|
return InkWell(
|
|
onTap: () {
|
|
_showItemActionDialog(context, battleProvider, item);
|
|
},
|
|
child: ItemCardWidget(
|
|
item: item,
|
|
// Inventory items usually don't show price unless in sell mode,
|
|
// but logic here implies standard view.
|
|
// If needed, we can toggle showPrice based on context.
|
|
showPrice: false,
|
|
canBuy: false,
|
|
),
|
|
);
|
|
} else {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: ThemeConfig.textColorGrey),
|
|
color: ThemeConfig.emptySlotBg,
|
|
),
|
|
child: const Center(
|
|
child: Icon(
|
|
Icons.add_box,
|
|
color: ThemeConfig.textColorGrey,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
void _showItemActionDialog(
|
|
BuildContext context,
|
|
BattleProvider provider,
|
|
Item item,
|
|
) {
|
|
bool isShop = provider.currentStage.type == StageType.shop;
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => SimpleDialog(
|
|
title: Text("${item.name} Actions"),
|
|
children: [
|
|
SimpleDialogOption(
|
|
onPressed: () {
|
|
Navigator.pop(ctx);
|
|
_showEquipConfirmationDialog(context, provider, item);
|
|
},
|
|
child: const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 8.0),
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.shield, color: ThemeConfig.btnDefendActive),
|
|
SizedBox(width: 10),
|
|
Text(AppStrings.equip),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
if (isShop)
|
|
SimpleDialogOption(
|
|
onPressed: () {
|
|
Navigator.pop(ctx);
|
|
_showSellConfirmationDialog(context, provider, item);
|
|
},
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
child: Row(
|
|
children: [
|
|
const Icon(
|
|
Icons.attach_money,
|
|
color: ThemeConfig.statGoldColor,
|
|
),
|
|
const SizedBox(width: 10),
|
|
Text("${AppStrings.sell} (${item.price} G)"),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
SimpleDialogOption(
|
|
onPressed: () {
|
|
Navigator.pop(ctx);
|
|
_showDiscardConfirmationDialog(context, provider, item);
|
|
},
|
|
child: const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 8.0),
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.delete, color: ThemeConfig.btnActionActive),
|
|
SizedBox(width: 10),
|
|
Text(AppStrings.discard),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showSellConfirmationDialog(
|
|
BuildContext context,
|
|
BattleProvider provider,
|
|
Item item,
|
|
) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text("Sell Item"),
|
|
content: Text("Sell ${item.name} for ${item.price} G?"),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text(AppStrings.cancel),
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: ThemeConfig.statGoldColor,
|
|
),
|
|
onPressed: () {
|
|
provider.sellItem(item);
|
|
Navigator.pop(ctx);
|
|
},
|
|
child: const Text(
|
|
AppStrings.sell,
|
|
style: TextStyle(color: Colors.black),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showDiscardConfirmationDialog(
|
|
BuildContext context,
|
|
BattleProvider provider,
|
|
Item item,
|
|
) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text("Discard Item"),
|
|
content: Text("Are you sure you want to discard ${item.name}?"),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text(AppStrings.cancel),
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: ThemeConfig.btnActionActive,
|
|
),
|
|
onPressed: () {
|
|
provider.discardItem(item);
|
|
Navigator.pop(ctx);
|
|
},
|
|
child: const Text(AppStrings.discard),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showEquipConfirmationDialog(
|
|
BuildContext context,
|
|
BattleProvider provider,
|
|
Item newItem,
|
|
) {
|
|
final player = provider.player;
|
|
final oldItem = player.equipment[newItem.slot];
|
|
|
|
final currentMaxHp = player.totalMaxHp;
|
|
final currentAtk = player.totalAtk;
|
|
final currentDef = player.totalDefense;
|
|
final currentHp = player.hp;
|
|
|
|
int newMaxHp = currentMaxHp - (oldItem?.hpBonus ?? 0) + newItem.hpBonus;
|
|
int newAtk = currentAtk - (oldItem?.atkBonus ?? 0) + newItem.atkBonus;
|
|
int newDef = currentDef - (oldItem?.armorBonus ?? 0) + newItem.armorBonus;
|
|
|
|
double ratio = currentMaxHp > 0 ? currentHp / currentMaxHp : 0.0;
|
|
int newHp = (newMaxHp * ratio).toInt();
|
|
if (newHp < 0) newHp = 0;
|
|
if (newHp > newMaxHp) newHp = newMaxHp;
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text("Change Equipment"),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
"${AppStrings.equip} ${newItem.name}?",
|
|
style: const TextStyle(fontWeight: ThemeConfig.fontWeightBold),
|
|
),
|
|
if (oldItem != null)
|
|
Text(
|
|
"Replaces ${oldItem.name}",
|
|
style: const TextStyle(
|
|
fontSize: 12,
|
|
color: ThemeConfig.textColorGrey,
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
_buildStatChangeRow("Max HP", currentMaxHp, newMaxHp),
|
|
_buildStatChangeRow("Current HP", currentHp, newHp),
|
|
_buildStatChangeRow(AppStrings.atk, currentAtk, newAtk),
|
|
_buildStatChangeRow(AppStrings.def, currentDef, newDef),
|
|
_buildStatChangeRow(
|
|
"LUCK",
|
|
player.totalLuck,
|
|
player.totalLuck - (oldItem?.luck ?? 0) + newItem.luck,
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text(AppStrings.cancel),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
provider.equipItem(newItem);
|
|
Navigator.pop(ctx);
|
|
},
|
|
child: const Text(AppStrings.confirm),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildStatChangeRow(String label, int oldVal, int newVal) {
|
|
int diff = newVal - oldVal;
|
|
Color color = diff > 0
|
|
? ThemeConfig.statDiffPositive
|
|
: (diff < 0
|
|
? ThemeConfig.statDiffNegative
|
|
: ThemeConfig.statDiffNeutral);
|
|
String diffText = diff > 0 ? "(+$diff)" : (diff < 0 ? "($diff)" : "");
|
|
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(label),
|
|
Row(
|
|
children: [
|
|
Text(
|
|
"$oldVal",
|
|
style: const TextStyle(color: ThemeConfig.textColorGrey),
|
|
),
|
|
const Icon(
|
|
Icons.arrow_right,
|
|
size: 16,
|
|
color: ThemeConfig.textColorGrey,
|
|
),
|
|
Text(
|
|
"$newVal",
|
|
style: const TextStyle(fontWeight: ThemeConfig.fontWeightBold),
|
|
),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
diffText,
|
|
style: TextStyle(
|
|
color: color,
|
|
fontSize: 12,
|
|
fontWeight: ThemeConfig.fontWeightBold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|