cpd_2022_energy/lib/main.dart

148 lines
5.7 KiB
Dart

import 'package:energy/model/EnergyModelProvider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(const EnergyProviderApp());
}
class EnergyProviderApp extends StatelessWidget {
const EnergyProviderApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Energy App',
theme: ThemeData(
primarySwatch: Colors.yellow,
),
home: EnergyHomePage(),
);
}
}
class EnergyHomePage extends StatelessWidget {
// Constructor of EnergyHomePage
EnergyHomePage({Key? key}) : super(key: key);
final altitudeChangeValue = TextEditingController();
final velocityChangeValue = TextEditingController();
final weightValue = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter Energy App')),
body: ChangeNotifierProvider(
key: const Key("ChangeNotifierProvider"),
create: (_) => EnergyModelProvider(),
child: Center(
child: Column(
children: [
// Textfield to enter altitude change value
TextField(
key: const Key("AltitudeChange"),
controller: altitudeChangeValue,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Enter altitude change in meter:',
suffixText: 'm',
suffixIcon: IconButton(
onPressed: weightValue.clear,
icon: Icon(Icons.clear),
)
),
),
// Textfield to enter velocity change value
TextField(
key: const Key("VelocityChange"),
controller: velocityChangeValue,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Enter velocity change in m/s:',
suffixText: 'm/s',
suffixIcon: IconButton(
onPressed: weightValue.clear,
icon: Icon(Icons.clear),
)
),
),
// Textfield to enter weight value
TextField(
key: const Key("WeightValue"),
controller: weightValue,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Enter weight of object in kg:',
suffixText: 'kg',
suffixIcon: IconButton(
onPressed: weightValue.clear,
icon: Icon(Icons.clear),
)
),
),
Consumer<EnergyModelProvider>(
builder: (context, model, child) => Column(
children: [
TextButton(
key: const Key("ButtonEnergyInJoule"),
style: TextButton.styleFrom(
foregroundColor: Colors.green,
),
onPressed: () => model.calculateEnergyInJoule(weightValue.text, velocityChangeValue.text),
child: Text('Press to calculate energy in joule')
),
TextButton(
key: const Key("ButtonPotentialEnergy"),
style: TextButton.styleFrom(
foregroundColor: Colors.red,
),
onPressed: () => model.calculateEnergyPotential(weightValue.text, altitudeChangeValue.text),
child: Text('Press to calculate Potential energy')
),
TextButton(
key: const Key("ButtonKineticEnergy"),
style: TextButton.styleFrom(
foregroundColor: Colors.blueAccent,
),
onPressed: () => model.calculateEnergyKinetic(weightValue.text, velocityChangeValue.text),
child: Text('Press to calculate Kinetic energy')
),
TextButton(
key: const Key("ButtonLiterWater"),
style: TextButton.styleFrom(
foregroundColor: Colors.grey,
),
onPressed: () => model.calculateLiterToHeatWater(),
child: Text('Press to calculate liter to heat water')
),
TextButton(
key: const Key("ButtonKilogrammIron"),
style: TextButton.styleFrom(
foregroundColor: Colors.purple,
),
onPressed: () => model.calculateKgToHeatIron(),
child: Text('Press to calculate kg to heat iron')
),
Text(
'Energy in Joule: ${model.energyInJoule} J'),
Text(
'Potential Energy in Joule: ${model.potentialEnergy} J'),
Text(
'Kinetic Energy in Joule: ${model.kineticEnergy} J'),
Text(
'Amount of water you could heat water: ${model.literWaterToHeat} L'),
Text(
'Amount of iron you could heat: ${model.kilogrammIronToHeat} kg'),
],
),
),
],
),
),
),
);
throw UnimplementedError();
}
}