Commit 1c9e750
Changed files (2)
lib
components
dialoges
test
ui
components
lib/components/dialoges/add_measurement.dart
@@ -34,6 +34,7 @@ class AddMeasurementDialoge extends StatefulWidget {
class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
final formKey = GlobalKey<FormState>();
final firstFocusNode = FocusNode();
+ late final TextEditingController sysController;
/// Currently selected time.
late DateTime time;
@@ -58,6 +59,7 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
super.initState();
time = widget.initialRecord?.creationTime ?? DateTime.now();
needlePin = widget.initialRecord?.needlePin;
+ sysController = TextEditingController(text: (widget.initialRecord?.systolic ?? '').toString());
firstFocusNode.requestFocus();
}
@@ -111,14 +113,18 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
String? hintText,
void Function(String?)? onSaved,
FocusNode? focusNode,
+ TextEditingController? controller,
+ String? Function(String?)? validator,
}) {
+ assert(initialValue == null || controller == null);
return Expanded(
child: TextFormField(
- initialValue: (initialValue ?? '').toString(),
+ initialValue: initialValue?.toString(),
decoration: getInputDecoration(hintText),
keyboardType: TextInputType.number,
focusNode: focusNode,
onSaved: onSaved,
+ controller: controller,
inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
onChanged: (String? value) {
if (value != null && value.isNotEmpty && (int.tryParse(value) ?? -1) > 40) {
@@ -134,8 +140,7 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
// https://pubmed.ncbi.nlm.nih.gov/7741618/
return localizations.errUnrealistic;
}
- // TODO: check dia > sys
- return null;
+ return validator?.call(value);
},
),
);
@@ -196,7 +201,7 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
buildValueInput(localizations,
focusNode: firstFocusNode,
hintText: localizations.sysLong,
- initialValue: widget.initialRecord?.systolic,
+ controller: sysController,
onSaved: (value) => setState(() => systolic = int.tryParse(value ?? '')),
),
const SizedBox(width: 16,),
@@ -204,6 +209,12 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
hintText: localizations.diaLong,
initialValue: widget.initialRecord?.diastolic,
onSaved: (value) => setState(() => diastolic = int.tryParse(value ?? '')),
+ validator: (value) {
+ if (widget.settings.validateInputs && (int.tryParse(value ?? '') ?? 0) >= (int.tryParse(sysController.text) ?? 1)) {
+ return AppLocalizations.of(context)?.errDiaGtSys;
+ }
+ return null;
+ }
),
const SizedBox(width: 16,),
buildValueInput(localizations,
test/ui/components/add_measurement_dialoge_test.dart
@@ -147,6 +147,8 @@ void main() {
expect(find.text(localizations.errNaN), findsNothing);
expect(find.text(localizations.errLt30), findsNothing);
expect(find.text(localizations.errUnrealistic), findsNothing);
+ expect(find.text(localizations.errDiaGtSys), findsNothing);
+
await widgetTester.enterText(find.ancestor(of: find.text('Systolic').first, matching: find.byType(TextFormField)), '123');
await widgetTester.enterText(find.ancestor(of: find.text('Diastolic').first, matching: find.byType(TextFormField)), '67');
@@ -155,6 +157,9 @@ void main() {
await widgetTester.pumpAndSettle();
expect(find.byType(AddMeasurementDialoge), findsOneWidget);
expect(find.text(localizations.errNaN), findsOneWidget);
+ expect(find.text(localizations.errLt30), findsNothing);
+ expect(find.text(localizations.errUnrealistic), findsNothing);
+ expect(find.text(localizations.errDiaGtSys), findsNothing);
await widgetTester.enterText(find.ancestor(of: find.text('Pulse').first, matching: find.byType(TextFormField)), '20');
await widgetTester.tap(find.text('SAVE'));
@@ -162,6 +167,8 @@ void main() {
expect(find.byType(AddMeasurementDialoge), findsOneWidget);
expect(find.text(localizations.errNaN), findsNothing);
expect(find.text(localizations.errLt30), findsOneWidget);
+ expect(find.text(localizations.errUnrealistic), findsNothing);
+ expect(find.text(localizations.errDiaGtSys), findsNothing);
await widgetTester.enterText(find.ancestor(of: find.text('Pulse').first, matching: find.byType(TextFormField)), '60');
await widgetTester.enterText(find.ancestor(of: find.text('Diastolic').first, matching: find.byType(TextFormField)), '500');
@@ -171,14 +178,28 @@ void main() {
expect(find.text(localizations.errNaN), findsNothing);
expect(find.text(localizations.errLt30), findsNothing);
expect(find.text(localizations.errUnrealistic), findsOneWidget);
+ expect(find.text(localizations.errDiaGtSys), findsNothing);
+
+ await widgetTester.enterText(find.ancestor(of: find.text('Diastolic').first, matching: find.byType(TextFormField)), '100');
+ await widgetTester.enterText(find.ancestor(of: find.text('Systolic').first, matching: find.byType(TextFormField)), '90');
+ await widgetTester.tap(find.text('SAVE'));
+ await widgetTester.pumpAndSettle();
+ expect(find.byType(AddMeasurementDialoge), findsOneWidget);
+ expect(find.text(localizations.errNaN), findsNothing);
+ expect(find.text(localizations.errLt30), findsNothing);
+ expect(find.text(localizations.errUnrealistic), findsNothing);
+ expect(find.text(localizations.errDiaGtSys), findsOneWidget);
- await widgetTester.enterText(find.ancestor(of: find.text('Diastolic').first, matching: find.byType(TextFormField)), '80');
+
+ await widgetTester.enterText(find.ancestor(of: find.text('Diastolic').first, matching: find.byType(TextFormField)), '78');
+ await widgetTester.enterText(find.ancestor(of: find.text('Systolic').first, matching: find.byType(TextFormField)), '123');
await widgetTester.tap(find.text('SAVE'));
await widgetTester.pumpAndSettle();
expect(find.byType(AddMeasurementDialoge), findsNothing);
expect(find.text(localizations.errNaN), findsNothing);
expect(find.text(localizations.errLt30), findsNothing);
expect(find.text(localizations.errUnrealistic), findsNothing);
+ expect(find.text(localizations.errDiaGtSys), findsNothing);
});
testWidgets('should allow invalid values when setting is set', (widgetTester) async {
await widgetTester.pumpWidget(_materialApp(
@@ -200,7 +221,7 @@ void main() {
await widgetTester.pumpWidget(_materialApp(
Builder(
builder: (BuildContext context) => TextButton(onPressed: () async {
- await showAddMeasurementDialoge(context, Settings(validateInputs: false, allowMissingValues: true));
+ await showAddMeasurementDialoge(context, Settings(allowManualTimeInput: false));
}, child: const Text('TEST')),
)));
await widgetTester.tap(find.text('TEST'));