import 'package:flutter/material.dart';
+import 'package:flutterempires/player.dart';
void main() {
runApp(MyApp());
}
class _MyHomePageState extends State<MyHomePage> {
- int _counter = 0;
+ Future<Player> futurePlayer;
+ Future<List<Player>> allPlayers;
- void _incrementCounter() {
+ @override
+ void initState() {
+ super.initState();
+ futurePlayer = Player.fetchFirstPlayer();
+ allPlayers = Player.fetchAllPlayers();
+ }
+
+ void onPressPlusButton() {
setState(() {
- // This call to setState tells the Flutter framework that something has
- // changed in this State, which causes it to rerun the build method below
- // so that the display can reflect the updated values. If we changed
- // _counter without calling setState(), then the build method would not be
- // called again, and so nothing would appear to happen.
- _counter++;
+ // Probably use this to POST player name and character
});
}
),
body: new Container(
margin: const EdgeInsets.only(left: 20.0, right: 20.0),
- child : Center(
+ child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
- Spacer(flex: 10),
+ Spacer(flex: 1),
Text(
'Name:',
style: Theme.of(context).textTheme.headline4,
hintText: 'Enter your (real) name',
),
),
- Spacer(),
+ Spacer(flex: 1),
Text(
'Character:',
style: Theme.of(context).textTheme.headline4,
hintText: 'Enter your empire character name',
),
),
+ Spacer(flex: 1),
+ FutureBuilder<Player>(
+ future: futurePlayer,
+ builder: (context, snapshot) {
+ if (snapshot.hasData) {
+ return Text(snapshot.data.name);
+ } else if (snapshot.hasError) {
+ return Text("${snapshot.error}");
+ }
+ return CircularProgressIndicator();
+ },
+ ),
+ Spacer(flex: 1),
+ FutureBuilder<List<Player>>(
+ future: allPlayers,
+ builder: (context, snapshot) {
+ if (snapshot.hasData) {
+ return Text(snapshot.data.length.toString());
+ } else if (snapshot.hasError) {
+ return Text("${snapshot.error}");
+ }
+ // By default, show a loading spinner.
+ return CircularProgressIndicator();
+ },
+ ),
Spacer(flex: 10),
],
),
),
),
floatingActionButton: FloatingActionButton(
- onPressed: _incrementCounter,
+ onPressed: onPressPlusButton,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
--- /dev/null
+import 'dart:convert';
+
+import 'package:http/http.dart' as http;
+
+class Player {
+ final int id;
+ final String name;
+
+ Player({this.id, this.name});
+
+ factory Player.fromJson(Map<String, dynamic> json) {
+ return Player(
+ id: json['id'],
+ name: json['name'],
+ );
+ }
+
+ static Future<Player> fetchFirstPlayer() async {
+ final response = await http.get('https://families.cworth.org/api/players');
+
+ if (response.statusCode == 200) {
+ List<Player> playerList = parsePlayers(response.body);
+ return playerList.elementAt(0);
+ } else {
+ throw Exception('Failed to load player');
+ }
+ }
+
+ static Future<List<Player>> fetchAllPlayers() async {
+ final response = await http.get('https://families.cworth.org/api/players');
+
+ if (response.statusCode == 200) {
+ return parsePlayers(response.body);
+ } else {
+ throw Exception('Failed to load players');
+ }
+ }
+
+ static List<Player> parsePlayers(String responseBody) {
+ final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
+ return parsed.map<Player>((json) => Player.fromJson(json)).toList();
+ }
+}