forked from aspingley/flutter_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtake_home_assignment1.dart
357 lines (327 loc) · 9.29 KB
/
take_home_assignment1.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
void main() {
runApp(MyApp());
}
class Theme with ChangeNotifier {
static bool _isDark = false;
ThemeMode currentTheme() {
return _isDark ? ThemeMode.dark : ThemeMode.light;
}
void switchTheme() {
_isDark = !_isDark;
notifyListeners();
}
}
Theme currentTheme = Theme();
class MyApp extends StatefulWidget {
@override
_MyApp createState() => _MyApp();
}
class _MyApp extends State<MyApp> {
@override
void initState() {
super.initState();
currentTheme.addListener(() {
print('Changes');
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Startup Name Generator',
darkTheme: ThemeData.dark(),
theme: ThemeData.light(),
themeMode: currentTheme.currentTheme(),
home: RandomWords(),
);
}
}
class RandomWords extends StatefulWidget {
@override
_RandomWordsState createState() => _RandomWordsState();
}
class _RandomWordsState extends State<RandomWords> {
final _suggestions = <WordPair>[];
final _saved = Set<WordPair>(); // NEW
final _biggerFont = TextStyle(fontSize: 18.0);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Startup Name Generator'),
actions: [
IconButton(icon: Icon(Icons.list), onPressed: _pushSaved),
],
),
body: _buildSuggestions(),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
/*floatingActionButton: FloatingActionButton.extended(
onPressed: () {
currentTheme.switchTheme();
},
label: Text('Theme'),
icon: Icon(Icons.brightness_5),
),*/
floatingActionButton: Container(
padding: EdgeInsets.symmetric(vertical: 0, horizontal: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
ElevatedButton(
onPressed: () {
if (_saved.length == 4) {
Navigator.push(
context,
MaterialPageRoute(
//builder: (context) =>
// GridScreen(dataPreviousScreen: _saved)),
builder: (context) =>
StatelessGridScreen(data: _saved)),
);
} else {
return showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text("Warning"),
content: Text("You must select exactly 4 favorites"),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(ctx).pop();
},
child: Text("okay"),
),
],
),
);
}
},
child: Icon(Icons.arrow_forward),
),
FloatingActionButton(
onPressed: () {
currentTheme.switchTheme();
},
child: Icon(Icons.brightness_5),
),
],
),
),
);
}
Widget _buildSuggestions() {
return ListView.builder(
padding: const EdgeInsets.all(16),
itemBuilder: (BuildContext _context, int i) {
if (i.isOdd) {
return Divider();
}
final int index = i ~/ 2;
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10));
}
return _buildRow(_suggestions[index]);
});
}
Widget _buildRow(WordPair pair) {
final alreadySaved = _saved.contains(pair);
return ListTile(
title: Text(
pair.asPascalCase,
style: _biggerFont,
),
trailing: Icon(
alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null,
),
onTap: () {
setState(() {
if (alreadySaved) {
_saved.remove(pair);
} else {
_saved.add(pair);
}
});
},
);
}
void _pushSaved() {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (BuildContext context) {
final tiles = _saved.map(
(WordPair pair) {
return ListTile(
title: Text(
pair.asPascalCase,
style: _biggerFont,
),
);
},
);
final divided = ListTile.divideTiles(
context: context,
tiles: tiles,
).toList();
return Scaffold(
appBar: AppBar(
title: Text('Saved Suggestions'),
),
body: ListView(children: divided),
);
},
),
);
}
}
class StatelessGridScreen extends StatelessWidget {
final Set<WordPair> data;
StatelessGridScreen({Key key, @required this.data}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blueGrey[900],
title: Center(
child: Text(
'Flutter GridView',
style: TextStyle(
color: Colors.blueAccent,
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
),
),
),
body: Column(children: <Widget>[
Row(
//ROW 1
children: [
FlippingCell(
index: 1,
data: data.elementAt(0).asPascalCase,
),
FlippingCell(
index: 2,
data: data.elementAt(1).asPascalCase,
)
//getContainer(0),
//getContainer(1),
],
),
Row(//ROW 2
children: [
//getContainer(2),
//getContainer(3),
FlippingCell(
index: 3,
data: data.elementAt(2).asPascalCase,
),
FlippingCell(
index: 4,
data: data.elementAt(3).asPascalCase,
)
]),
]),
),
);
}
}
class FlippingCell extends StatefulWidget {
final int index;
final String data;
const FlippingCell({Key key, this.index, this.data}) : super(key: key);
@override
_FlippingCellState createState() => _FlippingCellState();
}
class _FlippingCellState extends State<FlippingCell> {
static bool _showFrontSide = true;
@override
Widget build(BuildContext context) {
return getContainer(widget.index);
}
Widget getContainer(int index) {
return GestureDetector(
onTap: () => setState(() => _showFrontSide = !_showFrontSide),
child: Container(
height: MediaQuery.of(context).size.height * 0.25,
width: MediaQuery.of(context).size.width * 0.40,
//color: Colors.blue,
margin: EdgeInsets.all(25.0),
child: AnimatedSwitcher(
duration: Duration(milliseconds: 600),
transitionBuilder: __transitionBuilder,
child:
_showFrontSide ? _buildFront(index + 1) : _buildRear(widget.data),
),
),
);
}
Widget _buildFront(int index) {
return __buildLayoutFront(
key: ValueKey(true),
backgroundColor: Colors.blue,
count: index,
);
}
Widget _buildRear(String name) {
return __buildLayoutRear(
key: ValueKey(false),
backgroundColor: Colors.blue.shade700,
faceName: name,
);
}
Widget __buildLayoutFront({Key key, int count, Color backgroundColor}) {
return Container(
key: key,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://picsum.photos/250?image=' + count.toString()),
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(
Radius.circular(20.0),
),
),
//child: Center(
// child: Text(faceName.substring(0, 1), style: TextStyle(fontSize: 80.0)),
//),
);
}
Widget __buildLayoutRear({Key key, String faceName, Color backgroundColor}) {
return Container(
key: key,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(20.0),
color: backgroundColor,
),
child: Center(
child: Text(faceName, style: TextStyle(fontSize: 25.0)),
),
);
}
Widget __transitionBuilder(Widget widget, Animation<double> animation) {
final rotateAnim = Tween(begin: pi, end: 0.0).animate(animation);
return AnimatedBuilder(
animation: rotateAnim,
child: widget,
builder: (context, widget) {
final isUnder = (ValueKey(_showFrontSide) != widget.key);
final value =
isUnder ? min(rotateAnim.value, pi / 2) : rotateAnim.value;
return Transform(
transform: Matrix4.rotationY(value),
child: widget,
alignment: Alignment.center,
);
},
);
}
}