-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
389 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.