Created
May 30, 2021 07:29
-
-
Save vladnicula/f80a4adc128b9bd410eb133422b25e3e to your computer and use it in GitHub Desktop.
Hello Flutter 07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | |
String message = "Loading..."; | |
int pos = 0; | |
@override | |
initState () { | |
super.initState(); | |
getNextStartWarsPerson(); | |
} | |
getNextStartWarsPerson () { | |
setState(() { | |
message = "Loading..."; | |
}); | |
StartWarsApi.fetchPerson(pos++) | |
.then((response) { | |
setState(() { | |
message = response['name']; | |
}); | |
}) | |
.catchError((err) { | |
setState(() { | |
message = err; | |
}); | |
}); | |
} | |
@override | |
build (BuildContext context) { | |
return Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Text(message), | |
TextButton( | |
onPressed: () { | |
getNextStartWarsPerson(); | |
}, | |
child: Text("Next Person") | |
) | |
] | |
) | |
); | |
} | |
} | |
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 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