outsource inputfields, added input validation onChange
parent
c8380be8db
commit
50b337e1cd
|
@ -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,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
import '../models/EnergyBilanceModel.dart';
|
||||||
|
|
||||||
class MyInputWidget extends StatelessWidget {
|
class MyInputWidget extends StatelessWidget {
|
||||||
const MyInputWidget({super.key});
|
const MyInputWidget({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
|
||||||
final model = Provider.of<EnergyBilanceModel>(context);
|
final model = Provider.of<EnergyBilanceModel>(context);
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
Row(
|
Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
|
|
||||||
children: [
|
children: [
|
||||||
Expanded(
|
ExpandedNumberInputWidget("Anfangsgeschwindigkeit", "in m/s", model.setStartVelocity),
|
||||||
child: TextFormField(
|
ExpandedNumberInputWidget("Endgeschwindigkeit", "in m/s", model.setEndVelocity)
|
||||||
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'
|
|
||||||
),
|
|
||||||
)
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
Row(
|
Row(
|
||||||
|
@ -63,53 +26,11 @@ class MyInputWidget extends StatelessWidget {
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
|
||||||
children: [
|
children: [
|
||||||
Expanded(
|
ExpandedNumberInputWidget("Anfangshöhe", "in m", model.setStartHeight),
|
||||||
child: TextFormField(
|
ExpandedNumberInputWidget("Endhöhe", "in m", model.setEndHeight),
|
||||||
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'
|
|
||||||
),
|
|
||||||
)
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
Expanded(
|
ExpandedNumberInputWidget("Gewicht", "in kg", model.setWeight),
|
||||||
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'
|
|
||||||
),
|
|
||||||
)
|
|
||||||
),
|
|
||||||
|
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue