import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Несие калькуляторы',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CalculatorPage(),
);
}
}
class CalculatorPage extends StatefulWidget {
@override
_CalculatorPageState createState() => _CalculatorPageState();
}
class _CalculatorPageState extends State {
double loanAmount = 0;
double interestRate = 0;
int loanTerm = 0;
double monthlyPayment = 0;
void calculateMonthlyPayment() async {
if (loanAmount > 0 && interestRate > 0 && loanTerm > 0) {
double monthlyInterestRate = interestRate / 100 / 12;
double totalPayments = loanTerm * 12;
double discountFactor = await Future(() {
return (pow(1 + monthlyInterestRate, totalPayments) - 1) /
(monthlyInterestRate * pow(1 + monthlyInterestRate, totalPayments));
});
setState(() {
monthlyPayment = loanAmount / discountFactor;
});
} else {
setState(() {
monthlyPayment = 0;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Несие калькуляторы'),
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Несие сомасы',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 8),
TextFormField(
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
loanAmount = double.tryParse(value) ?? 0;
});
},
),
SizedBox(height: 16),
const Text(
'Пайыздық мөлшерлеме (%)',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 8),
TextFormField(
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
interestRate = double.tryParse(value) ?? 0;
});
},
),
SizedBox(height: 16),
const Text(
'Несие мерзімі (жылдар)',
style: TextStyle(fontSize: 20),
),
const SizedBox(height: 8),
TextFormField(
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
loanTerm = int.tryParse(value) ?? 0;
});
},
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
calculateMonthlyPayment();
},
child: Text('Есептеу'),
),
const SizedBox(height: 16),
const Text(
'Ай сайынғы төлем:',
style: TextStyle(fontSize: 24),
),
const SizedBox(height: 8),
Text(
'$monthlyPayment \теңге',
style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
),
],
),
),
),
);
}
}