import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:pshared/controllers/wallets.dart'; import 'package:pshared/models/payment/wallet.dart'; import 'package:pweb/pages/dashboard/buttons/balance/carousel.dart'; import 'package:pweb/pages/dashboard/buttons/balance/controller.dart'; import 'package:pweb/generated/i18n/app_localizations.dart'; class BalanceWidget extends StatelessWidget { final ValueChanged onTopUp; const BalanceWidget({ super.key, required this.onTopUp, }); @override Widget build(BuildContext context) { final walletsController = context.watch(); final carousel = context.watch(); final loc = AppLocalizations.of(context)!; if (walletsController.isLoading) { return const Center(child: CircularProgressIndicator()); } final wallets = walletsController.wallets; if (wallets.isEmpty) { return Center(child: Text(loc.noWalletsAvailable)); } // Ensure index is always valid when wallets list changes carousel.setIndex(carousel.index, wallets.length); final index = carousel.index; final wallet = wallets[index]; // Single source of truth: controller if (walletsController.selectedWallet?.id != wallet.id) { walletsController.selectWallet(wallet); } return WalletCarousel( wallets: wallets, currentIndex: index, onIndexChanged: (i) { carousel.setIndex(i, wallets.length); walletsController.selectWallet(wallets[i]); }, onTopUp: onTopUp, ); } }