203 lines
8.1 KiB
Dart
203 lines
8.1 KiB
Dart
import 'dart:ui';
|
|
import 'package:be_happy/presentation/event/tariff_sheet_event.dart';
|
|
import 'package:be_happy/presentation/viewmodel/tariff_sheet_bloc.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:be_happy/presentation/components/payment_option.dart';
|
|
|
|
import '../../../domain/entities/payment_card.dart';
|
|
import '../../event/payment_method_sheet_event.dart';
|
|
import '../../state/payment_method_sheet_state.dart';
|
|
import '../../viewmodel/payment_method_sheet_bloc.dart';
|
|
|
|
class PaymentMethodSheet extends StatefulWidget {
|
|
final PaymentCard? initialSelectedCard;
|
|
final bool showBalance;
|
|
|
|
const PaymentMethodSheet({
|
|
super.key,
|
|
this.initialSelectedCard,
|
|
this.showBalance = true,
|
|
});
|
|
|
|
@override
|
|
State<PaymentMethodSheet> createState() => _PaymentMethodSheetState();
|
|
}
|
|
|
|
class _PaymentMethodSheetState extends State<PaymentMethodSheet> {
|
|
int? _selectedPaymentMethod = -2;
|
|
int? _processingCardId; // <-- Переменная для отслеживания отправленного запроса
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
context.read<PaymentMethodSheetBloc>().add(PaymentMethodSheetStarted());
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return BlocConsumer<PaymentMethodSheetBloc, PaymentMethodSheetState>(
|
|
listener: (context, state) {
|
|
if (state.status == PaymentMethodSheetStatus.success && _processingCardId != null) {
|
|
final updatedCard = state.cards.cast<PaymentCard?>().firstWhere(
|
|
(c) => c?.id == _processingCardId,
|
|
orElse: () => state.cards.cast<PaymentCard?>().firstWhere((c) => c?.isMain == true, orElse: () => null),
|
|
);
|
|
|
|
_processingCardId = null;
|
|
Navigator.pop(context, updatedCard);
|
|
}
|
|
},
|
|
builder: (context, state) {
|
|
|
|
if (state.status == PaymentMethodSheetStatus.success && _selectedPaymentMethod == -2) {
|
|
if (widget.initialSelectedCard != null) {
|
|
final initialIndex = state.cards.indexWhere(
|
|
(card) => card.cardLastNumber == widget.initialSelectedCard!.cardLastNumber
|
|
);
|
|
_selectedPaymentMethod = initialIndex != -1 ? initialIndex : -1;
|
|
} else {
|
|
final mainCardIndex = state.cards.indexWhere((card) => card.isMain);
|
|
_selectedPaymentMethod = mainCardIndex != -1 ? mainCardIndex : (widget.showBalance ? -1 : 0);
|
|
}
|
|
}
|
|
|
|
return Align(
|
|
alignment: Alignment.bottomCenter,
|
|
child: ClipRRect(
|
|
borderRadius: const BorderRadius.vertical(top: Radius.circular(30)),
|
|
child: BackdropFilter(
|
|
filter: ImageFilter.blur(sigmaX: 20, sigmaY: 20),
|
|
child: Container(
|
|
height: 450,
|
|
padding: const EdgeInsets.only(top: 20, bottom: 10),
|
|
decoration: BoxDecoration(
|
|
color: const Color(0x00000032).withOpacity(0.6),
|
|
borderRadius: const BorderRadius.vertical(top: Radius.circular(30)),
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
Expanded(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
|
child: Column(
|
|
children: [
|
|
if (widget.showBalance) ...[
|
|
PaymentOption(
|
|
title: 'Баланс',
|
|
subtitle: '${state.balance} баллов',
|
|
isSelected: _selectedPaymentMethod == -1,
|
|
onTap: () {
|
|
setState(() {
|
|
_selectedPaymentMethod = -1;
|
|
});
|
|
Navigator.pop(context, 'balance');
|
|
},
|
|
),
|
|
const SizedBox(height: 12),
|
|
],
|
|
|
|
...state.cards.asMap().entries.map((entry) {
|
|
final index = entry.key;
|
|
final card = entry.value;
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: 12),
|
|
child: PaymentOption(
|
|
title: card.isMain ? "Основная" : "Карта",
|
|
subtitle: '${card.cardLastNumber}',
|
|
isSelected: _selectedPaymentMethod == index,
|
|
onTap: () async {
|
|
setState(() {
|
|
_selectedPaymentMethod = index;
|
|
_processingCardId = card.id;
|
|
});
|
|
|
|
// Отправляем событие в Блок
|
|
context.read<PaymentMethodSheetBloc>().add(
|
|
SelectPaymentCardRequested(card),
|
|
);
|
|
|
|
},
|
|
),
|
|
);
|
|
}).toList(),
|
|
|
|
Container(
|
|
height: 56,
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(24),
|
|
border: Border.all(
|
|
color: Colors.white.withOpacity(0.4),
|
|
width: 1,
|
|
),
|
|
),
|
|
child: Material(
|
|
color: Colors.transparent,
|
|
child: InkWell(
|
|
onTap: () {
|
|
context.read<PaymentMethodSheetBloc>().add(AddPaymentCardRequested());
|
|
},
|
|
borderRadius: BorderRadius.circular(24),
|
|
child: Center(
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.add,
|
|
color: const Color(0xFF66E3C4),
|
|
size: 20,
|
|
),
|
|
const SizedBox(width: 8),
|
|
const Text(
|
|
'Добавить платежную карту',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
|
|
},
|
|
);
|
|
}
|
|
|
|
String _getCardType(String lastNumber) {
|
|
if (lastNumber.isEmpty) return 'Card';
|
|
final firstDigit = lastNumber[0];
|
|
switch (firstDigit) {
|
|
case '4':
|
|
return 'Visa';
|
|
case '5':
|
|
return 'Mastercard';
|
|
case '9':
|
|
return 'BelCard';
|
|
default:
|
|
return 'Card';
|
|
}
|
|
}
|
|
}
|