Closed contemsolutions closed 3 years ago
Good day,
My currency value is not updating, though the currency changes. can anyone help?
CoinData
`import 'package:http/http.dart' as http; import 'dart:convert'; import 'constants.dart'; import 'price_screen.dart';
const List currenciesList = [ 'AUD', 'BRL', 'CAD', 'CNY', 'EUR', 'GBP', 'HKD', 'IDR', 'ILS', 'INR', 'JPY', 'MXN', 'NOK', 'NGN', 'NZD', 'PLN', 'RON', 'RUB', 'SEK', 'SGD', 'USD', 'ZAR' ];
const List cryptoList = [ 'BTC', 'ETH', 'LTC', ];
String cryptoName = 'BTC';
class CoinData {
Future getCoinData(String selectedCurrency) async {
String requestURL = '$coinAPIURL/$cryptoName/$selectedCurrency?apikey=$apiKey'; http.Response response = await http.get(Uri.parse(requestURL)); if(response.statusCode == 200) { var decodeData = jsonDecode(response.body); var lastPrice = decodeData['rate']; return lastPrice; } else { print(response.statusCode); throw 'Problem with the get request'; }
}
}`
PriceScreen
`import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'coin_data.dart'; import 'dart:io' show Platform;
class PriceScreen extends StatefulWidget { @override _PriceScreenState createState() => _PriceScreenState(); }
class _PriceScreenState extends State { String selectedCurrency = 'USD'; String cryptoName = 'BTC';
DropdownButton androidDropdown() { List<DropdownMenuItem> dropdownItems = []; for (String currency in currenciesList) { setState(() { var newItem = DropdownMenuItem( child: Text(currency), value: currency, ); dropdownItems.add(newItem); }); }
return DropdownButton<String>( value: selectedCurrency, items: dropdownItems, iconSize: 40, isExpanded: true, onChanged: (value) { setState(() { selectedCurrency = value; }); }, );
CupertinoPicker iOSPicker() { List pickerItems = []; for (String currency in currenciesList) { pickerItems.add(Text(currency)); }
return CupertinoPicker( backgroundColor: Colors.lightBlue, itemExtent: 32.0, onSelectedItemChanged: (selectedIndex) { print(selectedIndex); setState(() { selectedCurrency = currenciesList[selectedIndex]; getData(); }); }, children: pickerItems, );
String bitcoinValue = '?';
void getData() async { try { double data = await CoinData().getCoinData(selectedCurrency); setState(() { bitcoinValue = data.toStringAsFixed(0); }); } catch (e) { print(e); } }
@override void initState() { super.initState(); getData(); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('🤑 Coin Ticker'), ), body: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Padding( padding: EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0), child: Card( color: Colors.lightBlueAccent, elevation: 5.0, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10.0), ), child: Padding( padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 28.0), child: Text( '1 $cryptoName = $bitcoinValue $selectedCurrency', textAlign: TextAlign.center, style: TextStyle( fontSize: 20.0, color: Colors.white, ), ), ), ), ), DropdownButtonHideUnderline( child: Container( height: 150.0, alignment: Alignment.center, padding: EdgeInsets.only(bottom: 30.0), color: Colors.lightBlue, child: Container( margin: EdgeInsets.all(16), padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4), decoration: BoxDecoration( border: Border.all(color: Colors.white, width: 4), borderRadius: BorderRadius.circular(12), ), child: Platform.isIOS ? iOSPicker() : androidDropdown(), ), ), ), ], ), ); } }`
Sorry, found the issue
Good day,
My currency value is not updating, though the currency changes. can anyone help?
CoinData
`import 'package:http/http.dart' as http; import 'dart:convert'; import 'constants.dart'; import 'price_screen.dart';
const List currenciesList = [
'AUD',
'BRL',
'CAD',
'CNY',
'EUR',
'GBP',
'HKD',
'IDR',
'ILS',
'INR',
'JPY',
'MXN',
'NOK',
'NGN',
'NZD',
'PLN',
'RON',
'RUB',
'SEK',
'SGD',
'USD',
'ZAR'
];
const List cryptoList = [
'BTC',
'ETH',
'LTC',
];
String cryptoName = 'BTC';
class CoinData {
Future getCoinData(String selectedCurrency) async {
}
}`
PriceScreen
`import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'coin_data.dart'; import 'dart:io' show Platform;
class PriceScreen extends StatefulWidget { @override _PriceScreenState createState() => _PriceScreenState(); }
class _PriceScreenState extends State {
String selectedCurrency = 'USD';
String cryptoName = 'BTC';
DropdownButton androidDropdown() {
List<DropdownMenuItem> dropdownItems = [];
for (String currency in currenciesList) {
setState(() {
var newItem = DropdownMenuItem(
child: Text(currency),
value: currency,
);
dropdownItems.add(newItem);
});
}
}
CupertinoPicker iOSPicker() { List pickerItems = [];
for (String currency in currenciesList) {
pickerItems.add(Text(currency));
}
}
String bitcoinValue = '?';
void getData() async { try { double data = await CoinData().getCoinData(selectedCurrency); setState(() { bitcoinValue = data.toStringAsFixed(0); }); } catch (e) { print(e); } }
@override void initState() { super.initState(); getData(); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('🤑 Coin Ticker'), ), body: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.stretch, children:[
Padding(
padding: EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0),
child: Card(
color: Colors.lightBlueAccent,
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 28.0),
child: Text(
'1 $cryptoName = $bitcoinValue $selectedCurrency',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
),
DropdownButtonHideUnderline(
child: Container(
height: 150.0,
alignment: Alignment.center,
padding: EdgeInsets.only(bottom: 30.0),
color: Colors.lightBlue,
child: Container(
margin: EdgeInsets.all(16),
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4),
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 4),
borderRadius: BorderRadius.circular(12),
),
child: Platform.isIOS ? iOSPicker() : androidDropdown(),
),
),
),
],
),
);
}
}`