mnemo_cards/lib/widgets/packs_menu_widget.dart

251 lines
8.3 KiB
Dart
Raw Normal View History

2024-05-22 20:48:44 +00:00
import 'dart:convert';
import 'dart:developer';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mnemo_cards/features/packs/pack_manager.dart';
import 'package:mnemo_cards/utils/string_helper.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
2024-07-18 21:45:04 +00:00
import 'package:mnemo_cards_frontend_common/mnemo_cards_frontend_common.dart';
2024-05-22 20:48:44 +00:00
import '../di/locator.dart';
import '../domain/router/app_router.dart';
import '../domain/router/app_router.gr.dart';
import '../managers/repository/repository.dart';
import 'package:collection/collection.dart';
class PacksMenuWidget extends StatefulWidget {
final Repository repository;
final PackManager packManager;
final ScrollController? scrollController;
const PacksMenuWidget(
this.repository,
this.packManager, {
this.scrollController,
super.key,
});
@override
State<StatefulWidget> createState() => _PacksMenuWidgetState();
}
class _PacksMenuWidgetState extends State<PacksMenuWidget> {
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
final cardHeight = 130.h;
final theme = Theme.of(context);
return StreamBuilder(
stream: widget.packManager.packsPreviewStream.asyncMap((packs) async {
if (packs != null) {
await _precache(packs);
}
return packs;
}),
builder: (context, snapshot) {
return Column(
children: [
Header(
'Темы',
trail: GestureDetector(
onTap: () {
AppRouter.openAuthOrProfile();
},
child: Image.asset(
'icons/profile.png',
width: 27.w,
height: 27.h,
2024-06-22 12:29:18 +00:00
color: Theme.of(context).colorScheme.primary,
2024-05-22 20:48:44 +00:00
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: RefreshIndicator(
onRefresh: locator.previewPackPoller.poll,
2024-05-28 21:25:51 +00:00
backgroundColor: Colors.white,
color: snapshot.data?.firstOrNull?.color?.asColor ??
Colors.black,
2024-06-21 19:12:29 +00:00
child: (!snapshot.hasData)
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset(
'images/cerdo.jpg',
width: 100,
),
),
Text('Загрузка...'),
],
),
)
: ListView(
physics: BouncingScrollPhysics(
decelerationRate: ScrollDecelerationRate.fast,
parent: AlwaysScrollableScrollPhysics(),
2024-05-22 20:48:44 +00:00
),
2024-06-21 19:12:29 +00:00
scrollDirection: Axis.vertical,
children: snapshot.requireData!
.map((pack) => _PackButton4(pack, cardHeight))
.toList(),
2024-05-22 20:48:44 +00:00
),
),
),
),
],
);
},
);
});
}
Future<void> _precache(List<CardPackPreviewDto> packs) async {
for (final pack in packs) {
if (pack.imageBase64 != null) {
try {
2024-06-21 19:12:29 +00:00
final bytes = await compute(base64Decode, pack.imageBase64!);
2024-05-28 21:25:51 +00:00
final image = MemoryImage(bytes);
locator.imagesHolder.setImage('pack_${pack.id}', image);
await precacheImage(image, context);
2024-05-22 20:48:44 +00:00
} catch (e) {
log('${pack.id} cover not cached');
}
}
}
}
}
class _PackButton4 extends StatelessWidget {
final CardPackPreviewDto pack;
final double cardHeight;
const _PackButton4(
this.pack,
this.cardHeight,
);
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return InkWell(
onTap: () {
AutoRouter.of(context).push(
PageRouteInfo(
CardPackPage.name,
2024-05-28 21:25:51 +00:00
args: CardPackPageArgs(
previewDto: pack,
),
2024-05-22 20:48:44 +00:00
),
);
},
2024-06-13 05:06:50 +00:00
borderRadius: BorderRadius.circular(12.0),
2024-05-22 20:48:44 +00:00
child: Container(
margin: EdgeInsets.all(4.0.h),
alignment: Alignment.center,
height: cardHeight,
decoration: BoxDecoration(
2024-06-22 12:29:18 +00:00
// color: main,
2024-05-22 20:48:44 +00:00
border: Border.all(
color: pack.color?.asColor?.withOpacity(0.9) ??
Colors.grey.withOpacity(0.5),
2024-05-31 14:35:41 +00:00
width: 1,
2024-05-22 20:48:44 +00:00
),
borderRadius: BorderRadius.circular(12.0),
),
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Container(
decoration: BoxDecoration(
image: pack.imageBase64 == null
? null
: DecorationImage(
2024-05-28 21:25:51 +00:00
image: locator.imagesHolder.get('pack_${pack.id}') ??
2024-06-21 19:12:29 +00:00
MemoryImage(base64Decode(pack.imageBase64!)),
2024-05-22 20:48:44 +00:00
),
),
margin: EdgeInsets.all(2.0.w),
width: cardHeight,
height: cardHeight,
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FittedBox(
fit: BoxFit.scaleDown,
child: Text(
pack.title,
style: TextStyle(
2024-06-22 12:29:18 +00:00
fontWeight: FontWeight.w700,
2024-05-22 20:48:44 +00:00
fontSize: 24.sp,
height: 0.9,
),
),
),
if (pack.subtitle != null && pack.subtitle!.isNotEmpty)
FittedBox(
fit: BoxFit.scaleDown,
child: Text(
pack.subtitle!,
style: TextStyle(
fontWeight: FontWeight.w300,
fontSize: 16.sp,
),
),
),
Spacer(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Row(
children: [
Text(
pack.cards.toString(),
style: TextStyle(
fontWeight: FontWeight.w200,
fontSize: 15.sp,
),
),
Image.asset(
'icons/cards.png',
width: 18.w,
height: 16.h,
2024-06-22 12:29:18 +00:00
color: Theme.of(context).colorScheme.primary,
2024-05-22 20:48:44 +00:00
)
],
),
),
if (pack.price != null && !pack.isAvailable)
Text(
pack.price!,
style: TextStyle(
fontWeight: FontWeight.w400,
fontSize: 20.sp,
),
),
],
)
],
),
),
),
],
),
),
);
}
}