mnemo_cards/lib/features/tests/test_page.dart
2024-06-11 03:28:57 +03:00

160 lines
5.3 KiB
Dart

import 'dart:developer';
import 'dart:math' as math;
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
import '../../di/locator.dart';
import 'scroll_physics.dart';
import 'test_complete_widget.dart';
import 'test_progress_widget.dart';
import 'test_widgets/input_buttons_test.dart';
import 'test_widgets/simple_test.dart';
@RoutePage()
class TestPage extends StatefulWidget {
final String testId;
@override
State<StatefulWidget> createState() => _TestPageState();
TestPage(this.testId);
}
class _TestPageState extends State<TestPage> {
PageController get pageController => locator.testManager.pageController;
@override
void initState() {
super.initState();
locator.testManager.setActiveTest(widget.testId);
locator.testManager.preloadImages(context);
pageController.addListener(_pageScrollListener);
}
@override
void dispose() {
pageController.removeListener(_pageScrollListener);
super.dispose();
}
void _pageScrollListener() {
final index = pageController.page?.floor() ?? 0;
if (index > 0 && index <= locator.testManager.questions.length) {
final prevId = locator.testManager.questions[index - 1].id!;
final state = locator.testManager.getState(prevId);
if (state?.isAnswered == false) {
// log('Question skipped');
locator.testManager.skip(prevId);
}
}
}
int lastTestIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: StreamBuilder(
stream: locator.testManager.testHolder.asStream,
builder: (context, s) {
if (!s.hasData || s.requireData!.questions.isEmpty) {
return Scaffold(
body: Center(
child: Text('Загружаю тест...'),
),
);
}
return Scaffold(
backgroundColor: locator.testManager.color.withOpacity(0.25),
body: SafeArea(
child: Builder(builder: (context) {
return Column(
children: [
SizedBox(
height:
(64.h - 20.h - MediaQuery.of(context).padding.top) /
2,
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0.w),
child: const TestProgressWidget(),
),
Expanded(
child: PageView.builder(
physics: RightBlockedScrollPhysics(
canScroll: _canScroll,
),
controller: locator.testManager.pageController,
itemBuilder: (context, index) {
if (index > locator.testManager.questions.length) {
return null;
}
if (index == locator.testManager.questions.length) {
return TestCompleteWidget(
locator.testManager.states,
locator.testManager.color,
);
}
lastTestIndex = math.max(index, lastTestIndex);
return switch (locator.testManager
.getTest(index)
.questionType) {
TestQuestionType.simple => SimpleTestWidget(
locator.testManager.getTest(index)
as SimpleTestQuestionBody,
),
TestQuestionType.input_buttons =>
InputButtonsTestWidget(
locator.testManager.getTest(index)
as InputButtonsTestQuestionBody,
),
TestQuestionType.matrix => Container(),
TestQuestionType.match => Container(),
// TODO: Handle this case.
TestQuestionType.undefined =>
throw UnimplementedError(),
};
},
),
),
],
);
}),
),
);
}),
);
}
bool _canScroll() {
return locator.testManager.pageController.hasClients &&
locator.testManager.pageController.page !=
locator.testManager.questions.length;
}
}
class _LifeWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: [
Icon(Icons.ac_unit_outlined),
SizedBox(
width: 2,
),
Text(
'30',
style: TextStyle(fontSize: 22),
)
],
);
}
}