mnemo_cards/lib/features/tests/test_widgets/simple_test.dart
2024-05-22 23:48:44 +03:00

154 lines
5.4 KiB
Dart

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mnemo_cards/features/tests/test_manager.dart';
import 'package:mnemo_cards/features/tests/test_widgets/test_image.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
import '../../../di/locator.dart';
import '../../../widgets/game_card_widget.dart';
import '../question_states/simple_test_state.dart';
import 'test_button.dart';
class SimpleTestWidget extends StatelessWidget {
final SimpleTestQuestionBody _simpleTestModel;
SimpleTestQuestionBody get model => _simpleTestModel;
bool get isCorrect => state.answer.startsWith(model.answer);
SimpleTestState get state => manager.getState(model.id!) as SimpleTestState;
TestManager get manager => locator.testManager;
Color get testColor => manager.color;
SimpleTestWidget(this._simpleTestModel, {super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context).textTheme;
final random = Random(state.seed);
final buttons = [...model.buttons]..shuffle(random);
return LayoutBuilder(builder: (context, constraints) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (model.image != null)
Flexible(
flex: 4,
child: Container(
margin: const EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.circular(GameCardWidget.borderRadius),
),
child: TestImageWidget(
TestImage.image(model.image!),
),
),
),
if (model.text != null)
Flexible(
flex: 1,
child: Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Center(
child: Text(
model.text!,
style: theme.headlineLarge,
),
),
),
),
SizedBox(
height: 16.0,
),
StatefulBuilder(builder: (context, setState) {
void setAnswer(String answer) {
setState(() {
manager.setState(
model.id!,
state.copyWith(
answer: answer,
),
);
manager.setState(
model.id!,
state.copyWith(
isAnswered: true,
isCorrect: isCorrect,
),
);
});
if (isCorrect) {
Future.delayed(
Duration(milliseconds: 300),
manager.nextTest,
);
}
}
bool buttonLine = (model.image != null &&
buttons.where((element) => element.image != null).length !=
2);
final imageSide = buttonLine
? constraints.maxWidth / 4 - 8
: constraints.maxWidth / 2 - 16;
final spacing = buttonLine ? 4.0 : 8.0;
return AbsorbPointer(
absorbing: state.isCorrect,
child: Wrap(
spacing: spacing,
runSpacing: 8.0,
children: buttons
.map(
(e) => e.isTextButton
? Container(
width: constraints.maxWidth / 2 - 16,
height: 90.h,
child: TestButton(
text: e.text,
onTap: () {
setAnswer(e.id);
},
// borderColor: testColor,
color: state.answer == e.id
? isCorrect
? Colors.green[200]
: Colors.red[200]
: Colors.white,
),
)
: SizedBox(
width: imageSide,
height: imageSide,
child: TestButton(
image: TestImage.image(e.image!),
onTap: () {
setAnswer(e.id);
},
// borderColor: testColor,
color: e.id == state.answer
? isCorrect
? Colors.green[200]
: Colors.red[200]
: Colors.white,
),
),
)
.toList(),
),
);
})
],
),
);
});
}
}