]> git.cworth.org Git - lmno.games/blob - flutterempires/lib/main.dart
7d972a3a44b1655b0fa9f88aa02337da3cec226f
[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   int _counter = 0;
55
56   Future<Player> futurePlayer;
57   Future<List<Player>> allPlayers;
58
59   @override
60   void initState() {
61     super.initState();
62     futurePlayer = Player.fetchFirstPlayer();
63     allPlayers = Player.fetchAllPlayers();
64   }
65
66   void _incrementCounter() {
67     setState(() {
68       // This call to setState tells the Flutter framework that something has
69       // changed in this State, which causes it to rerun the build method below
70       // so that the display can reflect the updated values. If we changed
71       // _counter without calling setState(), then the build method would not be
72       // called again, and so nothing would appear to happen.
73       _counter++;
74     });
75   }
76
77   @override
78   Widget build(BuildContext context) {
79     // This method is rerun every time setState is called, for instance as done
80     // by the _incrementCounter method above.
81     //
82     // The Flutter framework has been optimized to make rerunning build methods
83     // fast, so that you can just rebuild anything that needs updating rather
84     // than having to individually change instances of widgets.
85     return Scaffold(
86       appBar: AppBar(
87         // Here we take the value from the MyHomePage object that was created by
88         // the App.build method, and use it to set our appbar title.
89         title: Text(widget.title),
90       ),
91       body: new Container(
92         margin: const EdgeInsets.only(left: 20.0, right: 20.0),
93         child : Center(
94           child: Column(
95             mainAxisAlignment: MainAxisAlignment.center,
96             crossAxisAlignment: CrossAxisAlignment.start,
97             children: <Widget>[
98               Spacer(flex: 1),
99               Text(
100                 'Name:',
101                 style: Theme.of(context).textTheme.headline4,
102               ),
103               TextField(
104                 decoration: InputDecoration(
105                   hintText: 'Enter your (real) name',
106                 ),
107               ),
108               Spacer(flex: 1),
109               Text(
110                 'Character:',
111                 style: Theme.of(context).textTheme.headline4,
112               ),
113               TextField(
114                 decoration: InputDecoration(
115                   hintText: 'Enter your empire character name',
116                 ),
117               ),
118               Spacer(flex: 1),
119               FutureBuilder<Player>(
120                 future: futurePlayer,
121                 builder: (context, snapshot) {
122                   if (snapshot.hasData) {
123                     return Text(snapshot.data.name);
124                   } else if (snapshot.hasError) {
125                     return Text("${snapshot.error}");
126                   }
127                   return CircularProgressIndicator();
128                 },
129               ),
130               Spacer(flex: 1),
131               FutureBuilder<List<Player>>(
132                 future: allPlayers,
133                 builder: (context, snapshot) {
134                   if (snapshot.hasData) {
135                     return Text(snapshot.data.length.toString());
136                   } else if (snapshot.hasError) {
137                     return Text("${snapshot.error}");
138                   }
139                   // By default, show a loading spinner.
140                   return CircularProgressIndicator();
141                 },
142               ),
143               Spacer(flex: 10),
144             ],
145           ),
146         ),
147       ),
148       floatingActionButton: FloatingActionButton(
149         onPressed: _incrementCounter,
150         tooltip: 'Increment',
151         child: Icon(Icons.add),
152       ), // This trailing comma makes auto-formatting nicer for build methods.
153     );
154   }
155 }