Advertisement
rifansyah

main.dart cryptoapp

Sep 23rd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.58 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'dart:convert';
  4. import 'package:http/http.dart' as http;
  5.  
  6. final String apiUrl = 'https://api.coinmarketcap.com/v1/ticker/?convert=IDR&limit=20';
  7.  
  8. @override
  9. Future<List> fetchCurrencies() async {
  10.     http.Response response = await http.get(apiUrl);
  11.     String responseBody = response.body;
  12.     final statusCode = response.statusCode;
  13.  
  14.     if (statusCode < 200 || statusCode >= 300 || responseBody == Null) {
  15.       throw new FetchDataException('An error occured : [Status Code : $statusCode]');
  16.     }
  17.  
  18.     final List cryptoItems = json.decode(responseBody);
  19.     return cryptoItems;
  20. }
  21.  
  22. class FetchDataException implements Exception {
  23.   final _message;
  24.  
  25.   FetchDataException([this._message]);
  26.  
  27.   String toString() {
  28.     if (_message == null) return "Exception";
  29.     return "Exception: $_message";
  30.   }
  31. }
  32.  
  33. void main() async {
  34.   List currencies = await fetchCurrencies();
  35.  
  36.   runApp(
  37.     new MaterialApp(
  38.       title: 'Flutter Demo',
  39.       theme: new ThemeData(
  40.         primarySwatch: Colors.purple,
  41.       ),
  42.       home: new CryptoHome(currencies),
  43.     )
  44.   );
  45. }
  46.  
  47. class CryptoHome extends StatelessWidget {
  48.  
  49.   final List _currencies;
  50.   final List<MaterialColor> _colors = [Colors.blue, Colors.indigo, Colors.red];
  51.  
  52.   CryptoHome(this._currencies);
  53.  
  54.   @override
  55.   Widget build(BuildContext context) {
  56.     return new Scaffold(
  57.       body: _buildBody(),
  58.       backgroundColor: Colors.purple,
  59.     );
  60.   }
  61.  
  62.   Widget _buildBody() {
  63.     return new Container(
  64.       margin: const EdgeInsets.fromLTRB(8.0, 56.0, 8.0, 0.0),
  65.       child: new Column(
  66.         children: <Widget>[
  67.           _getAppTitleWidget(),
  68.           _getListViewWidget()
  69.         ],
  70.       ),
  71.     );
  72.   }
  73.  
  74.   Widget _getAppTitleWidget() {
  75.     return new Container(
  76.       margin: const EdgeInsets.only(bottom: 16.0),
  77.       child: new Center(
  78.         child: new Text(
  79.         'Cryptocurrencies',
  80.         style: new TextStyle(
  81.           color: Colors.white,
  82.           fontWeight: FontWeight.bold,
  83.           fontSize: 24.0
  84.           ),
  85.         )
  86.       )
  87.     );
  88.   }
  89.  
  90.   Widget _getListViewWidget() {
  91.     return new Flexible(
  92.       child: new ListView.builder(
  93.         itemCount: _currencies.length,
  94.         itemBuilder: (context, index) {
  95.           final Map currency = _currencies[index];
  96.           final MaterialColor color = _colors[index % _colors.length];
  97.  
  98.           return _getListItemWidget(currency, color);
  99.         },
  100.       ),
  101.     );
  102.   }
  103.  
  104.   Container _getListItemWidget(Map currency, MaterialColor color) {
  105.     return new Container(
  106.       margin: const EdgeInsets.only(top: 5.0),
  107.       child: new Card(
  108.         child: _getListTileWidget(currency, color),
  109.       ),
  110.     );
  111.   }
  112.  
  113.   ListTile _getListTileWidget(Map currency, MaterialColor color) {
  114.     return new ListTile(
  115.       leading: _getLeadingWidget(currency['name'], color),
  116.       title: _getTitleWidget(currency['name']),
  117.       subtitle: _getSubtitleWidget(currency['price_usd'], currency['percent_change_1h']),
  118.       isThreeLine: true,
  119.     );
  120.   }
  121.  
  122.   CircleAvatar _getLeadingWidget(String currencyName, MaterialColor color) {
  123.     return new CircleAvatar(
  124.       backgroundColor: color,
  125.       child: new Text(currencyName[0]),
  126.     );
  127.   }
  128.  
  129.   Text _getTitleWidget(String currencyName) {
  130.     return new Text(
  131.       currencyName,
  132.       style: new TextStyle(
  133.         fontWeight: FontWeight.bold
  134.       ),
  135.     );
  136.   }
  137.  
  138.   Text _getSubtitleWidget(String priceUsd, String percentChange1h) {
  139.     return new Text(
  140.       '\$$priceUsd\n1 hour: $percentChange1h'
  141.     );
  142.   }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement