From 50b337e1cdc3fa3d93e1500e1fbce2b14ecfb3b5 Mon Sep 17 00:00:00 2001 From: Julian Gegner Date: Tue, 8 Nov 2022 22:11:18 +0100 Subject: [PATCH] outsource inputfields, added input validation onChange --- lib/widgets/ExpandedNumberInputWidget.dart | 42 ++++++++++ lib/widgets/MyInputWidget.dart | 97 ++-------------------- 2 files changed, 51 insertions(+), 88 deletions(-) create mode 100644 lib/widgets/ExpandedNumberInputWidget.dart diff --git a/lib/widgets/ExpandedNumberInputWidget.dart b/lib/widgets/ExpandedNumberInputWidget.dart new file mode 100644 index 0000000..d93c729 --- /dev/null +++ b/lib/widgets/ExpandedNumberInputWidget.dart @@ -0,0 +1,42 @@ + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; + +class ExpandedNumberInputWidget extends StatelessWidget { + + String _labelDisplayText = ""; + String _hintText = ""; + void Function(double) _setValue = (x) => {}; + + ExpandedNumberInputWidget(String labelDisplayText, String hintText, void Function(double) setValue, {super.key}) { + _labelDisplayText = labelDisplayText; + _hintText = hintText; + _setValue = setValue; + } + + @override + Widget build(BuildContext context) { + return Expanded( + child: TextFormField( + keyboardType: TextInputType.number, + inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.]'))], + onChanged: (value) { + double? number = double.tryParse(value); + if (number == null){ + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Bitte eine gültige Zahl eingeben')), + ); + }else{ + ScaffoldMessenger.of(context).hideCurrentSnackBar(); + _setValue(number); + } + }, + decoration: InputDecoration( + labelText: _labelDisplayText, + hintText: _hintText, + ), + ) + ); + } + +} \ No newline at end of file diff --git a/lib/widgets/MyInputWidget.dart b/lib/widgets/MyInputWidget.dart index 85407e8..6ca648d 100644 --- a/lib/widgets/MyInputWidget.dart +++ b/lib/widgets/MyInputWidget.dart @@ -1,61 +1,24 @@ -import 'package:energy_bilance/models/EnergyBilanceModel.dart'; +import 'package:energy_bilance/widgets/ExpandedNumberInputWidget.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; +import '../models/EnergyBilanceModel.dart'; + class MyInputWidget extends StatelessWidget { const MyInputWidget({super.key}); @override Widget build(BuildContext context) { - final model = Provider.of(context); - return Column( + return Column( children: [ Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, - children: [ - Expanded( - child: TextFormField( - keyboardType: TextInputType.number, - inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.]'))], - onChanged: (value) { - double number = double.parse(value); - model.setStartVelocity(number); - }, - validator: (value) { - if(value == null){ - return null; - } - final number = num.tryParse(value); - if(number == null){ - return '"$value" ist keine Zahl'; - } - return null; - }, - decoration: const InputDecoration( - labelText: 'Anfangsgeschwindigkeit', - hintText: 'in m/s' - ), - ) - ), - Expanded( - child: TextFormField( - keyboardType: TextInputType.number, - inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.]'))], - onChanged: (value) { - double number = double.parse(value); - model.setEndVelocity(number); - }, - decoration: const InputDecoration( - labelText: 'Endgeschwindigkeit', - hintText: 'in m/s' - ), - ) - ), + ExpandedNumberInputWidget("Anfangsgeschwindigkeit", "in m/s", model.setStartVelocity), + ExpandedNumberInputWidget("Endgeschwindigkeit", "in m/s", model.setEndVelocity) ], ), Row( @@ -63,53 +26,11 @@ class MyInputWidget extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Expanded( - child: TextFormField( - keyboardType: TextInputType.number, - inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.]'))], - onChanged: (value) { - double number = double.parse(value); - model.setStartHeight(number); - }, - decoration: const InputDecoration( - labelText: 'Anfangshöhe', - hintText: 'in m' - ), - ) - ), - Expanded( - child: TextFormField( - keyboardType: TextInputType.number, - inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.]'))], - onChanged: (value) { - double number = double.parse(value); - model.setEndHeight(number); - }, - decoration: const InputDecoration( - labelText: 'Endhöhe', - hintText: 'in m' - ), - ) - ), + ExpandedNumberInputWidget("Anfangshöhe", "in m", model.setStartHeight), + ExpandedNumberInputWidget("Endhöhe", "in m", model.setEndHeight), ], ), - Expanded( - child: TextFormField( - keyboardType: TextInputType.number, - inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.]'))], - onChanged: (value) { - double? number = double.tryParse(value); - if(number != null){ - model.setWeight(number); - } - }, - decoration: const InputDecoration( - labelText: 'Gewicht', - hintText: 'in kg' - ), - ) - ), - + ExpandedNumberInputWidget("Gewicht", "in kg", model.setWeight), ] );