Bitcoin Ticker
API site :
https://apiv2.bitcoinaverage.com/#ticker-data-per-symbol
- Final code
- pubspec.yaml
name: bitcoin_ticker
description: A new Flutter application.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
http: ^0.12.0+2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
- main.dart
import 'package:flutter/material.dart';
import 'price_screen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
primaryColor: Colors.lightBlue,
scaffoldBackgroundColor: Colors.white),
home: PriceScreen(),
);
}
}
- price_screen.dart
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<PriceScreen> {
String selectedCurrency = 'AUD';
DropdownButton<String> androidDropdown() {
List<DropdownMenuItem<String>> dropdownItems = [];
for (String currency in currenciesList) {
var newItem = DropdownMenuItem(
child: Text(currency),
value: currency,
);
dropdownItems.add(newItem);
}
return DropdownButton<String>(
value: selectedCurrency,
items: dropdownItems,
onChanged: (value) {
setState(() {
selectedCurrency = value;
getData();
});
},
);
}
CupertinoPicker iOSPicker() {
List<Text> pickerItems = [];
for (String currency in currenciesList) {
pickerItems.add(Text(currency));
}
return CupertinoPicker(
backgroundColor: Colors.lightBlue,
itemExtent: 32.0,
onSelectedItemChanged: (selectedIndex) {
setState(() {
selectedCurrency = currenciesList[selectedIndex];
getData();
});
},
children: pickerItems,
);
}
Map<String, String> coinValues = {};
bool isWaiting = false;
void getData() async {
isWaiting = true;
try {
var data = await CoinData().getCoinData(selectedCurrency);
isWaiting = false;
setState(() {
coinValues = data;
});
} catch (e) {
print(e);
}
}
@override
void initState() {
super.initState();
getData();
}
Column makeCards() {
List<CryptoCard> cryptoCards = [];
for (String crypto in cryptoList) {
cryptoCards.add(
CryptoCard(
cryptoCurrency: crypto,
selectedCurrency: selectedCurrency,
value: isWaiting ? '?' : coinValues[crypto],
),
);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: cryptoCards,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('🤑 Coin Ticker'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
makeCards(),
Container(
height: 150.0,
alignment: Alignment.center,
padding: EdgeInsets.only(bottom: 30.0),
color: Colors.lightBlue,
child: Platform.isIOS ? iOSPicker() : androidDropdown(),
),
],
),
);
}
}
class CryptoCard extends StatelessWidget {
const CryptoCard({
this.value,
this.selectedCurrency,
this.cryptoCurrency,
});
final String value;
final String selectedCurrency;
final String cryptoCurrency;
@override
Widget build(BuildContext context) {
return 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 $cryptoCurrency = $value $selectedCurrency',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
);
}
}
- coin.dart
import 'dart:convert';
import 'package:http/http.dart' as http;
const List<String> currenciesList = [
'AUD',
'BRL',
'CAD',
'CNY',
'EUR',
'GBP',
'HKD',
'IDR',
'ILS',
'INR',
'JPY',
'MXN',
'NOK',
'NZD',
'PLN',
'RON',
'RUB',
'SEK',
'SGD',
'USD',
'ZAR'
];
const List<String> cryptoList = ['BTC', 'ETH', 'LTC'];
const bitcoinAverageURL =
'https://apiv2.bitcoinaverage.com/indices/global/ticker';
class CoinData {
Future getCoinData(String selectedCurrency) async {
Map<String, String> cryptoPrices = {};
for (String crypto in cryptoList) {
String requestURL = '$bitcoinAverageURL/$crypto$selectedCurrency';
http.Response response = await http.get(requestURL);
if (response.statusCode == 200) {
var decodedData = jsonDecode(response.body);
double lastPrice = decodedData['last'];
cryptoPrices[crypto] = lastPrice.toStringAsFixed(0);
} else {
print(response.statusCode);
throw 'Problem with the get request';
}
}
return cryptoPrices;
}
}
'플러터(Flutter) > 플러터 실습(Flutter Practice)' 카테고리의 다른 글
ToDo app (0) | 2020.03.18 |
---|