card adding and selecting default one

This commit is contained in:
Username1548
2025-03-21 20:40:48 +07:00
parent fa817548d9
commit cdc16b9a9c
30 changed files with 1254 additions and 296 deletions

View File

@@ -0,0 +1,66 @@
import 'dart:developer';
import 'package:easy_debounce/easy_debounce.dart';
import 'package:eatvio_app/features/my_finance/models/payment_url_model.dart';
import 'package:eatvio_app/features/my_finance/presentation/cubit/user_cards_cubit/user_cards_state.dart';
import 'package:eatvio_app/features/my_finance/repositories/cards_repository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class UserCardsCubit extends Cubit<UserCardsState> {
final CardsRepository cardsRepository;
UserCardsCubit({required this.cardsRepository}) : super(UserCardsState());
Future<PaymentUrlModel?> addCard() async {
try {
return await cardsRepository.addCard();
} catch (e, st) {
log(e.toString(), stackTrace: st);
}
return null;
}
Future<void> getCards() async {
try {
if (isClosed) return;
final cards = await cardsRepository.getCards();
emit(state.copyWith(cards: cards));
} catch (e, st) {
log(e.toString(), stackTrace: st);
}
}
Future<void> markAsDefault({required int id}) async {
final oldCards = [...state.cards];
if (isClosed) return;
final cards =
oldCards.map((card) {
if (card.id == id) {
return card.copyWith(isDefault: true);
}
return card.copyWith(isDefault: false);
}).toList();
emit(state.copyWith(cards: cards));
EasyDebounce.debounce('markAsDefault', Duration(milliseconds: id), () async {
try {
await cardsRepository.markAsDefault(id: id);
} catch (e, st) {
emit(state.copyWith(cards: oldCards));
log(e.toString(), stackTrace: st);
}
});
}
Future<void> deleteCard({required int id}) async {
final oldCards = [...state.cards];
try {
if (isClosed) return;
final cards = oldCards.where((card) => card.id != id).toList();
emit(state.copyWith(cards: cards));
await cardsRepository.deleteCard(id: id);
} catch (e, st) {
emit(state.copyWith(cards: oldCards));
log(e.toString(), stackTrace: st);
}
}
}

View File

@@ -0,0 +1,14 @@
import 'package:eatvio_app/features/my_finance/models/card_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'user_cards_state.freezed.dart';
@freezed
class UserCardsState with _$UserCardsState {
factory UserCardsState({
@Default([]) List<CardModel> cards,
@Default(false) bool isLoading,
}) = _UserCardsState;
}

View File

@@ -0,0 +1,186 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'user_cards_state.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models',
);
/// @nodoc
mixin _$UserCardsState {
List<CardModel> get cards => throw _privateConstructorUsedError;
bool get isLoading => throw _privateConstructorUsedError;
/// Create a copy of UserCardsState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$UserCardsStateCopyWith<UserCardsState> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $UserCardsStateCopyWith<$Res> {
factory $UserCardsStateCopyWith(
UserCardsState value,
$Res Function(UserCardsState) then,
) = _$UserCardsStateCopyWithImpl<$Res, UserCardsState>;
@useResult
$Res call({List<CardModel> cards, bool isLoading});
}
/// @nodoc
class _$UserCardsStateCopyWithImpl<$Res, $Val extends UserCardsState>
implements $UserCardsStateCopyWith<$Res> {
_$UserCardsStateCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of UserCardsState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({Object? cards = null, Object? isLoading = null}) {
return _then(
_value.copyWith(
cards:
null == cards
? _value.cards
: cards // ignore: cast_nullable_to_non_nullable
as List<CardModel>,
isLoading:
null == isLoading
? _value.isLoading
: isLoading // ignore: cast_nullable_to_non_nullable
as bool,
)
as $Val,
);
}
}
/// @nodoc
abstract class _$$UserCardsStateImplCopyWith<$Res>
implements $UserCardsStateCopyWith<$Res> {
factory _$$UserCardsStateImplCopyWith(
_$UserCardsStateImpl value,
$Res Function(_$UserCardsStateImpl) then,
) = __$$UserCardsStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({List<CardModel> cards, bool isLoading});
}
/// @nodoc
class __$$UserCardsStateImplCopyWithImpl<$Res>
extends _$UserCardsStateCopyWithImpl<$Res, _$UserCardsStateImpl>
implements _$$UserCardsStateImplCopyWith<$Res> {
__$$UserCardsStateImplCopyWithImpl(
_$UserCardsStateImpl _value,
$Res Function(_$UserCardsStateImpl) _then,
) : super(_value, _then);
/// Create a copy of UserCardsState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({Object? cards = null, Object? isLoading = null}) {
return _then(
_$UserCardsStateImpl(
cards:
null == cards
? _value._cards
: cards // ignore: cast_nullable_to_non_nullable
as List<CardModel>,
isLoading:
null == isLoading
? _value.isLoading
: isLoading // ignore: cast_nullable_to_non_nullable
as bool,
),
);
}
}
/// @nodoc
class _$UserCardsStateImpl implements _UserCardsState {
_$UserCardsStateImpl({
final List<CardModel> cards = const [],
this.isLoading = false,
}) : _cards = cards;
final List<CardModel> _cards;
@override
@JsonKey()
List<CardModel> get cards {
if (_cards is EqualUnmodifiableListView) return _cards;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_cards);
}
@override
@JsonKey()
final bool isLoading;
@override
String toString() {
return 'UserCardsState(cards: $cards, isLoading: $isLoading)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$UserCardsStateImpl &&
const DeepCollectionEquality().equals(other._cards, _cards) &&
(identical(other.isLoading, isLoading) ||
other.isLoading == isLoading));
}
@override
int get hashCode => Object.hash(
runtimeType,
const DeepCollectionEquality().hash(_cards),
isLoading,
);
/// Create a copy of UserCardsState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$UserCardsStateImplCopyWith<_$UserCardsStateImpl> get copyWith =>
__$$UserCardsStateImplCopyWithImpl<_$UserCardsStateImpl>(
this,
_$identity,
);
}
abstract class _UserCardsState implements UserCardsState {
factory _UserCardsState({final List<CardModel> cards, final bool isLoading}) =
_$UserCardsStateImpl;
@override
List<CardModel> get cards;
@override
bool get isLoading;
/// Create a copy of UserCardsState
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$UserCardsStateImplCopyWith<_$UserCardsStateImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View File

@@ -1,21 +1,35 @@
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/custom_text_style.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/my_finance/presentation/cubit/user_cards_cubit/user_cards_cubit.dart';
import 'package:eatvio_app/features/my_finance/presentation/cubit/user_cards_cubit/user_cards_state.dart';
import 'package:eatvio_app/features/my_finance/presentation/widgets/card_widget.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:collection/collection.dart';
class CardSettingsScreen extends StatelessWidget {
static const path = '/card-settings-screen';
static const name = 'cardSettingsScreen';
const CardSettingsScreen({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(create: (context) => sl<UserCardsCubit>()..getCards(), child: CardsSettingsContent());
}
}
class CardsSettingsContent extends StatelessWidget {
CardsSettingsContent({super.key});
@override
Widget build(BuildContext context) {
final s = S.of(context);
return Scaffold(
backgroundColor: Color(0xFFF0F2F5),
appBar: AppBar(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(12),
),
),
title: Text('Мои карты'),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.vertical(bottom: Radius.circular(12))),
title: Text(s.my_cards),
centerTitle: true,
),
body: Column(
@@ -30,41 +44,42 @@ class CardSettingsScreen extends StatelessWidget {
const SizedBox(height: 16),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
'Привязанные карты',
style: TextStyle(
color: Color(0xFF323F4B),
fontSize: 18,
fontWeight: FontWeight.w600,
height: 1,
),
),
child: Text('Привязанные карты', style: TextStyle(color: Color(0xFF323F4B), fontSize: 18, fontWeight: FontWeight.w600, height: 1)),
),
const SizedBox(height: 8),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
'Выплату будут производиться\nна карту ** 0278',
style: TextStyle(
color: Color(0xFF323F4B),
fontSize: 16,
fontWeight: FontWeight.w500,
height: 1,
),
child: BlocBuilder<UserCardsCubit, UserCardsState>(
builder: (context, state) {
final defaultCardNumber = state.cards.firstWhereOrNull((element) => element.isDefault)?.cardNumber;
if (defaultCardNumber != null) {
return Text(
'Выплату будут производиться\nна карту ${defaultCardNumber.substring(defaultCardNumber.length - 7)}',
style: TextStyle(color: Color(0xFF323F4B), fontSize: 16, fontWeight: FontWeight.w500, height: 1),
);
}
return Text(s.no_default_cards, style: TextStyles.textMMedium.copyWith(color: appTheme.black));
},
),
),
const SizedBox(height: 16),
SingleChildScrollView(
padding: EdgeInsets.symmetric(horizontal: 16),
scrollDirection: Axis.horizontal,
child: Row(
children: [
_card(true),
const SizedBox(width: 8),
_card(),
const SizedBox(width: 8),
_card(),
],
child: BlocBuilder<UserCardsCubit, UserCardsState>(
builder: (context, state) {
if (state.isLoading) {}
return Row(
spacing: 12,
children: List.generate(state.cards.length + 1, (index) {
if (index < state.cards.length) {
final cardsNumber = state.cards[index].cardNumber.substring(state.cards[index].cardNumber.length - 7);
return CardWidget(isSelected: state.cards[index].isDefault, cardId: state.cards[index].id, cardNumber: cardsNumber);
}
return CardWidget(enabledAddCard: true);
}),
);
},
),
),
const SizedBox(height: 16),
@@ -75,40 +90,4 @@ class CardSettingsScreen extends StatelessWidget {
),
);
}
_card([bool isSelected = false]) {
return Container(
padding: EdgeInsets.all(4),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: isSelected ? appTheme.blueA400 : Color(0xFFF1F2F6),
width: 2,
),
),
child: Container(
width: 148,
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Color(0xFFF1F2F6),
borderRadius: BorderRadius.circular(4),
),
child: Column(
children: [
SvgPicture.asset('assets/icons/mir.svg'),
const SizedBox(height: 24),
Text(
'** 0278',
style: TextStyle(
color: Color(0xFF323F4B),
fontSize: 14,
height: 1,
fontWeight: FontWeight.w600,
),
),
],
),
),
);
}
}

View File

@@ -2,6 +2,7 @@ import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/utils/date_time_utils.dart';
import 'package:eatvio_app/core/widgets/custom_drop_down_button.dart';
import 'package:eatvio_app/features/my_finance/entities/consultant_finance_entity.dart';
import 'package:eatvio_app/features/my_finance/presentation/pages/card_settings.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
@@ -13,11 +14,7 @@ class UserFinanceScreen extends StatelessWidget {
return Scaffold(
backgroundColor: Color(0xFFF0F2F5),
appBar: AppBar(
shape: RoundedRectangleBorder(
borderRadius: BorderRadiusDirectional.vertical(
bottom: Radius.circular(12),
),
),
shape: RoundedRectangleBorder(borderRadius: BorderRadiusDirectional.vertical(bottom: Radius.circular(12))),
title: Text('История операций'),
centerTitle: true,
),
@@ -28,26 +25,16 @@ class UserFinanceScreen extends StatelessWidget {
const SizedBox(height: 8),
InkWell(
onTap: () {
GoRouter.of(context).pushNamed('cardSettingsScreen');
GoRouter.of(context).pushNamed(CardSettingsScreen.name);
},
borderRadius: BorderRadius.circular(20),
child: Ink(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: appTheme.blueA400,
),
decoration: BoxDecoration(borderRadius: BorderRadius.circular(20), color: appTheme.blueA400),
width: MediaQuery.of(context).size.width - 32,
child: Center(
child: Padding(
padding: const EdgeInsets.all(16),
child: Text(
'Управление картами',
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
child: Text('Управление картами', style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w600)),
),
),
),
@@ -62,108 +49,73 @@ class UserFinanceScreen extends StatelessWidget {
physics: const NeverScrollableScrollPhysics(),
child: Column(
children: [
CustomDropDownButton(
items: [],
onChanged: (value) {},
),
CustomDropDownButton(items: [], onChanged: (value) {}),
const SizedBox(height: 12),
ListView.separated(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) => Material(
color: Color(0xFFF6F6F6),
borderRadius: BorderRadius.circular(20),
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
itemBuilder:
(context, index) => Material(
color: Color(0xFFF6F6F6),
borderRadius: BorderRadius.circular(20),
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
testFinanceEntity.consultationsList[index].id,
style: TextStyle(
color: Color(0xFF323F4B),
height: 1,
fontWeight: FontWeight.w500,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
testFinanceEntity.consultationsList[index].id,
style: TextStyle(color: Color(0xFF323F4B), height: 1, fontWeight: FontWeight.w500),
),
Text(
testFinanceEntity.consultationsList[index].date.format('dd.MM.yyy', 'RU'),
style: TextStyle(color: Color(0xFF6D6F71), height: 1, fontWeight: FontWeight.w500),
),
],
),
const SizedBox(height: 8),
Text(
testFinanceEntity.consultationsList[index].date.format('dd.MM.yyy', 'RU'),
style: TextStyle(
color: Color(0xFF6D6F71),
height: 1,
fontWeight: FontWeight.w500,
),
testFinanceEntity.consultationsList[index].title,
style: TextStyle(color: appTheme.blueA400, height: 1, fontSize: 18, fontWeight: FontWeight.w600),
),
],
),
const SizedBox(height: 8),
Text(
testFinanceEntity.consultationsList[index].title,
style: TextStyle(
color: appTheme.blueA400,
height: 1,
fontSize: 18,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 8),
Text(
'Покупатель: ' + testFinanceEntity.consultationsList[index].clientName,
style: TextStyle(
color: Color(0xFF323F4B),
height: 1,
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 8),
Text(
testFinanceEntity.consultationsList[index].planDuration.toString() + ' дней',
style: TextStyle(
color: Color(0xFF323F4B),
height: 1,
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 8),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const SizedBox(height: 8),
Text(
testFinanceEntity.consultationsList[index].price.toString() + ' ₽',
style: TextStyle(
color: Color(0xFF323F4B),
height: 1,
fontSize: 18,
fontWeight: FontWeight.w600,
),
'Покупатель: ' + testFinanceEntity.consultationsList[index].clientName,
style: TextStyle(color: Color(0xFF323F4B), height: 1, fontSize: 14, fontWeight: FontWeight.w500),
),
Material(
color: Color(0xFF0BB450).withAlpha(11),
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
child: Text(
testFinanceEntity.consultationsList[index].status.name,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Color(0xFF0BB450),
height: 1,
const SizedBox(height: 8),
Text(
testFinanceEntity.consultationsList[index].planDuration.toString() + ' дней',
style: TextStyle(color: Color(0xFF323F4B), height: 1, fontSize: 14, fontWeight: FontWeight.w500),
),
const SizedBox(height: 8),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
testFinanceEntity.consultationsList[index].price.toString() + ' ₽',
style: TextStyle(color: Color(0xFF323F4B), height: 1, fontSize: 18, fontWeight: FontWeight.w600),
),
Material(
color: Color(0xFF0BB450).withAlpha(11),
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
child: Text(
testFinanceEntity.consultationsList[index].status.name,
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w400, color: Color(0xFF0BB450), height: 1),
),
),
),
),
],
),
],
),
],
),
),
),
),
separatorBuilder: (context, index) => const SizedBox(height: 12),
itemCount: testFinanceEntity.consultationsList.length,
),

View File

@@ -0,0 +1,68 @@
import 'package:eatvio_app/core/theme/custom_text_style.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/my_finance/presentation/cubit/user_cards_cubit/user_cards_cubit.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:url_launcher/url_launcher.dart';
class CardWidget extends StatelessWidget {
const CardWidget({super.key, this.isSelected = false, this.enabledAddCard = false, this.cardId = 0, this.cardNumber = ''});
final int cardId;
final bool isSelected;
final bool enabledAddCard;
final String cardNumber;
@override
Widget build(BuildContext context) {
final s = S.of(context);
return GestureDetector(
onTap: enabledAddCard
? () async {
final addCardUrl = await context.read<UserCardsCubit>().addCard();
if (addCardUrl != null) {
final uri = Uri.parse(addCardUrl.paymentUrl);
if (await canLaunchUrl(uri)) {
await launchUrl(uri, mode: LaunchMode.externalApplication);
}
}
}
: () {
context.read<UserCardsCubit>().markAsDefault(id: cardId);
},
child: Container(
padding: EdgeInsets.all(4),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
border: Border.all(color: isSelected ? appTheme.blueA400 : Color(0xFFF1F2F6), width: 2),
),
child: enabledAddCard
? Container(
width: 148,
padding: EdgeInsets.all(10),
decoration: BoxDecoration(color: Color(0xFFF1F2F6), borderRadius: BorderRadius.circular(4)),
child: Column(
children: [
SvgPicture.asset('assets/icons/add_card.svg'),
const SizedBox(height: 24),
Text(s.new_card, style: TextStyles.textSSemibold.copyWith(color: appTheme.black)),
],
),
)
: Container(
width: 148,
padding: EdgeInsets.all(10),
decoration: BoxDecoration(color: Color(0xFFF1F2F6), borderRadius: BorderRadius.circular(4)),
child: Column(
children: [
SvgPicture.asset('assets/icons/card.svg'),
const SizedBox(height: 24),
Text(cardNumber, style: TextStyles.textSSemibold.copyWith(color: appTheme.black)),
],
),
),
),
);
}
}