Fixed wallets load
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pshared/controllers/wallets.dart';
|
||||
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
|
||||
import 'package:pshared/models/payment/wallet.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
|
||||
|
||||
class BalanceAmount extends StatelessWidget {
|
||||
|
||||
@@ -6,6 +6,7 @@ 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';
|
||||
|
||||
@@ -13,26 +14,45 @@ import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
class BalanceWidget extends StatelessWidget {
|
||||
final ValueChanged<Wallet> onTopUp;
|
||||
|
||||
const BalanceWidget({super.key, required this.onTopUp});
|
||||
const BalanceWidget({
|
||||
super.key,
|
||||
required this.onTopUp,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final walletsController = context.watch<WalletsController>();
|
||||
final carousel = context.watch<CarouselIndexController>();
|
||||
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,
|
||||
onWalletChanged: walletsController.selectWallet,
|
||||
currentIndex: index,
|
||||
onIndexChanged: (i) {
|
||||
carousel.setIndex(i, wallets.length);
|
||||
walletsController.selectWallet(wallets[i]);
|
||||
},
|
||||
onTopUp: onTopUp,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,101 +1,44 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pshared/models/payment/wallet.dart';
|
||||
import 'package:pweb/pages/dashboard/buttons/balance/card.dart';
|
||||
import 'package:pweb/pages/dashboard/buttons/balance/config.dart';
|
||||
import 'package:pweb/pages/dashboard/buttons/balance/indicator.dart';
|
||||
import 'package:pweb/providers/carousel.dart';
|
||||
|
||||
|
||||
class WalletCarousel extends StatefulWidget {
|
||||
class WalletCarousel extends StatelessWidget {
|
||||
final List<Wallet> wallets;
|
||||
final ValueChanged<Wallet> onWalletChanged;
|
||||
final int currentIndex;
|
||||
final ValueChanged<int> onIndexChanged;
|
||||
final ValueChanged<Wallet> onTopUp;
|
||||
|
||||
const WalletCarousel({
|
||||
super.key,
|
||||
required this.wallets,
|
||||
required this.onWalletChanged,
|
||||
required this.currentIndex,
|
||||
required this.onIndexChanged,
|
||||
required this.onTopUp,
|
||||
});
|
||||
|
||||
@override
|
||||
State<WalletCarousel> createState() => _WalletCarouselState();
|
||||
}
|
||||
|
||||
class _WalletCarouselState extends State<WalletCarousel> {
|
||||
late final PageController _pageController;
|
||||
int _currentPage = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_pageController = PageController(
|
||||
viewportFraction: WalletCardConfig.viewportFraction,
|
||||
);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (widget.wallets.isNotEmpty) {
|
||||
widget.onWalletChanged(widget.wallets[_currentPage]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pageController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onPageChanged(int index) {
|
||||
setState(() {
|
||||
_currentPage = index;
|
||||
});
|
||||
context.read<CarouselIndexProvider>().updateIndex(index);
|
||||
widget.onWalletChanged(widget.wallets[index]);
|
||||
}
|
||||
|
||||
void _goToPreviousPage() {
|
||||
if (_currentPage > 0) {
|
||||
_pageController.animateToPage(
|
||||
_currentPage - 1,
|
||||
duration: const Duration(milliseconds: 300),
|
||||
curve: Curves.easeInOut,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _goToNextPage() {
|
||||
if (_currentPage < widget.wallets.length - 1) {
|
||||
_pageController.animateToPage(
|
||||
_currentPage + 1,
|
||||
duration: const Duration(milliseconds: 300),
|
||||
curve: Curves.easeInOut,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (wallets.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
final safeIndex = currentIndex.clamp(0, wallets.length - 1);
|
||||
final wallet = wallets[safeIndex];
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
height: WalletCardConfig.cardHeight,
|
||||
child: PageView.builder(
|
||||
controller: _pageController,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
itemCount: widget.wallets.length,
|
||||
onPageChanged: _onPageChanged,
|
||||
itemBuilder: (context, index) {
|
||||
return Padding(
|
||||
padding: WalletCardConfig.cardPadding,
|
||||
child: WalletCard(
|
||||
wallet: widget.wallets[index],
|
||||
onTopUp: () => widget.onTopUp(widget.wallets[index]),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Padding(
|
||||
padding: WalletCardConfig.cardPadding,
|
||||
child: WalletCard(
|
||||
wallet: wallet,
|
||||
onTopUp: () => onTopUp(wallet),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
@@ -103,15 +46,20 @@ class _WalletCarouselState extends State<WalletCarousel> {
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
IconButton(
|
||||
onPressed: _currentPage > 0 ? _goToPreviousPage : null,
|
||||
onPressed: safeIndex > 0
|
||||
? () => onIndexChanged(safeIndex - 1)
|
||||
: null,
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
CarouselIndicator(itemCount: widget.wallets.length),
|
||||
CarouselIndicator(
|
||||
itemCount: wallets.length,
|
||||
index: safeIndex,
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
IconButton(
|
||||
onPressed: _currentPage < widget.wallets.length - 1
|
||||
? _goToNextPage
|
||||
onPressed: safeIndex < wallets.length - 1
|
||||
? () => onIndexChanged(safeIndex + 1)
|
||||
: null,
|
||||
icon: const Icon(Icons.arrow_forward),
|
||||
),
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
|
||||
class CarouselIndexController with ChangeNotifier {
|
||||
int _index = 0;
|
||||
|
||||
int get index => _index;
|
||||
|
||||
void setIndex(int value, int max) {
|
||||
final next = value.clamp(0, max > 0 ? max - 1 : 0);
|
||||
if (next == _index) return;
|
||||
_index = next;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void reset() {
|
||||
if (_index == 0) return;
|
||||
_index = 0;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,26 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:pweb/pages/dashboard/buttons/balance/config.dart';
|
||||
|
||||
import 'package:pweb/providers/carousel.dart';
|
||||
|
||||
|
||||
class CarouselIndicator extends StatelessWidget {
|
||||
final int itemCount;
|
||||
final int index;
|
||||
|
||||
const CarouselIndicator({
|
||||
super.key,
|
||||
required this.itemCount,
|
||||
required this.index,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final currentIndex = context.watch<CarouselIndexProvider>().currentIndex;
|
||||
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: List.generate(
|
||||
itemCount,
|
||||
(index) => _Dot(isActive: currentIndex == index),
|
||||
),
|
||||
);
|
||||
}
|
||||
Widget build(BuildContext context) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: List.generate(
|
||||
itemCount,
|
||||
(i) => _Dot(isActive: i == index),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class _Dot extends StatelessWidget {
|
||||
@@ -35,15 +30,15 @@ class _Dot extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = Theme.of(context).colorScheme.primary;
|
||||
|
||||
return Container(
|
||||
width: WalletCardConfig.dotSize,
|
||||
height: WalletCardConfig.dotSize,
|
||||
margin: WalletCardConfig.dotMargin,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: isActive
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: Theme.of(context).colorScheme.primary.withAlpha(60),
|
||||
color: isActive ? color : color.withAlpha(64),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user