Files
sendico/frontend/pweb/lib/pages/dashboard/buttons/balance/carousel.dart
2025-12-24 19:59:50 +01:00

124 lines
3.3 KiB
Dart

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 {
final List<Wallet> wallets;
final ValueChanged<Wallet> onWalletChanged;
final ValueChanged<Wallet> onTopUp;
const WalletCarousel({
super.key,
required this.wallets,
required this.onWalletChanged,
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) {
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]),
),
);
},
),
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: _currentPage > 0 ? _goToPreviousPage : null,
icon: const Icon(Icons.arrow_back),
),
const SizedBox(width: 16),
CarouselIndicator(itemCount: widget.wallets.length),
const SizedBox(width: 16),
IconButton(
onPressed: _currentPage < widget.wallets.length - 1
? _goToNextPage
: null,
icon: const Icon(Icons.arrow_forward),
),
],
),
],
);
}
}