Skip to content

Commit

Permalink
Added Crypto Currency Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Garvit-k committed Aug 26, 2020
1 parent 8d489bb commit 3740cdc
Show file tree
Hide file tree
Showing 5 changed files with 389 additions and 121 deletions.
150 changes: 150 additions & 0 deletions lib/CryptoConveter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import 'package:currency_convertor/CryptoUtil.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'ConversionUtil.dart';
import 'CurrencyList.dart';

class CryptoConveter extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return CryptoConveterState();
}

}
class CryptoConveterState extends State<CryptoConveter> {
String baseCurrency="BTC";
String targetCurrency="USD";
String convertedResult="Convert to";
TextEditingController baseController = new TextEditingController();
@override
Widget build(BuildContext context) {
// DONE: implement build
return WillPopScope(
onWillPop: _onBackPressed,
child: Scaffold (
appBar: AppBar(title: Text("Currency Converter", style: TextStyle(color: Colors.white),),),
backgroundColor: Colors.black,
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Crypto Currency Converter",style: TextStyle(color: Colors.blue,fontSize: 30),),
SizedBox(height: 150,),
Container(
child:
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child:
TextFormField(
keyboardType: TextInputType.number,
controller: baseController,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white70,
border: OutlineInputBorder(),
labelText: "Number",
labelStyle: TextStyle(color: Colors.blue),
hintText: "1.00",
),
),
),
baseCurrencyList(),
],
),
padding: EdgeInsets.all(20),
),
Container(
child:
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,

children: <Widget>[
SizedBox(width: 80,),
Expanded(
child:
Text(convertedResult,style: TextStyle(color: Colors.red,fontSize: 20),),
),
targetCurrencyList(),
],
),
padding: EdgeInsets.all(20),
),
RaisedButton(
child: Text("Convert"),
onPressed: () {
convertValues();
},
),
],
)
)
);
}
Widget baseCurrencyList() {
return DropdownButton<String>(
value: baseCurrency,
dropdownColor: Colors.black,
items: CurrencyList.currenciesCrypto.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value,style: TextStyle(color: Colors.white),),
);
}).toList(),
onChanged: (String value) {
setState(() {
baseCurrency = value;
});
},
);
}
Widget targetCurrencyList() {
return DropdownButton<String>(
value: targetCurrency,
dropdownColor: Colors.black,
items: CurrencyList.currenciesCrypto.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value,style: TextStyle(color: Colors.white),),
);
}).toList(),
onChanged: (String value) {
setState(() {
targetCurrency = value;
});
},
);
}

convertValues() async{
convertedResult = await CryptoUtil().converter(baseController.text, baseCurrency, targetCurrency);
setState(() {
});
}

Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Are you sure?',style: TextStyle(fontSize: 25),),
content: new Text('Do you want Logout ?',style: TextStyle(fontSize: 20)),
actions: <Widget>[
new GestureDetector(
onTap: () => Navigator.of(context).pop(false),
child: Text("NO",style: TextStyle(fontSize: 20,color: Colors.white,backgroundColor: Colors.blue)),
),
SizedBox(height: 20),
new GestureDetector(
onTap: () => Navigator.of(context).pop(true),
child: Text("YES",style: TextStyle(fontSize: 20,color: Colors.white,backgroundColor: Colors.red)),
),
],
),
) ??
false;
}

}
50 changes: 50 additions & 0 deletions lib/CryptoUtil.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';

class CryptoUtil {

String APIENDPOINT = "https://min-api.cryptocompare.com/data/price";
String APIKEY = "eae6f5433e8ffbf50dc0a623f53e053e99aba9509d0264ccf1bfe92ae2890214";

Future<dynamic> fetchRate(base,target) async{
//Rates r = new Rates(base: "USD",rates: {"INR":21.123},date: "2");
//return r;
http.Response response = await http.get(APIENDPOINT+"?fsym=$base&tsyms=$target&api_key=$APIKEY");
//print('res '+response.body);
//print("responsecode "+response.statusCode.toString());
if(response.statusCode.toString() == "200") {
//print("yes");
//print("rates "+RatesData.fromJson(json.decode(response.body),target).toString());
//print("yes 1");
return Rate.fromJson(json.decode(response.body),target);
}
else
{
throw Exception("Failed to get rates");
}
}

Future<String> converter(String baseIn,base,target) async{
try {
//print("call");
Rate rates = await fetchRate(base, target);
return (double.parse(baseIn) * rates.rate).toString();
}
catch(e) {
return e.toString();
}

}

}

class Rate {
double rate;

Rate({this.rate});

Rate.fromJson(Map<String, dynamic> json,String target) {
rate = json[target];
}
}
29 changes: 28 additions & 1 deletion lib/CurrencyList.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,31 @@ class CurrencyList {
"HUF",
"SEK"
];
}

static const currenciesCrypto = <String>[
"BTC",
"ETH",
"USD",
"INR",
"WRX",
"XMR",
"XLM",
"BNB",
"MATIC",
"LINK",
"EOS",
"LTC",
"TRX",
"USDT",
"BAT",
"DOGE",
"DAI",
"JST",
"OMG",
"SXP",
"BAND",
"BTT",

];

}
148 changes: 148 additions & 0 deletions lib/FiatConveter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import 'package:currency_convertor/ConversionUtil.dart';
import 'package:currency_convertor/CurrencyList.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class FiatConveter extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return FiatConveterState();
}

}
class FiatConveterState extends State<FiatConveter> {
String baseCurrency="USD";
String targetCurrency="INR";
String convertedResult="Convert to";
TextEditingController baseController = new TextEditingController();
@override
Widget build(BuildContext context) {
// DONE: implement build
return WillPopScope(
onWillPop: _onBackPressed,
child: Scaffold (
appBar: AppBar(title: Text("Currency Converter", style: TextStyle(color: Colors.white),),),
backgroundColor: Colors.black,
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Fiat Currency Converter",style: TextStyle(color: Colors.blue,fontSize: 30),),
SizedBox(height: 150,),
Container(
child:
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child:
TextFormField(
keyboardType: TextInputType.number,
controller: baseController,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white70,
border: OutlineInputBorder(),
labelText: "Number",
labelStyle: TextStyle(color: Colors.blue),
hintText: "1.00",
),
),
),
baseCurrencyList(),
],
),
padding: EdgeInsets.all(20),
),
Container(
child:
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,

children: <Widget>[
SizedBox(width: 80,),
Expanded(
child:
Text(convertedResult,style: TextStyle(color: Colors.red,fontSize: 20),),
),
targetCurrencyList(),
],
),
padding: EdgeInsets.all(20),
),
RaisedButton(
child: Text("Convert"),
onPressed: () {
convertValues();
},
),
],
)
)
);
}
Widget baseCurrencyList() {
return DropdownButton<String>(
value: baseCurrency,
dropdownColor: Colors.black,
items: CurrencyList.currencies.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value,style: TextStyle(color: Colors.white),),
);
}).toList(),
onChanged: (String value) {
setState(() {
baseCurrency = value;
});
},
);
}
Widget targetCurrencyList() {
return DropdownButton<String>(
value: targetCurrency,
dropdownColor: Colors.black,
items: CurrencyList.currencies.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value,style: TextStyle(color: Colors.white),),
);
}).toList(),
onChanged: (String value) {
setState(() {
targetCurrency = value;
});
},
);
}

convertValues() async{
convertedResult = await ConversionUtil().converter(baseController.text, baseCurrency, targetCurrency);
setState(() {
});
}

Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Are you sure?',style: TextStyle(fontSize: 25),),
content: new Text('Do you want Logout ?',style: TextStyle(fontSize: 20)),
actions: <Widget>[
new GestureDetector(
onTap: () => Navigator.of(context).pop(false),
child: Text("NO",style: TextStyle(fontSize: 20,color: Colors.white,backgroundColor: Colors.blue)),
),
SizedBox(height: 20),
new GestureDetector(
onTap: () => Navigator.of(context).pop(true),
child: Text("YES",style: TextStyle(fontSize: 20,color: Colors.white,backgroundColor: Colors.red)),
),
],
),
) ??
false;
}

}
Loading

0 comments on commit 3740cdc

Please sign in to comment.