import 'package:flutter/material.dart'; import 'package:energy/widgets/calculate.dart'; void main() { runApp(const EnergyApp()); } class EnergyApp extends StatefulWidget { const EnergyApp({Key? key}) : super(key: key); @override State createState() => _EnergyAppState(); } class _EnergyAppState extends State { TextEditingController v1Controller = TextEditingController(); TextEditingController v2Controller = TextEditingController(); TextEditingController h1Controller = TextEditingController(); TextEditingController h2Controller = TextEditingController(); TextEditingController mController = TextEditingController(); num joule = 0, lWater = 0, kgIron = 0; List headerLabels = ["Energie in Joule", "Liter Wasser, die zum Kochen gebracht werden können", "Kg Eisen, die zum Glühen gebracht werden können"]; String header = "Energie in Joule"; int _currentIndex = 0; num _currentValue = 0; static const TextStyle myTextStyle = TextStyle(fontSize: 20, fontWeight: FontWeight.bold); static const TextStyle myHeaderStyle = TextStyle(fontSize: 35, fontWeight: FontWeight.bold, color: Colors.blue); void changeCalculateValues() { setState(() { Calculate calc = Calculate( num.parse(v1Controller.text), num.parse(v2Controller.text), num.parse(h1Controller.text), num.parse(h2Controller.text), num.parse(mController.text)); joule = calc.calculateJoule(); lWater = calc.calculateLWater(); kgIron = calc.calculateKgIron(); setCurrentValue(); }); } void changeEnergyForm(){ setState(() { header = headerLabels[_currentIndex]; setCurrentValue(); }); } void setCurrentValue(){ if(_currentIndex == 0 ){ _currentValue = joule; } else if(_currentIndex == 1 ){ _currentValue = lWater; } else if(_currentIndex == 2 ){ _currentValue = kgIron; } } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( backgroundColor: Colors.blue, title: const Text("Energiebielanz bei Geschwindigkeits- und Höhenänderungen"), ), body: Center( child: Container( width: 800, child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Padding( padding: const EdgeInsets.only(bottom: 100.0), child: Text(header, style: myHeaderStyle) ), Padding( padding: const EdgeInsets.all(8.0), child: TextField( keyboardType: TextInputType.number, controller: v1Controller, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Anfangsgeschwindigkeit", hintText: "Anfangsgeschwindigkeit"), )), Padding( padding: const EdgeInsets.all(8.0), child: TextField( keyboardType: TextInputType.number, controller: v2Controller, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Endgeschwindigkeit", hintText: "Endgeschwindigkeit"), )), Padding( padding: const EdgeInsets.all(8.0), child: TextField( keyboardType: TextInputType.number, controller: h1Controller, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Anfangshöhe", hintText: "Anfangshöhe"), )), Padding( padding: const EdgeInsets.all(8.0), child: TextField( keyboardType: TextInputType.number, controller: h2Controller, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Endhöhe", hintText: "Endhöhe"), )), Padding( padding: const EdgeInsets.all(8.0), child: TextField( keyboardType: TextInputType.number, controller: mController, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: "Gewicht in Kg", hintText: "Gewicht in Kg"), )), TextButton( style: TextButton.styleFrom( backgroundColor: Colors.blueAccent, foregroundColor: Colors.white, padding: const EdgeInsets.all(16.0), textStyle: const TextStyle(fontSize: 20) ), onPressed: changeCalculateValues, child: const Text('Berechnen')), Padding( padding: const EdgeInsets.only(top: 50.0), child: Text("$_currentValue", style: myTextStyle) ), ], ), ), ), bottomNavigationBar: BottomNavigationBar( currentIndex: _currentIndex, items: const [ BottomNavigationBarItem( icon: Icon(Icons.energy_savings_leaf), label: 'Energie', ), BottomNavigationBarItem( icon: Icon(Icons.water_drop), label: 'Wasser', ), BottomNavigationBarItem( icon: Icon(Icons.iron), label: 'Eisen', ), ], onTap: (index){ _currentIndex = index; changeEnergyForm(); }, ), )); } }