Skip to content

Commit

Permalink
实现月供计算年利率
Browse files Browse the repository at this point in the history
  • Loading branch information
巫圣泰 committed Dec 31, 2019
1 parent ea108f4 commit eec4614
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 75 deletions.
4 changes: 4 additions & 0 deletions lib/utils/Utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ class Utils{
FocusScope.of(context).requestFocus(FocusNode());
}

static int currentTimeMillis() {
return new DateTime.now().millisecondsSinceEpoch;
}

}
49 changes: 46 additions & 3 deletions lib/utils/calc.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@



import 'package:loan_calc/member/LoanInfo.dart';


import 'dart:math';
import 'package:loan_calc/utils/Utils.dart';

LoandInfo calcRate(double money,double rateMoney,int rateType){

Expand Down Expand Up @@ -57,14 +57,47 @@ LoandInfo calcRate(double money,double rateMoney,int rateType){

LoandInfo info=calcRate(totalMoney, avaLocalMoney, rateType);

double rateYear=0.0;


if(rateType==RATE.MONTH){

var rate=await calcRateImpl(totalMoney, avaMoney, time, info.yearRate/12);
rateYear=rate*12;


}else{
var rate=await calcRateImpl(totalMoney, avaMoney, time, info.yearRate/365);
rateYear=rate*365;
}

return rateYear;

return info;
}

//in thread
calcRateImpl(double totalMoney,double avaMoney,int number,double startRate) async{



int t1=Utils.currentTimeMillis();
// var rate=(startRate-3)<0?0:(startRate-3);
double rate=startRate;
double temp=0.0;
double stage=0.0001;

do{
temp=caclAverageMoney(totalMoney, rate, number);
if(temp>avaMoney){
break;
}
rate+=stage;
}while(((temp-avaMoney).abs()>0.5));

int t2=Utils.currentTimeMillis();

print("eat time:"+(t2-t1).toString());
return rate;

}

Expand Down Expand Up @@ -114,4 +147,14 @@ LoandInfo calc(double money,double rate,int time,int rateType,int timeType){
return info;


}



double caclAverageMoney(double totalMoney,double rate,int number){
double result= 0.0;

result = (totalMoney * (rate * pow((1 + rate), number.toDouble()))) / (pow((1 + rate), number.toDouble()) - 1);

return result;
}
31 changes: 20 additions & 11 deletions lib/view/MoneyCalcPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ class _MoneyCalcPageState extends State<MoneyCalcPage>

double rate = double.parse(rateController.text);

int rateType = RATE.YEAR;
if (money == 0) {
showTaost("金额不能为0");
return null;
}

if (rate == 0) {
showTaost("利息不能为0");
return null;
}

int rateType = RATE.YEAR;

if ("月息" == spnnerRateCtrl.value) {
rateType = RATE.MONTH;
Expand All @@ -82,13 +91,16 @@ class _MoneyCalcPageState extends State<MoneyCalcPage>
child:
Column(mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[
SizedBox(
height: 36,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"通过利息计算年化",
style: TextStyle(color: Colors.black45),
))),
height: 36,
child: Container(
color: Colors.black12,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"通过利息计算年化(主要还是用于理财)",
style: TextStyle(color: Colors.black45),
))),
),
SizedBox(
height: 48,
child: Row(
Expand Down Expand Up @@ -121,8 +133,6 @@ class _MoneyCalcPageState extends State<MoneyCalcPage>
controller: rateController,
keyboardType: TextInputType.number,
//数字输入

// inputFormatters:[WhitelistingTextInputFormatter.digitsOnly] ,
decoration: InputDecoration(hintText: "需要的利息(元)"),
),
),
Expand Down Expand Up @@ -153,6 +163,5 @@ class _MoneyCalcPageState extends State<MoneyCalcPage>
child: SingleChildScrollView(child: Text("$displayDetail")))
]),
);

}
}
94 changes: 71 additions & 23 deletions lib/view/MoneyCalcRealPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class _MoneyCalcRealPageState extends State<MoneyCalcRealPage>
with AutomaticKeepAliveClientMixin {
final TextEditingController moneyController = new TextEditingController();
final TextEditingController rateController = new TextEditingController();
final TextEditingController numberController = new TextEditingController();

SpnnerControl spnnerRateCtrl = new SpnnerControl();

Expand All @@ -31,50 +32,71 @@ class _MoneyCalcRealPageState extends State<MoneyCalcRealPage>
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
String rateTypeStr="月供";
String rateTypeStr = "月供";

@override
void initState() {
super.initState();

spnnerRateCtrl.addListener(() {

setState(() {
rateTypeStr=rateController.value.toString();
rateTypeStr = spnnerRateCtrl.textValue;
});
});
}

void checkAndCalc(TapUpDetails details) {
void checkAndCalc(TapUpDetails details) async {
Utils.closeKeybord(context);
if (moneyController.text.isEmpty) {
showTaost("金额不能为空");
return null;
}

if (rateController.text.isEmpty) {
showTaost("利息不能为空");
showTaost("月供不能为空");
return null;
}
if (numberController.text.isEmpty) {
showTaost("贷款期数不能为空");
return null;
}

double money = double.parse(moneyController.text);

double rate = double.parse(rateController.text);
double avaMoney = double.parse(rateController.text);

int rateType = RATE.YEAR;
int number = double.parse(numberController.text).toInt();

if (money == 0) {
showTaost("金额不能为0");
return null;
}

if ("月息" == spnnerRateCtrl.value) {
rateType = RATE.MONTH;
} else if ("日息" == spnnerRateCtrl.value) {
if (avaMoney == 0) {
showTaost("月供不能为0");
return null;
}
if (number == 0) {
showTaost("贷款期数不能为0");
return null;
}

int rateType = RATE.YEAR;

print(spnnerRateCtrl.value);
if ("日供" == spnnerRateCtrl.value) {
rateType = RATE.DAY;
} else {
rateType = RATE.YEAR;
rateType = RATE.MONTH;
}
LoandInfo info = calcRate(money, rate, rateType);

double rateYear = await calcRateHight(money, avaMoney, number, rateType);

rateYear *= 100;

String temp = Utils.formatNumber(rateYear);
setState(() {
rateMoney = info.interestTotal;
displayDetail = info.toString();
this.displayDetail = "年利率 ≈ $temp %";
});
}

Expand All @@ -86,15 +108,19 @@ class _MoneyCalcRealPageState extends State<MoneyCalcRealPage>
return Container(
margin: EdgeInsets.only(left: 16, right: 16),
child:
Column(mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[
Column(mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[
SizedBox(
height: 36,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"真实利息计算年化",
style: TextStyle(color: Colors.black45),
))),
height: 36,
child: Container(
color: Colors.black12,
child: Align(
alignment: Alignment.centerLeft,

child: Text(
"真实利息计算年化(用于了解贷款的真实利率)",
style: TextStyle(color: Colors.black45),
))),
),
SizedBox(
height: 48,
child: Row(
Expand Down Expand Up @@ -137,6 +163,29 @@ class _MoneyCalcRealPageState extends State<MoneyCalcRealPage>
],
)),
SizedBox(height: 16),
SizedBox(
height: 48,
child: Row(
children: <Widget>[
Expanded(
child: TextField(
obscureText: false,
//是否是密码
controller: numberController,
keyboardType: TextInputType.number,
//数字输入

inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly
],
decoration: InputDecoration(hintText: "贷款期数"),
),
),
SizedBox(width: 10),
SizedBox(width: 80, child: Text("$rateTypeStr次数")),
],
)),
SizedBox(height: 16),
GestureDetector(
child: Container(
color: Colors.deepOrange,
Expand All @@ -159,6 +208,5 @@ class _MoneyCalcRealPageState extends State<MoneyCalcRealPage>
child: SingleChildScrollView(child: Text("$displayDetail")))
]),
);

}
}
38 changes: 24 additions & 14 deletions lib/view/NormalCalcPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ class _NormalCalcPageState extends State<NormalCalcPage> {

double rate = double.parse(rateController.text);

if (money == 0) {
showTaost("金额不能为0");
return null;
}

if (rate == 0) {
showTaost("利息不能为0");
return null;
}

if (time == 0) {
showTaost("时间不能为0");
return null;
}

int rateType = RATE.YEAR;

int timeType = TIME.YEAR;
Expand Down Expand Up @@ -92,26 +107,21 @@ class _NormalCalcPageState extends State<NormalCalcPage> {
Widget build(BuildContext context) {
// TODO: implement build






return Scaffold(
body:

Container(
body: Container(
margin: EdgeInsets.only(left: 16, right: 16),
child: Column(mainAxisAlignment: MainAxisAlignment.start, children: <
Widget>[
SizedBox(
height: 36,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"通过年化计算利息",
style: TextStyle(color: Colors.black45),
))),
child: Container(
color: Colors.black12,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"通过年化计算利息",
style: TextStyle(color: Colors.black45),
)))),
SizedBox(
height: 48,
child: Row(
Expand Down
Loading

0 comments on commit eec4614

Please sign in to comment.