|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:survey_kit/src/answer_format/double_answer_format.dart'; |
| 3 | +import 'package:survey_kit/src/views/decoration/input_decoration.dart'; |
| 4 | +import 'package:survey_kit/src/result/question/double_question_result.dart'; |
| 5 | +import 'package:survey_kit/src/steps/predefined_steps/question_step.dart'; |
| 6 | +import 'package:survey_kit/src/views/widget/step_view.dart'; |
| 7 | + |
| 8 | +class DoubleAnswerView extends StatefulWidget { |
| 9 | + final QuestionStep questionStep; |
| 10 | + final DoubleQuestionResult? result; |
| 11 | + |
| 12 | + const DoubleAnswerView({ |
| 13 | + Key? key, |
| 14 | + required this.questionStep, |
| 15 | + required this.result, |
| 16 | + }) : super(key: key); |
| 17 | + |
| 18 | + @override |
| 19 | + _DoubleAnswerViewState createState() => _DoubleAnswerViewState(); |
| 20 | +} |
| 21 | + |
| 22 | +class _DoubleAnswerViewState extends State<DoubleAnswerView> { |
| 23 | + late final DoubleAnswerFormat _doubleAnswerFormat; |
| 24 | + late final TextEditingController _controller; |
| 25 | + late final DateTime _startDate; |
| 26 | + |
| 27 | + bool _isValid = false; |
| 28 | + |
| 29 | + @override |
| 30 | + void initState() { |
| 31 | + super.initState(); |
| 32 | + _doubleAnswerFormat = |
| 33 | + widget.questionStep.answerFormat as DoubleAnswerFormat; |
| 34 | + _controller = TextEditingController(); |
| 35 | + _controller.text = widget.result?.result?.toString() ?? ''; |
| 36 | + _checkValidation(_controller.text); |
| 37 | + _startDate = DateTime.now(); |
| 38 | + } |
| 39 | + |
| 40 | + @override |
| 41 | + void dispose() { |
| 42 | + _controller.dispose(); |
| 43 | + super.dispose(); |
| 44 | + } |
| 45 | + |
| 46 | + void _checkValidation(String text) { |
| 47 | + setState(() { |
| 48 | + _isValid = text.isNotEmpty && double.tryParse(text) != null; |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + @override |
| 53 | + Widget build(BuildContext context) { |
| 54 | + return StepView( |
| 55 | + step: widget.questionStep, |
| 56 | + resultFunction: () => DoubleQuestionResult( |
| 57 | + id: widget.questionStep.stepIdentifier, |
| 58 | + startDate: _startDate, |
| 59 | + endDate: DateTime.now(), |
| 60 | + valueIdentifier: _controller.text, |
| 61 | + result: double.tryParse(_controller.text) ?? |
| 62 | + _doubleAnswerFormat.defaultValue ?? |
| 63 | + null, |
| 64 | + ), |
| 65 | + isValid: _isValid || widget.questionStep.isOptional, |
| 66 | + title: widget.questionStep.title.isNotEmpty |
| 67 | + ? Text( |
| 68 | + widget.questionStep.title, |
| 69 | + style: Theme.of(context).textTheme.headline2, |
| 70 | + textAlign: TextAlign.center, |
| 71 | + ) |
| 72 | + : widget.questionStep.content, |
| 73 | + child: Padding( |
| 74 | + padding: const EdgeInsets.symmetric(vertical: 32.0), |
| 75 | + child: Container( |
| 76 | + width: MediaQuery.of(context).size.width, |
| 77 | + child: TextField( |
| 78 | + decoration: textFieldInputDecoration( |
| 79 | + hint: _doubleAnswerFormat.hint, |
| 80 | + ), |
| 81 | + controller: _controller, |
| 82 | + onChanged: (String value) { |
| 83 | + _checkValidation(value); |
| 84 | + }, |
| 85 | + keyboardType: TextInputType.number, |
| 86 | + textAlign: TextAlign.center, |
| 87 | + ), |
| 88 | + ), |
| 89 | + ), |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
0 commit comments