Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'dart:async';
- import 'dart:convert';
- import 'package:http/http.dart' as http;
- final String apiUrl = 'https://api.coinmarketcap.com/v1/ticker/?convert=IDR&limit=20';
- @override
- Future<List> fetchCurrencies() async {
- http.Response response = await http.get(apiUrl);
- String responseBody = response.body;
- final statusCode = response.statusCode;
- if (statusCode < 200 || statusCode >= 300 || responseBody == Null) {
- throw new FetchDataException('An error occured : [Status Code : $statusCode]');
- }
- final List cryptoItems = json.decode(responseBody);
- return cryptoItems;
- }
- class FetchDataException implements Exception {
- final _message;
- FetchDataException([this._message]);
- String toString() {
- if (_message == null) return "Exception";
- return "Exception: $_message";
- }
- }
- void main() async {
- List currencies = await fetchCurrencies();
- runApp(
- new MaterialApp(
- title: 'Flutter Demo',
- theme: new ThemeData(
- primarySwatch: Colors.purple,
- ),
- home: new CryptoHome(currencies),
- )
- );
- }
- class CryptoHome extends StatelessWidget {
- final List _currencies;
- final List<MaterialColor> _colors = [Colors.blue, Colors.indigo, Colors.red];
- CryptoHome(this._currencies);
- @override
- Widget build(BuildContext context) {
- return new Scaffold(
- body: _buildBody(),
- backgroundColor: Colors.purple,
- );
- }
- Widget _buildBody() {
- return new Container(
- margin: const EdgeInsets.fromLTRB(8.0, 56.0, 8.0, 0.0),
- child: new Column(
- children: <Widget>[
- _getAppTitleWidget(),
- _getListViewWidget()
- ],
- ),
- );
- }
- Widget _getAppTitleWidget() {
- return new Container(
- margin: const EdgeInsets.only(bottom: 16.0),
- child: new Center(
- child: new Text(
- 'Cryptocurrencies',
- style: new TextStyle(
- color: Colors.white,
- fontWeight: FontWeight.bold,
- fontSize: 24.0
- ),
- )
- )
- );
- }
- Widget _getListViewWidget() {
- return new Flexible(
- child: new ListView.builder(
- itemCount: _currencies.length,
- itemBuilder: (context, index) {
- final Map currency = _currencies[index];
- final MaterialColor color = _colors[index % _colors.length];
- return _getListItemWidget(currency, color);
- },
- ),
- );
- }
- Container _getListItemWidget(Map currency, MaterialColor color) {
- return new Container(
- margin: const EdgeInsets.only(top: 5.0),
- child: new Card(
- child: _getListTileWidget(currency, color),
- ),
- );
- }
- ListTile _getListTileWidget(Map currency, MaterialColor color) {
- return new ListTile(
- leading: _getLeadingWidget(currency['name'], color),
- title: _getTitleWidget(currency['name']),
- subtitle: _getSubtitleWidget(currency['price_usd'], currency['percent_change_1h']),
- isThreeLine: true,
- );
- }
- CircleAvatar _getLeadingWidget(String currencyName, MaterialColor color) {
- return new CircleAvatar(
- backgroundColor: color,
- child: new Text(currencyName[0]),
- );
- }
- Text _getTitleWidget(String currencyName) {
- return new Text(
- currencyName,
- style: new TextStyle(
- fontWeight: FontWeight.bold
- ),
- );
- }
- Text _getSubtitleWidget(String priceUsd, String percentChange1h) {
- return new Text(
- '\$$priceUsd\n1 hour: $percentChange1h'
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement