Files
Remever/lib/screens/dialogs/replace_diaog.dart

228 lines
6.9 KiB
Dart

import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:remever/common/functions.dart';
import 'package:remever/common/resources.dart';
import 'package:remever/common/utils.dart';
import 'package:remever/common/widgets/typography.dart';
import 'package:remever/common/widgets/w_if.dart';
import 'package:remever/common/widgets/wspace.dart';
import 'package:remever/components/extensions/context.dart';
import 'package:remever/components/extensions/state.dart';
import 'package:remever/database/database.dart';
import 'package:remever/gen/assets.gen.dart';
import 'package:remever/i18n/strings.g.dart';
import 'package:remever/inject.dart';
import 'package:remever/router.gr.dart';
import 'package:remever/screens/collections/widgets/collection_progress_bar.dart';
import 'package:remever/screens/dialogs/dialog_header.dart';
import 'package:remever/services/tickets/tickets_interface.dart';
import 'package:remever/widgets/primary_button.dart';
class ReplaceDialog extends StatefulWidget {
const ReplaceDialog({
super.key,
required this.currentCollection,
required this.ticket,
});
final Collection currentCollection;
final Ticket ticket;
@override
State<ReplaceDialog> createState() => _ReplaceDialogState();
}
class _ReplaceDialogState extends State<ReplaceDialog> {
Collection? _collectionToTransfer;
void _onCollectionTap() {
context.pushRoute(
CollectionSearchRoute(
onCollectionSelect: (collection) {
safeSetState(() => _collectionToTransfer = collection);
},
),
);
}
void _onTransferTap() async {
if (_collectionToTransfer == null) {
showErrorToast('Необходимо выбрать место для переноса');
return;
}
if (_collectionToTransfer!.id == widget.currentCollection.id) {
showErrorToast('Карточка итак в этой коллекции, выберете другую');
return;
}
await getIt<TicketsInterface>().transferTicket(
widget.ticket.id,
_collectionToTransfer!.id,
);
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
return Material(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const DialogHeader(title: 'Переместить карточку'),
const HSpace(16),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 28).r,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
AppTypography('из коллекции', type: Medium16px()),
const HSpace(8),
_buildCollection(widget.currentCollection),
const HSpace(16),
Center(
child: SizedBox.square(
dimension: 34.r,
child: DecoratedBox(
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: AppColors.gray,
),
child: Center(
child: Assets.icons.typeArrowDown.image(
height: 18.h,
width: 18.w,
),
),
),
),
),
const HSpace(16),
AppTypography('в коллекцию', type: Medium16px()),
const HSpace(8),
_buildCollection(_collectionToTransfer, isSelectable: true),
const HSpace(16),
_buildTransferButton(context),
],
),
),
],
),
);
}
Widget _buildTransferButton(BuildContext context) {
return PrimaryButton(
child: AppTypography(
'Перенести',
type: Regular14px(),
color: AppColors.white,
),
onTap: () => _onTransferTap(),
);
}
Widget _buildCollection(Collection? collection, {bool isSelectable = false}) {
return GestureDetector(
onTap: isSelectable ? () => _onCollectionTap() : null,
child: Container(
constraints: BoxConstraints(minHeight: 66.h, maxHeight: 84.h),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12).r,
color: AppColors.white,
),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8).r,
child:
collection == null
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AppTypography('Выберите коллекцию', type: Bold14px()),
],
)
: Row(
children: <Widget>[
_buildAvatar(collection),
const WSpace(5),
_buildInfo(collection),
],
),
),
);
}
///
/// Построение основной информации
///
Widget _buildInfo(Collection collection) {
return SizedBox(
width: 230.w,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_buildTitle(collection),
const HSpace(4),
Row(
children: [
Assets.icons.typeCards.image(
height: 18.h,
width: 18.w,
color: AppColors.disabled,
),
const WSpace(2),
AppTypography(
'${collection.likesCount.toString()} ${Utils.declOfNum(collection.likesCount, ['карточек', 'карточки', 'карточек'])}',
type: Regular14px(),
color: AppColors.disabled,
),
],
),
const HSpace(6),
const CollectionProgressBar(),
],
),
);
}
///
/// Название коллекции
///
Widget _buildTitle(Collection collection) {
return AppTypography(
collection.title,
type: Medium16px(),
maxLines: 2,
softWrap: true,
);
}
///
/// Обложка коллекции
///
Widget _buildAvatar(Collection collection) {
return SizedBox.square(
dimension: 50.r,
child: DecoratedBox(
decoration: BoxDecoration(shape: BoxShape.circle, color: AppColors.bg),
child: Wif(
condition: collection.image != null,
builder:
(context) => ClipOval(
child: Image.memory(collection.image!, fit: BoxFit.cover),
),
fallback:
(context) => Center(
child: AppTypography(
collection.title.substring(0, 1),
type: Bold34px(),
),
),
),
),
);
}
}