From a60e9f968b10817903e8405c9efd2a51b13e347a Mon Sep 17 00:00:00 2001 From: Julian Gegner Date: Mon, 7 Nov 2022 22:12:21 +0100 Subject: [PATCH] added tapbarview to display data --- lib/main.dart | 18 ++++------- lib/models/EnergyBilanceModel.dart | 16 ++++++++-- lib/models/MyInputModel.dart | 12 -------- lib/widgets/EnergyDisplay.dart | 49 ++++++++++++++++++++++++++++++ lib/widgets/MyAppBar.dart | 25 --------------- lib/widgets/MyCustomForm.dart | 43 -------------------------- lib/widgets/MyInputWidget.dart | 2 +- lib/widgets/MyScaffold.dart | 24 --------------- lib/widgets/MyTest.dart | 30 ------------------ lib/widgets/TabDisplayWidget.dart | 19 ++++++++++++ test/EnergyBilanceModelTest.dart | 21 +++++++++++++ 11 files changed, 110 insertions(+), 149 deletions(-) delete mode 100644 lib/models/MyInputModel.dart create mode 100644 lib/widgets/EnergyDisplay.dart delete mode 100644 lib/widgets/MyAppBar.dart delete mode 100644 lib/widgets/MyCustomForm.dart delete mode 100644 lib/widgets/MyScaffold.dart delete mode 100644 lib/widgets/MyTest.dart create mode 100644 lib/widgets/TabDisplayWidget.dart create mode 100644 test/EnergyBilanceModelTest.dart diff --git a/lib/main.dart b/lib/main.dart index 1dec2d5..4de7f05 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,9 +1,7 @@ import 'package:energy_bilance/models/EnergyBilanceModel.dart'; -import 'package:energy_bilance/models/MyInputModel.dart'; -import 'package:energy_bilance/widgets/MyCustomForm.dart'; +import 'package:energy_bilance/widgets/EnergyDisplay.dart'; import 'package:energy_bilance/widgets/MyInputWidget.dart'; -import 'package:energy_bilance/widgets/MyTest.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; @@ -17,15 +15,11 @@ void main() { child: Center( child: Column( mainAxisSize: MainAxisSize.min, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Expanded(child: MyInputWidget()), - Expanded( - child: Consumer( - builder: (context, model, child){ - return Text('Generierte Ekin: ${model.kineticEnergy}'); - } - )) + mainAxisAlignment: MainAxisAlignment.start, + children: const [ + Expanded(child: MyInputWidget()), + Divider(), + Expanded(child: EnergyDisplay()), ], ) diff --git a/lib/models/EnergyBilanceModel.dart b/lib/models/EnergyBilanceModel.dart index a233488..1e96f1c 100644 --- a/lib/models/EnergyBilanceModel.dart +++ b/lib/models/EnergyBilanceModel.dart @@ -11,14 +11,26 @@ class EnergyBilanceModel extends ChangeNotifier { double _weight = 0; - static const double _gravity = 9.81; //in m/s^2 + final double _gravity = 9.81; //in m/s^2 + + final double _waterHeatCapacity = 4.181; //J / (g * C°) + final double _ironHeatCapacity = 0.439; //in J / (g * C°) + + double _waterTemparatureStart = 20; //in °C + final int _temparatureToBoilWater = 100; //in °C + + double _ironTemparatureStart = 20; //in °C + final int _temparatureToMakeIronGlow = 700; //in °C double get deltaVelocity => _endVelocity - _startVelocity; double get deltaHeight => _endHeight - _startHeight; - double get potentialEnergy => _weight * _gravity * deltaHeight; + double get potentialEnergy => _weight * _gravity * deltaHeight; //in Joule = m^2 kg/s^2 double get kineticEnergy => _weight * pow(deltaVelocity, 2) / 2; + double get amountOfBoilingWaterInL => (((potentialEnergy + kineticEnergy) / _waterHeatCapacity) / (_temparatureToBoilWater - _waterTemparatureStart)) / 1000; //in litre + double get amountOfGlowingIronInKg => (((potentialEnergy + kineticEnergy) / _ironHeatCapacity) / (_temparatureToMakeIronGlow - _ironTemparatureStart)) / 1000; + void setStartVelocity(double v){ _startVelocity = v; notifyListeners(); diff --git a/lib/models/MyInputModel.dart b/lib/models/MyInputModel.dart deleted file mode 100644 index 02abf54..0000000 --- a/lib/models/MyInputModel.dart +++ /dev/null @@ -1,12 +0,0 @@ -import 'package:flutter/cupertino.dart'; - -class MyInputModel extends ChangeNotifier { - String _text = "Hallo Welt"; - - String get currentText => _text; - - void changeText(String newText) { - _text = newText; - notifyListeners(); - } -} \ No newline at end of file diff --git a/lib/widgets/EnergyDisplay.dart b/lib/widgets/EnergyDisplay.dart new file mode 100644 index 0000000..2d0d7a2 --- /dev/null +++ b/lib/widgets/EnergyDisplay.dart @@ -0,0 +1,49 @@ +import 'package:energy_bilance/models/EnergyBilanceModel.dart'; +import 'package:energy_bilance/widgets/TabDisplayWidget.dart'; +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; + +class EnergyDisplay extends StatelessWidget { + const EnergyDisplay({super.key}); + + @override + Widget build(BuildContext context) { + return DefaultTabController( + length: 3, + child: Scaffold( + appBar: AppBar( + title: const Text('Energie umrechnen in:'), + bottom: const TabBar( + tabs: [ + Tab(text: 'Wasser', icon: Icon(Icons.anchor_sharp),), + Tab(text: 'Eisen', icon: Icon(Icons.iron)), + Tab(text: 'Energie', icon: Icon(Icons.battery_charging_full),) + ] + ), + ), + body: TabBarView( + children: [ + Consumer( + builder: (context, model, child){ + return TabDisplayWidget('Wasser, das von 20 °C zum Kochen gebracht werden kann: ${double.parse((model.amountOfBoilingWaterInL).toStringAsFixed(2))} Liter'); + } + ), + Consumer( + builder: (context, model, child){ + return TabDisplayWidget('Eisen, das von 20 °C zum Glühen gebracht werden kann: ${double.parse((model.amountOfGlowingIronInKg).toStringAsFixed(2))} Kilo'); + } + ), + Consumer( + builder: (context, model, child){ + return TabDisplayWidget('Generierte potentielle Energie: ${model.potentialEnergy}\n' + 'Generierte kinetische Energie: ${model.kineticEnergy}'); + } + ), + ] + ), + ) + + ); + } + +} \ No newline at end of file diff --git a/lib/widgets/MyAppBar.dart b/lib/widgets/MyAppBar.dart deleted file mode 100644 index d2042b9..0000000 --- a/lib/widgets/MyAppBar.dart +++ /dev/null @@ -1,25 +0,0 @@ -import 'package:flutter/material.dart'; - -class MyAppBar extends StatelessWidget { - const MyAppBar({required this.title, super.key}); - - final Widget title; - - @override - Widget build(BuildContext context) { - return Container( - height: 56.0, - padding: const EdgeInsets.symmetric(horizontal: 8.0), - decoration: BoxDecoration(color: Colors.blue[500]), - child: Row( - children: [ - const IconButton(onPressed: null, icon: Icon(Icons.menu), tooltip: 'Navigation Menu'), - Expanded(child: title), - const IconButton(onPressed: null, icon: Icon(Icons.search), tooltip: 'Search') - ], - ), - ); - } - - -} \ No newline at end of file diff --git a/lib/widgets/MyCustomForm.dart b/lib/widgets/MyCustomForm.dart deleted file mode 100644 index 7380846..0000000 --- a/lib/widgets/MyCustomForm.dart +++ /dev/null @@ -1,43 +0,0 @@ -import 'package:energy_bilance/models/MyInputModel.dart'; -import 'package:flutter/material.dart'; -import 'package:provider/provider.dart'; - -class MyCustomForm extends StatefulWidget { - const MyCustomForm({super.key}); - - @override - State createState() { - return MyCustomFormState(); - } -} - -class MyCustomFormState extends State { - - final _formKey = GlobalKey(); - - @override - Widget build(BuildContext context) { - return Form( - key: _formKey, - child: Column( - children: [ - TextFormField( - decoration: const InputDecoration( - labelText: 'Gib was ein', - hintText: 'keine Zahlen bidde' - ), - validator: (String? value) { - return (value != null && value.contains('@')) ? 'Do not use the @ char.' : null; - }, - onChanged: (value) { - Provider.of(context, listen: false).changeText(value); - } - - ) - ], - ), - ); - } - - -} \ No newline at end of file diff --git a/lib/widgets/MyInputWidget.dart b/lib/widgets/MyInputWidget.dart index f677121..85407e8 100644 --- a/lib/widgets/MyInputWidget.dart +++ b/lib/widgets/MyInputWidget.dart @@ -15,7 +15,7 @@ class MyInputWidget extends StatelessWidget { children: [ Row( mainAxisSize: MainAxisSize.min, - mainAxisAlignment: MainAxisAlignment.spaceBetween, + mainAxisAlignment: MainAxisAlignment.start, children: [ Expanded( diff --git a/lib/widgets/MyScaffold.dart b/lib/widgets/MyScaffold.dart deleted file mode 100644 index 90b5ebf..0000000 --- a/lib/widgets/MyScaffold.dart +++ /dev/null @@ -1,24 +0,0 @@ -import 'package:energy_bilance/widgets/MyAppBar.dart'; -import 'package:flutter/material.dart'; - -class MyScaffold extends StatelessWidget { - const MyScaffold({super.key}); - - @override - Widget build(BuildContext context) { - return Material( - child: Column( - children: [ - MyAppBar(title: Text('MyAppBar', style: Theme.of(context).primaryTextTheme.headline6)), - const Expanded(child: - Center( - child: Text("Hello World!"), - ) - ) - ], - ), - ); - } - - -} \ No newline at end of file diff --git a/lib/widgets/MyTest.dart b/lib/widgets/MyTest.dart deleted file mode 100644 index f48cc4e..0000000 --- a/lib/widgets/MyTest.dart +++ /dev/null @@ -1,30 +0,0 @@ -import 'package:energy_bilance/models/MyInputModel.dart'; -import 'package:energy_bilance/widgets/MyCustomForm.dart'; -import 'package:flutter/material.dart'; -import 'package:provider/provider.dart'; - -class MyTest extends StatelessWidget { - const MyTest({super.key}); - - @override - Widget build(BuildContext context) { - return Material( - child: - ChangeNotifierProvider(create: (context) => MyInputModel(), - child: Column( - children: [ - const MyCustomForm(), - Consumer( - builder: (context, input, child){ - return Text('Das ist die Eingabe: ${input.currentText}'); - } - ), - ], - ), - ) - - ); - } - - -} \ No newline at end of file diff --git a/lib/widgets/TabDisplayWidget.dart b/lib/widgets/TabDisplayWidget.dart new file mode 100644 index 0000000..11fed6d --- /dev/null +++ b/lib/widgets/TabDisplayWidget.dart @@ -0,0 +1,19 @@ +import 'package:flutter/cupertino.dart'; + +class TabDisplayWidget extends StatelessWidget { + + String _displayText = ""; + + TabDisplayWidget(String displayText, {super.key}) { + _displayText = displayText; + } + + + @override + Widget build(BuildContext context) { + return Center( + child: Text(_displayText) + ); + } + +} \ No newline at end of file diff --git a/test/EnergyBilanceModelTest.dart b/test/EnergyBilanceModelTest.dart new file mode 100644 index 0000000..813dfd5 --- /dev/null +++ b/test/EnergyBilanceModelTest.dart @@ -0,0 +1,21 @@ +import 'dart:async'; + +import 'package:energy_bilance/models/EnergyBilanceModel.dart'; + +void main() async { + //Location _locationModel;StreamController _controller;setUp(() { + _controller = StreamController(); + _energyModel = EnergyBilanceModel(); +});group('[Location Model]', () { +//test('[Model] Check individual values', () async { +_locationModel = Location( +city: 'London', +countryName: 'England', +country: 'England', +currency: 'GBP', +ip: '', +inEu: true, +languages: 'EN', +);// BEGIN TESTS.... +expect(_locationModel.city, 'London');expect(_locationModel.countryName.runtimeType, equals(String));expect(_locationModel.country, isNotNull);expect(_locationModel.ip, isEmpty);expect(_locationModel.inEu, isTrue);expect(_locationModel.languages, contains('EN'));expect(_locationModel.currency, startsWith('G'));expect(_locationModel.country, matches('England')); +});} \ No newline at end of file