Simple Intrest Calculator.
-----------------------------------------------------------------------------------
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Simple Interest Calculator App',
// color: Colors.lightBlue,
home: SIForm(),
theme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.indigo,
accentColor: Colors.indigoAccent),
));
}
class SIForm extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _SIFormState();
}
}
class _SIFormState extends State<SIForm> {
var _formKey = GlobalKey<FormState>();
var _currencies = ['Rupees', 'Dollars', 'Pounds'];
final _minimumPadding = 5.0;
// ignore: unnecessary_type_check
var _currentItemSelected = 'Rupees';
@override
void initState() {
super.initState();
_currentItemSelected = _currencies[0];
}
TextEditingController principalController = TextEditingController();
TextEditingController roiController = TextEditingController();
TextEditingController termController = TextEditingController();
var displayResult = '';
@override
Widget build(BuildContext context) {
TextStyle? textStyle = Theme.of(context).textTheme.subtitle1;
return Scaffold(
//resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('Simple Interest Calculator'),
),
body: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.all(_minimumPadding * 2),
child: ListView(
children: <Widget>[
getImageAsset(),
Padding(
padding: EdgeInsets.only(
top: _minimumPadding, bottom: _minimumPadding),
child: TextFormField(
keyboardType: TextInputType.number,
style: textStyle,
controller: principalController,
validator: (var value) {
if (value!.isEmpty) {
return 'plese enter principal amount';
}
},
decoration: InputDecoration(
labelText: 'Principal',
hintText: 'Enter Principal e.g. 12000',
labelStyle: textStyle,
errorStyle: TextStyle(
color: Colors.yellowAccent, fontSize: 15.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
)),
Padding(
padding: EdgeInsets.only(
top: _minimumPadding, bottom: _minimumPadding),
child: TextFormField(
keyboardType: TextInputType.number,
style: textStyle,
controller: roiController,
validator: (var value) {
if (value!.isEmpty) {
return 'please enter rate of interest';
}
},
decoration: InputDecoration(
labelText: 'Rate Of Interest',
hintText: 'In Percent',
labelStyle: textStyle,
errorStyle: TextStyle(
color: Colors.yellowAccent, fontSize: 15.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
)),
Padding(
padding: EdgeInsets.only(
top: _minimumPadding, bottom: _minimumPadding),
child: Row(
children: <Widget>[
Expanded(
child: TextFormField(
keyboardType: TextInputType.number,
style: textStyle,
controller: termController,
validator: (var value) {
if (value!.isEmpty) {
return 'please enter time';
}
},
decoration: InputDecoration(
labelText: 'Term',
errorStyle: TextStyle(
color: Colors.yellowAccent, fontSize: 15.0),
hintText: 'Term in years',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
)),
Container(
width: _minimumPadding * 5,
),
Expanded(
child: DropdownButton<String>(
items: _currencies.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
value: _currentItemSelected,
onChanged: (var newValueSelected) {
_onDropdownItemSelected(newValueSelected!);
},
))
],
)),
Padding(
padding: EdgeInsets.only(
top: _minimumPadding, bottom: _minimumPadding),
child: Row(
children: <Widget>[
Expanded(
// ignore: deprecated_member_use
child: RaisedButton(
color: Theme.of(context).accentColor,
textColor: Theme.of(context).primaryColorDark,
child: Text(
'Calculate',
textScaleFactor: 1.5,
),
onPressed: () {
setState(() {
if (_formKey.currentState!.validate()) {
this.displayResult = _calculateTotalReturns();
}
});
},
)),
Expanded(
// ignore: deprecated_member_use
child: RaisedButton(
color: Theme.of(context).primaryColorDark,
textColor: Theme.of(context).primaryColorLight,
child: Text(
'Reset',
textScaleFactor: 1.5,
),
onPressed: () {
setState(() {
_reset();
});
},
))
],
)),
Padding(
padding: EdgeInsets.all(_minimumPadding * 2),
child: Text(
this.displayResult,
style: textStyle,
),
)
],
)),
),
);
}
Widget getImageAsset() {
AssetImage assetImage = AssetImage('images/Money-Icon.png');
Image image = Image(
image: assetImage,
width: 125.0,
height: 125.0,
);
return Container(
child: image,
margin: EdgeInsets.all(_minimumPadding * 10),
);
}
void _onDropdownItemSelected(String newValueSelected) {
setState(() {
this._currentItemSelected = newValueSelected;
});
}
String _calculateTotalReturns() {
double principal = double.parse(principalController.text);
double roi = double.parse(roiController.text);
double term = double.parse(termController.text);
double totalAmountPayable = principal + (principal * roi * term) / 100;
String result =
'After $term years , your investment will be worth $totalAmountPayable $_currentItemSelected';
return result;
}
void _reset() {
principalController.text = '';
roiController.text = '';
termController.text = '';
displayResult = '';
_currentItemSelected = _currencies[0];
}
}
0 Comments
Post a Comment