cpd_2022_energy/lib/main.dart

150 lines
5.2 KiB
Dart
Raw Normal View History

2022-11-08 20:37:41 +01:00
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();
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';
}
},
),
2022-11-09 08:41:37 +01:00
FittedBox(
child: Text(
'$_joule Joule \n'
'$_energyWater Liter Wasser können damit erhitzt werden \n'
'$_energyIron kg Eisen können damit zu glühen gebracht werden',
style: new TextStyle(
fontSize: 20.0,
color: Colors.black,
))),
2022-11-08 20:37:41 +01:00
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: () => {
2022-11-09 08:41:37 +01:00
if (formGlobalKey.currentState!.validate())
{formGlobalKey.currentState!.save()},
2022-11-08 20:37:41 +01:00
_changeJoule(context),
},
2022-11-09 08:41:37 +01:00
child: Text('Submit',
style: new TextStyle(
fontSize: 20.0,
)),
2022-11-08 20:37:41 +01:00
),
),
],
),
));
}
}
mixin InputValidationMixin {
2022-11-09 08:41:37 +01:00
bool isInputValid(String value) =>
!new RegExp(r'[0-9]+.?[0-9]*').hasMatch(value);
2022-11-08 20:37:41 +01:00
}