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/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<EnergyBilanceModel>(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),
|
||||
]
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue