From 565a49a6c68ed9c17986612f39c554b6e735e97b Mon Sep 17 00:00:00 2001 From: Christoph Giess Date: Wed, 12 Apr 2023 08:41:41 +0200 Subject: [PATCH] Added UI --- .gitignore | 1 + lib/{models => algorithms}/energy.dart | 4 +- lib/{models => algorithms}/heat_capacity.dart | 0 lib/{models => algorithms}/velocity.dart | 0 lib/main.dart | 43 +++++++++++++++++++ lib/models/energy_model.dart | 43 +++++++++++++++++++ lib/widgets/energy_input_widget.dart | 40 +++++++++++++++++ lib/widgets/energy_widget.dart | 29 +++++++++++++ lib/widgets/heat_capacity_widget.dart | 19 ++++++++ pubspec.yaml | 3 +- test/{ => algorithms}/energy_test.dart | 2 +- test/{ => algorithms}/heat_capacity_test.dart | 2 +- 12 files changed, 181 insertions(+), 5 deletions(-) rename lib/{models => algorithms}/energy.dart (94%) rename lib/{models => algorithms}/heat_capacity.dart (100%) rename lib/{models => algorithms}/velocity.dart (100%) create mode 100644 lib/main.dart create mode 100644 lib/models/energy_model.dart create mode 100644 lib/widgets/energy_input_widget.dart create mode 100644 lib/widgets/energy_widget.dart create mode 100644 lib/widgets/heat_capacity_widget.dart rename test/{ => algorithms}/energy_test.dart (97%) rename test/{ => algorithms}/heat_capacity_test.dart (83%) diff --git a/.gitignore b/.gitignore index 7fc9c66..62520f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # ---> Dart # See https://www.dartlang.org/guides/libraries/private-files +.idea # Files and directories created by pub .dart_tool/ diff --git a/lib/models/energy.dart b/lib/algorithms/energy.dart similarity index 94% rename from lib/models/energy.dart rename to lib/algorithms/energy.dart index 55cc0b1..f01453f 100644 --- a/lib/models/energy.dart +++ b/lib/algorithms/energy.dart @@ -1,11 +1,11 @@ /// free fall acceleration in m/s² -const double g = 9.81; +const double _g = 9.81; /// /// Potential Energy = [mass] * [height] * g /// with [mass] in kg, [height] in m and result in m²kg/s² = Joule double potential(double mass, double height) { - return mass * height * g; + return mass * height * _g; } double freedPotential(double mass, double hStart, double hEnd) { diff --git a/lib/models/heat_capacity.dart b/lib/algorithms/heat_capacity.dart similarity index 100% rename from lib/models/heat_capacity.dart rename to lib/algorithms/heat_capacity.dart diff --git a/lib/models/velocity.dart b/lib/algorithms/velocity.dart similarity index 100% rename from lib/models/velocity.dart rename to lib/algorithms/velocity.dart diff --git a/lib/main.dart b/lib/main.dart new file mode 100644 index 0000000..a85a827 --- /dev/null +++ b/lib/main.dart @@ -0,0 +1,43 @@ +import 'package:energy/widgets/energy_input_widget.dart'; +import 'package:energy/widgets/energy_widget.dart'; +import 'package:energy/widgets/heat_capacity_widget.dart'; +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; + +import 'models/energy_model.dart'; + +void main() => runApp(const EnergyApp()); + +class EnergyApp extends StatelessWidget { + const EnergyApp({super.key}); + + @override + Widget build(BuildContext context) { + return ChangeNotifierProvider( + create: (_) => EnergyModel(), + child: const MaterialApp( + home: EnergyHomePage(), + ), + ); + } +} + +class EnergyHomePage extends StatelessWidget { + const EnergyHomePage({super.key}); + + @override + Widget build(BuildContext context) { + final EnergyModel e = context.watch(); + + return Scaffold( + appBar: AppBar(title: const Text('Energy Calculation')), + body: Column( + children: [ + EnergyInputWidget(e: e), + EnergyWidget(energy: e.energy), + HeatCapacityWidget(water: e.water, iron: e.iron), + ], + ), + ); + } +} diff --git a/lib/models/energy_model.dart b/lib/models/energy_model.dart new file mode 100644 index 0000000..1dd98dd --- /dev/null +++ b/lib/models/energy_model.dart @@ -0,0 +1,43 @@ +import 'package:energy/algorithms/energy.dart'; +import 'package:energy/algorithms/heat_capacity.dart'; +import 'package:flutter/material.dart'; + +class EnergyModel with ChangeNotifier { + double _mass = 0.0; // in kg + double _hStart = 0.0; // in m + double _hEnd = 0.0; + double _vStart = 0.0; // in m/s + double _vEnd = 0.0; + + double get mass => _mass; + double get hStart => _hStart; + double get hEnd => _hEnd; + double get vStart => _vStart; + double get vEnd => _vEnd; + + set mass(double mass) { + _mass = mass; + notifyListeners(); + } + set hStart(double height) { + _hStart = height; + notifyListeners(); + } + set hEnd(double height) { + _hEnd = height; + notifyListeners(); + } + set vStart(double velocity) { + _vStart = velocity; + notifyListeners(); + } + set vEnd(double height) { + _vEnd = height; + notifyListeners(); + } + + get energy => freedEnergy(_mass, _hStart, _hEnd, _vStart, _vEnd); + get water => literWaterThatCanBeBoiled(energy); + get iron => kgIronThatCanBeHeatetUntilRed(energy); + +} \ No newline at end of file diff --git a/lib/widgets/energy_input_widget.dart b/lib/widgets/energy_input_widget.dart new file mode 100644 index 0000000..96e43cc --- /dev/null +++ b/lib/widgets/energy_input_widget.dart @@ -0,0 +1,40 @@ +import 'package:energy/algorithms/velocity.dart'; +import 'package:flutter/material.dart'; + +import '../models/energy_model.dart'; + +class EnergyInputWidget extends StatelessWidget { + const EnergyInputWidget({ + super.key, + required this.e, + }); + + final EnergyModel e; + + @override + Widget build(BuildContext context) { + return Column( + children: [ + Text("Mass ${e.mass.round()} kg"), + Slider( + value: e.mass, + max: 50000, + divisions: 1000, + label: "Mass", + onChanged: (double value) { + e.mass = value; + }, + ), + Text("Initial Velocity ${e.vStart.round()} m/s"), + Slider( + value: e.vStart, + max: 1000, + divisions: 1000, + onChanged: (double value) { + e.vStart = value; + }, + ), + ], + ); + } +} diff --git a/lib/widgets/energy_widget.dart b/lib/widgets/energy_widget.dart new file mode 100644 index 0000000..ef11185 --- /dev/null +++ b/lib/widgets/energy_widget.dart @@ -0,0 +1,29 @@ +import 'package:flutter/material.dart'; + +class EnergyWidget extends StatelessWidget { + const EnergyWidget({ + super.key, + required this.energy, + }); + + final double energy; + + @override + Widget build(BuildContext context) { + return Text("Energy ${_format(energy)}Joule", style: Theme.of(context).textTheme.headlineLarge,); + } + + String _format(double e) { + if (e > 1000000000000) { + return "${(energy / 1000000000000).round()} T"; + } else if (e > 1000000000) { + return "${(energy / 1000000000).round()} G"; + } else if (e > 1000000) { + return "${(energy / 1000000).round()} M"; + } else if ( e > 1000) { + return "${(energy / 1000).round()} k"; + } else { + return "${energy.round()} "; + } + } +} \ No newline at end of file diff --git a/lib/widgets/heat_capacity_widget.dart b/lib/widgets/heat_capacity_widget.dart new file mode 100644 index 0000000..e0f6520 --- /dev/null +++ b/lib/widgets/heat_capacity_widget.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; + +class HeatCapacityWidget extends StatelessWidget { + const HeatCapacityWidget({ + super.key, + required this.water, + required this.iron, + }); + + final double water; + final double iron; + + @override + Widget build(BuildContext context) { + return Column(children: [ + Text("${water.round()} liter water can be boiled", style: Theme.of(context).textTheme.headlineLarge,), + Text("${iron.round()} kg iron can be heated", style: Theme.of(context).textTheme.headlineLarge,),]); + } +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index acbc66c..82a68b4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -6,11 +6,12 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.0.0+1 environment: - sdk: ">=2.18.0 <3.0.0" + sdk: ">=2.19.0 <3.0.0" dependencies: flutter: sdk: flutter + provider: ^6.0.0 dev_dependencies: test: diff --git a/test/energy_test.dart b/test/algorithms/energy_test.dart similarity index 97% rename from test/energy_test.dart rename to test/algorithms/energy_test.dart index f9b1e41..7ae0db7 100644 --- a/test/energy_test.dart +++ b/test/algorithms/energy_test.dart @@ -1,4 +1,4 @@ -import 'package:energy/models/energy.dart'; +import 'package:energy/algorithms/energy.dart'; import 'package:test/test.dart'; void main() { diff --git a/test/heat_capacity_test.dart b/test/algorithms/heat_capacity_test.dart similarity index 83% rename from test/heat_capacity_test.dart rename to test/algorithms/heat_capacity_test.dart index 067c20a..feacc10 100644 --- a/test/heat_capacity_test.dart +++ b/test/algorithms/heat_capacity_test.dart @@ -1,4 +1,4 @@ -import 'package:energy/models/heat_capacity.dart'; +import 'package:energy/algorithms/heat_capacity.dart'; import 'package:test/test.dart'; void main() {