Skip to content

Instantly share code, notes, and snippets.

@vladnicula
Created June 5, 2021 04:28
Show Gist options
  • Save vladnicula/b6b266581cc0074a820241ce408179f0 to your computer and use it in GitHub Desktop.
Save vladnicula/b6b266581cc0074a820241ce408179f0 to your computer and use it in GitHub Desktop.
Hello Flutter 08
import 'package:flutter/material.dart';
class MyHelloWorld extends StatefulWidget {
const MyHelloWorld({Key? key}) : super(key: key);
@override
MyHelloWorldState createState() => MyHelloWorldState();
}
class MyHelloWorldState extends State<MyHelloWorld> {
int pos = 0;
@override
build (BuildContext context) {
return Center(
child: FutureBuilder<dynamic>(
future: StartWarsApi.fetchPerson(pos),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Column(mainAxisSize: MainAxisSize.min, children: [
Text(snapshot.data["name"]),
TextButton(
onPressed: () {
setState(() {
pos += 1;
});
},
child: Text("Next Person")),
]);
}
return Text("Loading");
})
);
}
}
void main() {
runApp(
MaterialApp(
title: 'Hello From Flutter', // used by the OS task switcher
home: SafeArea(
child: Scaffold(
body: MyHelloWorld()
),
),
));
}
// Since dart pad does not allow us to fetch data from the internets, we mock these 3 legendary
// star wars people here and simulate the fetch for the purpose of the demo.
class StartWarsApi {
static Future<dynamic> fetchPerson(int id) async {
if ( id >= 3 ) {
id = id % 3;
}
await Future.delayed(Duration(seconds: 1));
return mockData[id];
}
}
const mockData = [
{
"id": 1,
"name": "Luke Skywalker",
"height": 1.72,
"mass": 73,
"gender": "male",
"homeworld": "tatooine",
"wiki": "http://starwars.wikia.com/wiki/Luke_Skywalker",
"image": "https://vignette.wikia.nocookie.net/starwars/images/2/20/LukeTLJ.jpg",
"born": -19,
"bornLocation": "polis massa",
"died": 34,
"diedLocation": "ahch-to",
"species": "human",
"hairColor": "blond",
"eyeColor": "blue",
"skinColor": "light",
"cybernetics": "Prosthetic right hand",
"affiliations": [
"Alliance to Restore the Republic",
"Red Squadron",
"Rogue Squadron",
"Massassi Group",
"Leia Organa's team",
"Endor strike team",
"Jedi Order",
"Bright Tree tribe",
"New Republic",
"Resistance"
],
"masters": [
"Obi-Wan Kenobi",
"Yoda"
],
"apprentices": [
"Leia Organa",
"Ben Solo (along with a dozen apprentices)",
"Rey"
],
"formerAffiliations": []
},
{
"id": 2,
"name": "C-3PO",
"height": 1.71,
"mass": 75,
"gender": "male",
"homeworld": "tatooine",
"species": "droid",
"wiki": "http://starwars.wikia.com/wiki/C-3PO",
"image": "https://vignette.wikia.nocookie.net/starwars/images/3/3f/C-3PO_TLJ_Card_Trader_Award_Card.png",
"dateCreated": -32,
"dateDestroyed": 3,
"destroyedLocation": "bespin, rebuilt shortly after",
"creator": "Anakin Skywalker",
"manufacturer": "Cybot Galactica",
"model": "3PO unit",
"class": "Protocol droid",
"sensorColor": "yellow",
"platingColor": "gray, later primarily golden",
"equipment": "TranLang III communication module",
"affiliations": [
"Skywalker family",
"Confederacy of Independent Systems",
"Royal House of Naboo",
"Galactic Republic",
"House of Organa",
"Galactic Empire",
"Alliance to Restore the Republic",
"Massassi Group",
"Leia Organa's team",
"Pathfinders",
"Endor strike team",
"Bright Tree tribe",
"New Republic",
"Resistance",
"Resistance spy droid network"
],
"skinColor": "gold",
"eyeColor": "yellow",
"born": -112,
"formerAffiliations": []
},
{
"id": 3,
"name": "R2-D2",
"height": 1.09,
"mass": 32,
"gender": "male",
"homeworld": "naboo",
"species": "droid",
"wiki": "http://starwars.wikia.com/wiki/R2-D2",
"image": "https://vignette.wikia.nocookie.net/starwars/images/e/eb/ArtooTFA2-Fathead.png",
"manufacturer": "Industrial Automaton",
"productLine": "R-series",
"model": "R2 series astromech droid",
"class": "Astromech droid",
"sensorColor": "red",
"platingColor": "bluesilver",
"equipment": [
"Buzz saw",
"Electric pike",
"Drinks tray (Only on Jabba's Sail Barge)",
"Fusion welder",
"Data probe",
"Power recharge coupler",
"Rocket booster",
"Holoprojector",
"Motorized, all-terrain treads",
"Retractable third leg"
],
"affiliations": [
"Royal House of Naboo",
"Galactic Republic",
"501st Legion",
"R2-D2's battle droid squadron",
"D-Squad",
"House of Organa",
"Galactic Empire",
"Alliance to Restore the Republic",
"Massassi Group",
"Red Squadron",
"Leia Organa's team",
"Endor strike team",
"Bright Tree tribe",
"Resistance"
],
"skinColor": "white, blue",
"eyeColor": "red",
"born": -33,
"formerAffiliations": []
},
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment