Simple Interest Calculator Using Dart
Simple Interest Calculator Using Dart
1
2
Main.dart
import 'package:flutter/material.dart';
@override
void dispose() {
principalController.dispose();
rateController.dispose();
timeController.dispose();
super.dispose();
}
void calculateInterest() {
double principal =
double.tryParse(principalController.text) ?? 0.0;
double rate = double.tryParse(rateController.text) ?? 0.0;
double time = double.tryParse(timeController.text) ?? 0.0;
double interest = (principal * rate * time) / 100.0;
setState(() {
result = 'Simple Interest:
\₵${interest.toStringAsFixed(2)}';
3
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Interest Calculator'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: principalController,
keyboardType:
TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: 'Principal Amount',
border: OutlineInputBorder(),
),
),
SizedBox(height: 16.0),
TextField(
controller: rateController,
keyboardType:
TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: 'Annual Interest Rate',
border: OutlineInputBorder(),
),
),
SizedBox(height: 16.0),
TextField(
controller: timeController,
keyboardType:
TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: 'Time (in years)',
border: OutlineInputBorder(),
),
),
SizedBox(height: 32.0),
ElevatedButton(
onPressed: calculateInterest,
child: Text('Calculate'),
),
SizedBox(height: 16.0),
Text(
result,
4
style: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
);
}
}
Code: Explained
This code is a Flutter app that calculates simple interest based on the user input of principal
amount, annual interest rate, and time in years.
First, the InterestCalculatorApp class is defined which is a stateless widget. The build method
of this widget returns a MaterialApp widget which contains the theme data and the home
page, which is an instance of the InterestCalculatorPage class.
The InterestCalculatorPage class is a stateful widget that has three TextEditingController
objects to handle user input for principal amount, annual interest rate, and time. There is also
a result string variable which stores the calculated simple interest.
The dispose method is overridden to dispose of the text editing controllers when the widget
is removed from the widget tree.
The calculateInterest method is called when the user presses the "Calculate" button. This
method retrieves the user input from the text editing controllers and converts them to double
data type. Then it calculates the simple interest using the formula (principal * rate * time) /
100.0 and updates the result string variable.
Finally, the build method returns a Scaffold widget that contains the app bar with the title
"Interest Calculator" and a Column widget that contains three TextField widgets to handle user
input, an ElevatedButton widget that calls the calculateInterest method when pressed, and a
Text widget that displays the calculated simple interest.
Overall, this code demonstrates how to use Flutter widgets to create an interactive user
interface and perform calculations based on user input.