164 lines
4.9 KiB
Dart
164 lines
4.9 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:lamiter/Class/Question/question.dart';
|
|
import 'package:lamiter/Class/Question/question_layout_attributes.dart';
|
|
import 'package:lamiter/Component/q_title.dart';
|
|
import 'package:lamiter/Extension/build_context.dart';
|
|
import 'package:lamiter/Provider/Form/form_provider.dart';
|
|
import 'package:lamiter/Provider/Language/language_provider.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
// ignore: must_be_immutable
|
|
class DTQ<T extends FormProvider> extends Question<DateTime> {
|
|
final GlobalKey<DTQLayoutState> DTQKey = GlobalKey<DTQLayoutState>();
|
|
final TextEditingController controller = TextEditingController();
|
|
final DTQLayoutAttributes attributes;
|
|
|
|
DTQ({
|
|
super.key,
|
|
required super.id,
|
|
required super.required,
|
|
super.answer,
|
|
required this.attributes,
|
|
});
|
|
|
|
String? _datetime2string(DateTime? dt) {
|
|
return dt == null ? null : DateFormat("yyyy/MM/dd").format(dt);
|
|
}
|
|
|
|
@override
|
|
void setAnswerLayout(DateTime? _answer) {
|
|
if (_answer == null) return;
|
|
answer = _answer;
|
|
controller.text = _datetime2string(_answer) ?? '';
|
|
}
|
|
|
|
@override
|
|
void lockAnswerLayouot() {
|
|
DTQKey.currentState?.lockAnswer();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
void layoutSetAnswer(DateTime? layoutAnswer) {
|
|
answer = layoutAnswer;
|
|
context.read<T>().refresh();
|
|
}
|
|
|
|
return _Layout(
|
|
key: DTQKey,
|
|
controller: controller,
|
|
attributes: attributes,
|
|
datetime2string: _datetime2string,
|
|
setAnswer: layoutSetAnswer,
|
|
);
|
|
}
|
|
}
|
|
|
|
class DTQLayoutAttributes extends QuestionLayoutAttributes {
|
|
final String? hintText;
|
|
|
|
DTQLayoutAttributes({
|
|
super.title,
|
|
required super.required,
|
|
this.hintText,
|
|
});
|
|
}
|
|
|
|
class _Layout extends StatefulWidget {
|
|
// Date-time question
|
|
final TextEditingController controller;
|
|
final DTQLayoutAttributes attributes;
|
|
final String? Function(DateTime?) datetime2string;
|
|
final Function(DateTime?) setAnswer;
|
|
|
|
const _Layout({
|
|
super.key,
|
|
required this.controller,
|
|
required this.attributes,
|
|
required this.datetime2string,
|
|
required this.setAnswer,
|
|
});
|
|
|
|
@override
|
|
State<_Layout> createState() => DTQLayoutState();
|
|
}
|
|
|
|
class DTQLayoutState extends State<_Layout> {
|
|
bool _readOnly = false;
|
|
|
|
void lockAnswer() {
|
|
_readOnly = true;
|
|
setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final languageProvider = Provider.of<LanguageProvider>(context);
|
|
|
|
return Form(
|
|
child: Column(
|
|
children: [
|
|
widget.attributes.title != null && widget.attributes.title!.isNotEmpty
|
|
? QTitle(
|
|
title: languageProvider
|
|
.getLocaleString(widget.attributes.title!),
|
|
readOnly: _readOnly,
|
|
required: widget.attributes.required,
|
|
)
|
|
: const SizedBox.shrink(),
|
|
TextFormField(
|
|
controller: widget.controller,
|
|
minLines: 1,
|
|
autocorrect: false,
|
|
readOnly: true, // not allow use directly typing
|
|
keyboardType: TextInputType.none,
|
|
decoration: InputDecoration(
|
|
isDense: true,
|
|
hintText: languageProvider
|
|
.getLocaleString(widget.attributes.hintText ?? ''),
|
|
hintStyle: context.bL!.copyWith(color: context.primary),
|
|
enabledBorder:
|
|
UnderlineInputBorder(borderSide: context.QBorderSide),
|
|
),
|
|
onTap: () {
|
|
if (_readOnly) return;
|
|
showCupertinoModalPopup<void>(
|
|
context: context,
|
|
builder: (BuildContext context) => SizedBox(
|
|
height: 216.sp,
|
|
child: Container(
|
|
padding: EdgeInsets.only(bottom: 18.sp),
|
|
decoration: BoxDecoration(
|
|
color: context.secondary,
|
|
borderRadius: const BorderRadius.only(
|
|
topLeft: Radius.circular(20),
|
|
topRight: Radius.circular(20),
|
|
),
|
|
),
|
|
child: CupertinoDatePicker(
|
|
initialDateTime: DateTime.now(),
|
|
maximumDate: DateTime.now(),
|
|
mode: CupertinoDatePickerMode.date,
|
|
use24hFormat: true,
|
|
showDayOfWeek: false,
|
|
onDateTimeChanged: (DateTime newDate) {
|
|
widget.setAnswer(newDate);
|
|
widget.controller.text =
|
|
widget.datetime2string(newDate) ?? '';
|
|
setState(() {});
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|