/
main.dart
141 lines (132 loc) · 3.7 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import 'package:flutter/material.dart';
import 'package:trivia_flutter_demo/repository.dart';
import 'package:trivia_flutter_demo/widget/answers_container.dart';
import 'package:trivia_flutter_demo/widget/question_container.dart';
import 'package:flutter_stetho/flutter_stetho.dart';
void main() {
Stetho.initialize();
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Open Trivia',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Open Trivia'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: null,
body: _newQuestion(),
floatingActionButton: new FloatingActionButton(
backgroundColor: Colors.blueGrey,
onPressed: () {
setState(() {
});
},
tooltip: 'Next question',
child: new Icon(
Icons.skip_next,
color: Colors.white,
),
),
);
}
@override
void initState() {
super.initState();
}
Widget _newQuestion() {
return FutureBuilder(
future: Repository.get().fetchQuestion(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
return _getLoadingWidget();
case ConnectionState.done:
return _getMainWidget(snapshot.data);
}
});
}
Widget _getMainWidget(question) {
return SafeArea(
child: Padding(
padding: const EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 0.0),
child: Column(
children: <Widget>[
Text(
question.category,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.blueGrey,
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
QuestionContainer(question: question.question),
],
),
),
AnswersContainer(
question: question,
onQuestionAnswered: () {
setState(() {
});
},
),
],
),
),
);
}
Widget _getLoadingWidget() {
return SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
width: 40.0,
height: 40.0,
child: CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(Colors.blueGrey),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
child: Text(
'Loading',
style: TextStyle(
color: Colors.blueGrey,
fontSize: 26.0,
fontWeight: FontWeight.w400,
),
),
),
],
),
),
);
}
}