mnemo_cards/lib/widgets/card_game/card_game_page.dart

304 lines
13 KiB
Dart
Raw Normal View History

2024-06-16 19:01:33 +00:00
import 'dart:async';
2024-05-22 20:48:44 +00:00
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mnemo_cards/features/tests/progress_widget.dart';
2024-06-16 19:01:33 +00:00
import 'package:mnemo_cards/main.dart';
import 'package:mnemo_cards/managers/audio_player.dart';
2024-05-22 20:48:44 +00:00
import 'package:mnemo_cards/theme/themes.dart';
2024-06-22 12:29:18 +00:00
import 'package:mnemo_cards/utils/color_helper.dart';
2024-05-22 20:48:44 +00:00
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
import '../../di/locator.dart';
import '../../features/card_flipper/flip_card_controllers.dart';
import '../../features/card_swiper/flutter_card_swiper.dart';
import 'card_game_main.dart';
@RoutePage()
class CardGamePage extends StatefulWidget {
final int? startCardId;
2024-05-28 21:25:51 +00:00
final List<GameCardDto> cards;
final Color? color;
2024-05-22 20:48:44 +00:00
final bool infinite;
2024-05-28 21:25:51 +00:00
final bool shuffle;
2024-05-22 20:48:44 +00:00
const CardGamePage({
2024-05-28 21:25:51 +00:00
required this.cards,
required this.color,
2024-05-22 20:48:44 +00:00
this.startCardId,
this.infinite = false,
2024-05-28 21:25:51 +00:00
this.shuffle = true,
2024-05-22 20:48:44 +00:00
super.key,
});
@override
State<StatefulWidget> createState() => _CardGamePageState();
}
class _CardGamePageState extends State<CardGamePage> {
final CardSwiperController cardSwiperController = CardSwiperController();
final FlipCardController flipCardController = FlipCardController();
2024-06-16 19:01:33 +00:00
StreamSubscription? _sub;
2024-05-28 21:25:51 +00:00
List<GameCardDto> shuffledCards = [];
2024-05-22 20:48:44 +00:00
GameCardDto? get activeCard {
final index = cardSwiperController.currentIndex;
if (index >= 0) {
2024-05-28 21:25:51 +00:00
return shuffledCards[index % shuffledCards.length];
2024-05-22 20:48:44 +00:00
}
return null;
}
2024-05-28 21:25:51 +00:00
int get gameLength => widget.infinite ? 10000000 : shuffledCards.length;
2024-05-22 20:48:44 +00:00
2024-06-16 19:01:33 +00:00
void playSound(int index) {
if (globalSharedPreferences.getBool('auto_play_sound_view') == true) {
final text = activeCard?.original;
if (text != null) {
AudioPlayer.playAudio(text, lang: 'es-ES');
}
}
}
2024-05-22 20:48:44 +00:00
@override
Widget build(BuildContext context) {
// todo move to controller
2024-06-13 05:06:50 +00:00
// final timer = Stream.periodic(Duration(seconds: 1), (tick) {
// return Duration(seconds: tick + 1);
// }).asBroadcastStream().take(3600).startWith(Duration.zero);
2024-05-22 20:48:44 +00:00
return Scaffold(
body: PopScope(
child: SafeArea(
child: LayoutBuilder(builder: (context, constraints) {
return Column(
children: [
Expanded(
child: LayoutBuilder(
builder: (context, constraints) {
final aspectRatio =
constraints.maxHeight / constraints.maxWidth;
return Column(
children: [
if (aspectRatio > 14 / 9)
SizedBox(
height: (64.h -
20.h -
MediaQuery.of(context).padding.top) /
2,
),
if (aspectRatio > 14 / 9)
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.w),
child: StreamBuilder(
stream: cardSwiperController.asStream,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return SizedBox(
height: 30,
);
}
return ProgressWidget(
widget.infinite
? null
: List.filled(
cardSwiperController.progress,
Result.correct,
),
2024-05-28 21:25:51 +00:00
shuffledCards.length,
widget.color ?? borderGray,
2024-05-22 20:48:44 +00:00
popText: widget.infinite ? 'к теме' : null,
key: ValueKey('CARDS PROGRESS'),
);
},
),
),
if (aspectRatio > 14 / 9)
SizedBox(
height: (64.h -
20.h -
MediaQuery.of(context).padding.top) /
2,
),
Expanded(
child: Stack(
children: [
CardGameMainWidget(
2024-05-28 21:25:51 +00:00
shuffledCards,
widget.color,
2024-05-22 20:48:44 +00:00
widget.startCardId,
cardSwiperController,
flipCardController,
gameLength,
),
if (aspectRatio <= 14 / 9)
Align(
alignment: Alignment.topLeft,
child: IconButton(
onPressed:
AutoRouter.of(context).maybePop,
icon: Icon(
Icons.arrow_back_ios,
2024-06-22 12:29:18 +00:00
color: Colors.grey[400],
2024-05-22 20:48:44 +00:00
),
),
)
],
),
),
],
);
},
),
),
if (constraints.maxHeight / constraints.maxWidth > 17 / 9)
Hero(
tag: 'bottom',
createRectTween: (a, b) => RectTween(begin: a, end: b),
child: Material(
color: Colors.transparent,
child: StreamBuilder(
stream: cardSwiperController.asStream,
builder: (context, snapshot) {
return AnimatedContainer(
duration: Duration(milliseconds: 200),
height: activeCard == null ? 0 : 90.h,
padding: EdgeInsets.only(
left: 10.0.w,
right: 10.0.w,
bottom: 10.0.h,
2024-06-05 03:04:16 +00:00
top: 10.0.h,
2024-05-22 20:48:44 +00:00
),
child: Row(
children: [
Expanded(
flex: 1,
child: GestureDetector(
onTap: () async {
if (activeCard != null)
await locator.favoriteCardsController
.switchFavoriteCard(activeCard!);
},
child: StreamBuilder(
stream: locator
.favoriteCardsController.asStream,
initialData: locator
.favoriteCardsController.state,
builder: (context, snapshot) {
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
2024-06-22 12:29:18 +00:00
color: Theme.of(context)
.scaffoldBackgroundColor
.lighten(0.05),
2024-05-22 20:48:44 +00:00
border: Border.all(
color: borderGray),
borderRadius:
BorderRadius.circular(16.0),
),
padding: EdgeInsets.all(10.0.w),
child: Image.asset(
2024-05-28 21:25:51 +00:00
locator.favoriteCardsController
.isFavoriteCard(
activeCard?.id,
)
? "icons/heart_fill.png"
: "icons/heart.png",
2024-05-22 20:48:44 +00:00
width: 29.w,
height: 29.h,
color: locator
.favoriteCardsController
.isFavoriteCard(
activeCard?.id,
)
? Colors.red
: Colors.grey,
),
);
}),
),
),
SizedBox(
width: 10.0.w,
),
Expanded(
flex: 3,
child: InkWell(
onTap: () async {
cardSwiperController.swipe(
2024-06-16 19:01:33 +00:00
direction: CardSwiperState.swipeLeft,
2024-05-22 20:48:44 +00:00
);
},
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
2024-06-22 12:29:18 +00:00
color: Theme.of(context)
.scaffoldBackgroundColor
.lighten(0.05),
2024-05-22 20:48:44 +00:00
border: Border.all(color: borderGray),
borderRadius:
BorderRadius.circular(16.0),
),
padding: EdgeInsets.all(10.0.w),
child: Text(
'Дальше',
style: TextStyle(
fontSize: 25.sp,
2024-06-22 12:29:18 +00:00
fontWeight: FontWeight.w700,
2024-05-22 20:48:44 +00:00
height: 0.85,
),
),
),
),
),
],
),
);
}),
),
)
],
);
}),
),
),
);
}
2024-05-28 21:25:51 +00:00
@override
void initState() {
super.initState();
shuffledCards = widget.cards.toList();
if (widget.shuffle) {
shuffledCards..shuffle();
}
if (widget.startCardId != null) {
final startCardIndex = shuffledCards.indexWhere(
(card) => card.id == widget.startCardId,
);
if (startCardIndex >= 0) {
shuffledCards = [
...shuffledCards.sublist(startCardIndex),
...shuffledCards.sublist(0, startCardIndex),
];
} else {
shuffledCards = [];
}
} else {
shuffledCards = widget.cards.toList();
}
2024-06-16 19:01:33 +00:00
_sub = cardSwiperController.asStream.distinct().listen(playSound);
2024-05-28 21:25:51 +00:00
}
2024-05-22 20:48:44 +00:00
@override
void dispose() {
2024-06-16 19:01:33 +00:00
_sub?.cancel().then((_) => _sub = null);
2024-05-22 20:48:44 +00:00
// cardSwiperController.removeListener(cardSwiperListener);
cardSwiperController.dispose();
super.dispose();
}
}