import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; void main() { runApp(const EnergyProviderApp()); } class EnergyModel extends ChangeNotifier { String startheight = ""; String endheight = ""; String startspeed=""; String endspeed=""; String weight=""; EnergyModel(){ startheight; endheight; startspeed; endspeed; weight; } void setStartheight(String input){ startheight=input; } void setEndheight(String input){ endheight= input; } void setStartspeed(String input){ startspeed=input; } void setEndspeed(String input){ endspeed=input; } void setWeight(String input){ weight=input; } void changeInput(String input) { startheight = input; notifyListeners(); } } class KineticModel extends ChangeNotifier{ String startheight = ""; String endheight = ""; String startspeed=""; String endspeed=""; String weight=""; KineticModel(){ startspeed; endspeed; weight; } void setStartspeed(String input){ startspeed=input; } void setEndspeed(String input){ endspeed=input; } void setWeight(String input){ weight=input; } void update(){ notifyListeners(); } } class PotentialModel extends ChangeNotifier{ String startheight = ""; String endheight = ""; String weight=""; PotentialModel(){ startheight; endheight; weight; } void setStartheight(String input){ startheight=input; } void setEndheight(String input){ endheight=input; } void setWeight(String input){ weight=input; } void update(){ notifyListeners(); } } class PotentialCalc extends StatelessWidget{ PotentialCalc({super.key}); final _formKey = GlobalKey(); double calcPotentialE(double mass, double startheight, double endheight){ double height = (startheight-endheight).abs(); return mass * 9.81 * height; } double calcWater(double e){ return e/(80*4190); } double calcIron(double e){ return e/(460*680); } double joule=0.0; double water=0.0; double iron=0.0; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Potentielle Energie"), ), body: ChangeNotifierProvider( create: (_) => PotentialModel(), child: Center( child: Column( children: [ Consumer( builder: (context, model, child) => Column( children: [ Center( child: Form( key: _formKey, child: Column( children: [ const Padding( padding: const EdgeInsets.all(8.0), child: const Text("Geben Sie Ihre Werte ein"), ), //Anfangsgeschwindigkeit Padding( padding: const EdgeInsets.only(left: 8, top: 40, right: 8, bottom: 0), child: TextFormField( onSaved: (String? val) { model.setStartheight(val!); }, keyboardType: TextInputType.numberWithOptions(decimal: true), inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r"^((\d+)?(\.)?(\d{1,2})?)")) ], decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Anfangshöhe in Meter", hintText: "Geben Sie eine Dezimalzahl an", ), ), ), //Endgeschwindigkeit Padding( padding: const EdgeInsets.only(left: 8, top: 8, right: 8, bottom: 0), child: TextFormField( onSaved: (String? val) { model.setEndheight(val!); }, keyboardType: TextInputType.numberWithOptions(decimal: true), inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r"^((\d+)?(\.)?(\d{1,2})?)")) ], decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Endhöhe in Meter", hintText: "Geben Sie eine Dezimalzahl an", ), ), ), // Gewicht des Objekts Padding( padding: const EdgeInsets.only(left: 8, top: 40, right: 8, bottom: 0), child: TextFormField( onSaved: (String? val) { model.setWeight(val!); }, keyboardType: TextInputType.numberWithOptions(decimal: true), inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r"^((\d+)?(\.)?(\d{1,2})?)")) ], decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Gewicht in Kilogramm", hintText: "Geben Sie eine Dezimalzahl an", ), ), ), IconButton( onPressed: () { if(_formKey.currentState!.validate()){ _formKey.currentState!.save(); } joule=calcPotentialE(double.parse(model.weight), double.parse(model.startheight), double.parse(model.endheight)); water=calcWater(joule); iron=calcIron(joule); model.update(); }, icon: const Icon(Icons.calculate, color: Colors.blue), tooltip: 'Berechne', ), //Divider const Divider( height: 40, thickness: 2, color: Colors.black, ), const Text("Ergebnis:"), Padding( padding: const EdgeInsets.only(left: 8, top: 16, right: 8, bottom: 0), child: Text( joule.toStringAsFixed(2) +"Joule" ), ), Padding( padding: const EdgeInsets.only(left: 8, top: 16, right: 8, bottom: 0), child: Text( "Damit kann man "+ water.toStringAsFixed(2) +"l Wasser zum Kochen bringen" ), ), Padding( padding: const EdgeInsets.only(left: 8, top: 8, right: 8, bottom: 0), child: Text( "Damit kann man "+ iron.toStringAsFixed(2) +"kg Eisen zum Glühen bringen" ), ), ], ), ), ), ], ) ), ], ), ), ), //TextButton(child: const Text("Berechne"),onPressed: () { },) ); } } class KineticCalc extends StatelessWidget{ KineticCalc({super.key}); final _formKey = GlobalKey(); double calcKineticE(double mass, double startspeed, double endspeed){ double speed = (startspeed-endspeed).abs(); return mass * speed * speed/2; } double calcWater(double e){ return (e/(80*4190)); } double calcIron(double e){ return e/(460*680); } double joule=0.0; double water=0; double iron=0.0; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Kinetische Energie"), ), body: ChangeNotifierProvider( create: (_) => KineticModel(), child: Center( child: Column( children: [ Consumer( builder: (context, model, child) => Column( children: [ Center( child: Form( key: _formKey, child: Column( children: [ Padding( padding: const EdgeInsets.all(8.0), child: const Text("Geben Sie Ihre Werte ein"), ), //Anfangsgeschwindigkeit Padding( padding: const EdgeInsets.only(left: 8, top: 40, right: 8, bottom: 0), child: TextFormField( onSaved: (String? val) { model.setStartspeed(val!); }, keyboardType: TextInputType.numberWithOptions(decimal: true), inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r"^((\d+)?(\.)?(\d{1,2})?)")) ], decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Anfangsgeschwindigkeit in Meter/Sekunde", hintText: "Geben Sie eine Dezimalzahl an", ), ), ), //Endgeschwindigkeit Padding( padding: const EdgeInsets.only(left: 8, top: 8, right: 8, bottom: 0), child: TextFormField( onSaved: (String? val) { model.setEndspeed(val!); }, keyboardType: TextInputType.numberWithOptions(decimal: true), inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r"^((\d+)?(\.)?(\d{1,2})?)")) ], decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Endgeschwindigkeit in Meter/Sekunde", hintText: "Geben Sie eine Dezimalzahl an", ), ), ), // Gewicht des Objekts Padding( padding: const EdgeInsets.only(left: 8, top: 40, right: 8, bottom: 0), child: TextFormField( onSaved: (String? val) { model.setWeight(val!); }, keyboardType: TextInputType.numberWithOptions(decimal: true), inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r"^((\d+)?(\.)?(\d{1,2})?)")) ], decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Gewicht in Kilogramm", hintText: "Geben Sie eine Dezimalzahl an", ), ), ), IconButton( onPressed: () { if(_formKey.currentState!.validate()){ _formKey.currentState!.save(); } joule=calcKineticE(double.parse(model.weight), double.parse(model.startspeed), double.parse(model.endspeed)); water=calcWater(joule); iron=calcIron(joule); model.update(); }, icon: const Icon(Icons.calculate, color: Colors.blue), tooltip: 'Berechne', ), //Divider const Divider( height: 40, thickness: 2, color: Colors.black, ), const Text("Ergebnis:"), Padding( padding: const EdgeInsets.only(left: 8, top: 16, right: 8, bottom: 0), child: Text( joule.toStringAsFixed(2)+" Joule" ), ), Padding( padding: const EdgeInsets.only(left: 8, top: 16, right: 8, bottom: 0), child: Text( "Damit kann man "+ water.toStringAsFixed(2) +"l Wasser zum Kochen bringen" ), ), Padding( padding: const EdgeInsets.only(left: 8, top: 8, right: 8, bottom: 0), child: Text( "Damit kann man "+ iron.toStringAsFixed(2) +"kg Eisen zum Glühen bringen" ), ), ], ), ), ), ], ) ), ], ), ), ), //TextButton(child: const Text("Berechne"),onPressed: () { },) ); } } class EnergyProviderApp extends StatelessWidget { const EnergyProviderApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Energiebilanz', theme: ThemeData( primarySwatch: Colors.blue, ), home: const EnergyApp(title: 'Energiebilanz Rechner'), routes: { "/kinetic": (context) => KineticCalc(), "/potential": (context) => PotentialCalc(), }, ); } } class EnergyApp extends StatefulWidget { const EnergyApp({super.key, required this.title}); final String title; @override State createState() => _EnergyAppState(); } class _EnergyAppState extends State { double calcPotentialE(double mass, double startheight, double endheight){ return mass * 9.81 * (startheight - endheight).abs(); } double calcKineticE(double mass, double startspeed, double endspeed){ double speed = (startspeed-endspeed).abs(); return mass * speed * speed; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( children: [ Padding( padding: const EdgeInsets.all(8.0), child: ElevatedButton( onPressed: () {Navigator.pushNamed(context,"/kinetic"); }, child: const Text("Kinetische Energie berechnen"), ), ), Padding( padding: const EdgeInsets.all(8.0), child: ElevatedButton( onPressed: () => {Navigator.pushNamed(context, "/potential")}, child: const Text("Potentielle Energie berechnen"), ), ), ], ), ), ); } }