]> git.cworth.org Git - lmno.games/blob - flutterempires/lib/main.dart
89867ec99bb629883d56040ca5defdd9d28a1d33
[lmno.games] / flutterempires / lib / main.dart
1 import 'package:flutter/material.dart';
2 import 'package:flutterempires/player.dart';
3
4 void main() {
5   runApp(MyApp());
6 }
7
8 class MyApp extends StatelessWidget {
9   // This widget is the root of your application.
10   @override
11   Widget build(BuildContext context) {
12     return MaterialApp(
13       title: 'Empires',
14       theme: ThemeData(
15         // This is the theme of your application.
16         //
17         // Try running your application with "flutter run". You'll see the
18         // application has a blue toolbar. Then, without quitting the app, try
19         // changing the primarySwatch below to Colors.green and then invoke
20         // "hot reload" (press "r" in the console where you ran "flutter run",
21         // or simply save your changes to "hot reload" in a Flutter IDE).
22         // Notice that the counter didn't reset back to zero; the application
23         // is not restarted.
24         primarySwatch: Colors.blue,
25         // This makes the visual density adapt to the platform that you run
26         // the app on. For desktop platforms, the controls will be smaller and
27         // closer together (more dense) than on mobile platforms.
28         visualDensity: VisualDensity.adaptivePlatformDensity,
29       ),
30       home: MyHomePage(title: 'Empires'),
31     );
32   }
33 }
34
35 class MyHomePage extends StatefulWidget {
36   MyHomePage({Key key, this.title}) : super(key: key);
37
38   // This widget is the home page of your application. It is stateful, meaning
39   // that it has a State object (defined below) that contains fields that affect
40   // how it looks.
41
42   // This class is the configuration for the state. It holds the values (in this
43   // case the title) provided by the parent (in this case the App widget) and
44   // used by the build method of the State. Fields in a Widget subclass are
45   // always marked "final".
46
47   final String title;
48
49   @override
50   _MyHomePageState createState() => _MyHomePageState();
51 }
52
53 class _MyHomePageState extends State<MyHomePage> {
54   Future<Player> futurePlayer;
55   Future<List<Player>> allPlayers;
56
57   @override
58   void initState() {
59     super.initState();
60     futurePlayer = Player.fetchFirstPlayer();
61     allPlayers = Player.fetchAllPlayers();
62   }
63
64   void onPressPlusButton() {
65     setState(() {
66       // Probably use this to POST player name and character
67       allPlayers = Player.fetchAllPlayers();
68     });
69   }
70
71   @override
72   Widget build(BuildContext context) {
73     // This method is rerun every time setState is called, for instance as done
74     // by the _incrementCounter method above.
75     //
76     // The Flutter framework has been optimized to make rerunning build methods
77     // fast, so that you can just rebuild anything that needs updating rather
78     // than having to individually change instances of widgets.
79     return Scaffold(
80       appBar: AppBar(
81         // Here we take the value from the MyHomePage object that was created by
82         // the App.build method, and use it to set our appbar title.
83         title: Text(widget.title),
84       ),
85       body: new Container(
86         margin: const EdgeInsets.only(left: 20.0, right: 20.0),
87         child: Center(
88           child: Column(
89             mainAxisAlignment: MainAxisAlignment.center,
90             crossAxisAlignment: CrossAxisAlignment.start,
91             children: <Widget>[
92               Spacer(flex: 1),
93               Text(
94                 'Name:',
95                 style: Theme.of(context).textTheme.headline4,
96               ),
97               TextField(
98                 decoration: InputDecoration(
99                   hintText: 'Enter your (real) name',
100                 ),
101               ),
102               Spacer(flex: 1),
103               Text(
104                 'Character:',
105                 style: Theme.of(context).textTheme.headline4,
106               ),
107               TextField(
108                 decoration: InputDecoration(
109                   hintText: 'Enter your empire character name',
110                 ),
111               ),
112               Spacer(flex: 1),
113               FutureBuilder<Player>(
114                 future: futurePlayer,
115                 builder: (context, snapshot) {
116                   if (snapshot.hasData) {
117                     return Text(snapshot.data.name);
118                   } else if (snapshot.hasError) {
119                     return Text("${snapshot.error}");
120                   }
121                   return CircularProgressIndicator();
122                 },
123               ),
124               Spacer(flex: 1),
125               FutureBuilder<List<Player>>(
126                 future: allPlayers,
127                 builder: (context, snapshot) {
128                   if (snapshot.hasData) {
129                     return Text(snapshot.data.length.toString());
130                   } else if (snapshot.hasError) {
131                     return Text("${snapshot.error}");
132                   }
133                   // By default, show a loading spinner.
134                   return CircularProgressIndicator();
135                 },
136               ),
137               Expanded(
138                 flex: 10,
139                 child: FutureBuilder<List<Player>>(
140                     future: allPlayers,
141                     builder: (context, snapshot) {
142                       if (snapshot.hasData) {
143                         if (snapshot.data.length == 0) {
144                           return Text('No players yet');
145                         } else {
146                           return ListView.builder(
147                               itemCount: snapshot.data.length,
148                               itemBuilder: (context, index) {
149                                 return ListTile(
150                                     title: Text(
151                                         snapshot.data[index].name.toString()));
152                               });
153                         }
154                       } else if (snapshot.hasError) {
155                         return Text("${snapshot.error}");
156                       }
157                       return CircularProgressIndicator();
158                     }),
159               )
160             ],
161           ),
162         ),
163       ),
164       floatingActionButton: FloatingActionButton(
165         onPressed: onPressPlusButton,
166         tooltip: 'Increment',
167         child: Icon(Icons.add),
168       ), // This trailing comma makes auto-formatting nicer for build methods.
169     );
170   }
171 }