148 lines
5.0 KiB
Dart
148 lines
5.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'result_model.dart';
|
|
|
|
void main() => runApp(const MyApp());
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MultiProvider(
|
|
providers: [
|
|
ChangeNotifierProvider.value(
|
|
value: ResultModel(),
|
|
),
|
|
],
|
|
child: MaterialApp(
|
|
title: 'Energy',
|
|
home: MyCustomFormState(title: 'Energy'),
|
|
));
|
|
}
|
|
}
|
|
|
|
class MyCustomFormState extends StatelessWidget with InputValidationMixin {
|
|
final String title;
|
|
|
|
MyCustomFormState({required this.title});
|
|
|
|
final formGlobalKey = GlobalKey<FormState>();
|
|
|
|
final _controllerStartHeight = TextEditingController();
|
|
final _controllerEndHeight = TextEditingController();
|
|
final _controllerStartSpeed = TextEditingController();
|
|
final _controllerEndSpeed = TextEditingController();
|
|
final _controllerWeight = TextEditingController();
|
|
|
|
@override
|
|
void dispose() {
|
|
_controllerStartSpeed.dispose();
|
|
_controllerEndSpeed.dispose();
|
|
_controllerWeight.dispose();
|
|
}
|
|
|
|
void _changeJoule(BuildContext context) {
|
|
Provider.of<ResultModel>(context, listen: false).changeJoule(
|
|
double.parse(_controllerWeight.text),
|
|
double.parse(_controllerStartSpeed.text),
|
|
double.parse(_controllerEndSpeed.text),
|
|
double.parse(_controllerStartHeight.text),
|
|
double.parse(_controllerEndHeight.text),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
double _joule = Provider.of<ResultModel>(context).getJoule;
|
|
String _energyWater = Provider.of<ResultModel>(context).getWater;
|
|
String _energyIron = Provider.of<ResultModel>(context).getIron;
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(title),
|
|
),
|
|
body: Form(
|
|
key: formGlobalKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
TextFormField(
|
|
controller: _controllerStartHeight,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Start height',
|
|
hintText: 'Enter the height at the start (in meters)'),
|
|
validator: (value) {
|
|
if (isInputValid(value.toString())) {
|
|
return 'Please enter a floating point number';
|
|
}
|
|
},
|
|
),
|
|
TextFormField(
|
|
controller: _controllerEndHeight,
|
|
decoration: const InputDecoration(
|
|
labelText: 'End height',
|
|
hintText: 'Enter the height at the end (in meters)'),
|
|
validator: (value) {
|
|
if (isInputValid(value.toString())) {
|
|
return 'Please enter a floating point number';
|
|
}
|
|
},
|
|
),
|
|
TextFormField(
|
|
controller: _controllerStartSpeed,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Start speed',
|
|
hintText: 'Enter the speed at the end (in m/s)'),
|
|
validator: (value) {
|
|
if (isInputValid(value.toString())) {
|
|
return 'Please enter a floating point number';
|
|
}
|
|
},
|
|
),
|
|
TextFormField(
|
|
controller: _controllerEndSpeed,
|
|
decoration: const InputDecoration(
|
|
labelText: 'End speed',
|
|
hintText: 'Enter the speed at the end (in m/s)'),
|
|
validator: (value) {
|
|
if (isInputValid(value.toString())) {
|
|
return 'Please enter a floating point number';
|
|
}
|
|
},
|
|
),
|
|
TextFormField(
|
|
controller: _controllerWeight,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Weight',
|
|
hintText: 'Enter the weight of the object (in kg)'),
|
|
validator: (value) {
|
|
if (isInputValid(value.toString())) {
|
|
return 'Please enter a floating point number';
|
|
}
|
|
},
|
|
),
|
|
Text('$_joule Joule \n'
|
|
'$_energyWater Liter Wasser können damit erhitzt werden \n'
|
|
'$_energyIron kg Eisen können damit zu glühen gebracht werden'),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
|
child: ElevatedButton(
|
|
onPressed: () => {
|
|
if (formGlobalKey.currentState!.validate()) {
|
|
formGlobalKey.currentState!.save()
|
|
},
|
|
_changeJoule(context),
|
|
},
|
|
child: const Text('Submit'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
));
|
|
}
|
|
}
|
|
|
|
mixin InputValidationMixin {
|
|
bool isInputValid(String value) => new RegExp(r'[0-9]+.?[0-9]*').hasMatch(value);
|
|
}
|