Skip to content

Instantly share code, notes, and snippets.

View Elvis10ten's full-sized avatar

Elvis Chidera Elvis10ten

View GitHub Profile
itemBuilder: (context, index) {
final Map currency = _currencies[index];
final MaterialColor color = _colors[index % _colors.length];
}
Widget _getListViewWidget() {
// We want the ListView to have the flexibility to expand to fill the
// available space in the vertical axis
return new Flexible(
child: new ListView.builder(
// The number of items to show
itemCount: _currencies.length,
// Callback that should return ListView children
// The index parameter = 0...(itemCount-1)
itemBuilder: (context, index) {
@Elvis10ten
Elvis10ten / main.dart
Last active March 5, 2018 22:57
Note: Title text widget.
Widget _getAppTitleWidget() {
return new Text(
'Cryptocurrencies',
style: new TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 24.0),
);
}
Widget _buildBody() {
return new Container(
// A top margin of 56.0. A left and right margin of 8.0. And a bottom margin of 0.0.
margin: const EdgeInsets.fromLTRB(8.0, 56.0, 8.0, 0.0),
child: new Column(
// A column widget can have several widgets that are placed in a top down fashion
children: <Widget>[
_getAppTitleWidget(),
_getListViewWidget()
],
@Elvis10ten
Elvis10ten / main.dart
Created March 4, 2018 18:55
Note: A scaffold implements the basic material design visual layout structure.
@override
Widget build(BuildContext context) {
return new Scaffold(
body: _buildBody(),
backgroundColor: Colors.blue,
);
}
@Elvis10ten
Elvis10ten / main.dart
Last active March 5, 2018 22:39
Note: A widget’s main job is to implement a build function, which describes the widget in terms of other, lower-level widgets.
class CryptoListWidget extends StatelessWidget {
// This is a list of material colors. Feel free to add more colors, it won't break the code
final List<MaterialColor> _colors = [Colors.blue, Colors.indigo, Colors.red];
// The underscore before a variable name marks it as a private variable
final List _currencies;
// This is a constructor in Dart. We are assigning the value passed to the constructor
// to the _currencies variable
CryptoListWidget(this._currencies);
void main() async {
// Bad practice alert :). You should ideally show the UI, and probably a progress view,
// then when the requests completes, update the UI to show the data.
List currencies = await getCurrencies();
print(currencies);
runApp(new MaterialApp(
home: new CryptoListWidget(currencies),
));
}
@Elvis10ten
Elvis10ten / main.dart
Created March 4, 2018 18:38
Note: Imports the important Flutter material package.
import 'package:flutter/material.dart';
@Elvis10ten
Elvis10ten / main.dart
Created March 4, 2018 18:37
Note: Imports the packages required by the getCurrencies() function.
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
// Since we are using await within the main function, we have to make it asynchronous too
void main() async {
// Testing the getCurrencies function
// We wait for the currency data to arrive
List currencies = await getCurrencies();
// Before printing it to the Console
print(currencies);
runApp(new MaterialApp(
home: new Center(