-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.dart
394 lines (342 loc) · 10.6 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(HumanLifeApp());
}
class HumanLifeApp extends StatelessWidget {
final settings = HumanLifeSettings();
static const appName = 'Human Life Counter';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appName,
theme: ThemeData(
primaryColor: Color(0xFF38618C),
visualDensity: VisualDensity.adaptivePlatformDensity,
bottomSheetTheme: BottomSheetThemeData(
backgroundColor: Color(0xFFC0FF99),
),
),
home: HumanLifePage(settings: settings),
);
}
}
class HumanLifePage extends StatelessWidget {
final HumanLifeSettings settings;
const HumanLifePage({required this.settings});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
if (constraints.maxWidth < 600) {
// Narrow: use "mobile" UI.
return Scaffold(
appBar: AppBar(
title: Text(HumanLifeApp.appName),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(flex: 17, child: HumanLifeVisualization(settings)),
// Make sure there's enough space at the bottom that the bottom sheet
// can cover.
Expanded(flex: 3, child: SizedBox()),
],
),
bottomSheet: CustomizationDrawer(settings),
);
}
return Scaffold(
appBar: AppBar(
title: Text(HumanLifeApp.appName),
),
body: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 3, child: CustomizationListView(settings: settings)),
Expanded(flex: 7, child: HumanLifeVisualization(settings)),
],
),
);
},
);
}
}
class HumanLifeVisualization extends StatelessWidget {
final HumanLifeSettings settings;
const HumanLifeVisualization(this.settings);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16),
child: AnimatedBuilder(
animation: settings,
builder: (context, child) {
return RepaintBoundary(
child: CustomPaint(
painter: HumanLifePainter(
settings.currentAge, settings.maxAge, settings.maxActiveAge),
),
);
},
),
);
}
}
class HumanLifePainter extends CustomPainter {
final int currentAge, maxAge, maxActiveAge;
const HumanLifePainter(this.currentAge, this.maxAge, this.maxActiveAge);
/// Weeks per year, counting leap years (because why not).
/// https://en.wikipedia.org/wiki/ISO_week_date
///
/// Otherwise, this is just 365/7.
static const weeksPerYear = 52.1775;
@override
void paint(Canvas canvas, Size size) {
final weeks = maxAge * weeksPerYear;
const minimumSquareArea = 3 * 3;
const maximumSquareArea = 50 * 50;
const minColumns = 1;
const maxColumns = 1000;
int bestColumns = -1;
int bestRows = -1;
for (int columns = minColumns; columns < maxColumns; columns++) {
final sqArea = (size.height * size.height) / (columns * columns);
if (minimumSquareArea < sqArea && sqArea < maximumSquareArea) {
final rows = (weeks / columns).ceil();
// Trying to find the closest aspect ratio to the one of the viewport.
final distance = (columns / rows - size.aspectRatio).abs();
final prevDistance = (bestColumns / bestRows - size.aspectRatio).abs();
if (bestColumns != -1 && prevDistance < distance) {
// We've already seen the best ratio. It was the last one.
break;
}
bestColumns = columns;
bestRows = rows;
}
}
if (bestRows == -1 || bestColumns == -1) {
// Escape chute.
bestRows = sqrt(weeks).ceil();
bestColumns = (weeks / bestRows).ceil();
}
final sqSize = (size.height / bestRows).floorToDouble();
final width = sqSize * bestColumns;
final leftPadding = (size.width - width) / 2;
bool currentShownYet = false;
int week = 0;
for (int row = 0; row < bestRows; row++) {
for (int column = 0; column < bestColumns; column++) {
if (week < weeks) {
final age = week / weeksPerYear;
var paint = (age <= currentAge)
? lived
: (age <= maxActiveAge ? active : elderly);
if (paint == active && !currentShownYet) {
// The first square in current life.
paint = current;
currentShownYet = true;
}
final rect = Rect.fromLTWH(leftPadding + column * sqSize,
row * sqSize, sqSize - 1, sqSize - 1);
canvas.drawRect(rect, paint);
}
week++;
}
}
}
static final Paint lived = Paint()..color = Color(0xFFB4C5E4);
static final Paint current = Paint()..color = Colors.white;
static final Paint active = Paint()..color = Color(0xFF63C132);
static final Paint elderly = Paint()..color = Color(0xFF358600);
@override
bool shouldRepaint(HumanLifePainter old) {
return currentAge != old.currentAge ||
maxAge != old.maxAge ||
maxActiveAge != old.maxActiveAge;
}
}
class CustomizationDrawer extends StatelessWidget {
final HumanLifeSettings settings;
CustomizationDrawer(this.settings);
@override
Widget build(BuildContext context) {
return DraggableScrollableSheet(
minChildSize: 0.15,
initialChildSize: 0.15,
maxChildSize: 0.7,
expand: false,
builder: (context, scrollController) {
return Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
blurRadius: 0,
),
],
color: Theme.of(context).bottomSheetTheme.backgroundColor,
),
child: SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 16, right: 16),
child: CustomizationListView(
settings: settings,
scrollController: scrollController,
showPullUpHint: true,
),
),
),
);
},
);
}
}
class CustomizationListView extends StatelessWidget {
const CustomizationListView({
required this.settings,
this.scrollController,
this.showPullUpHint = false,
Key? key,
}) : super(key: key);
final HumanLifeSettings settings;
final ScrollController? scrollController;
final bool showPullUpHint;
@override
Widget build(BuildContext context) {
return ListView(
controller: scrollController,
padding: const EdgeInsets.only(left: 16),
children: [
if (showPullUpHint) SizedBox(height: 32),
if (showPullUpHint)
Row(
children: [
Text(
'Pull up to customize ',
style: Theme.of(context).textTheme.headline6,
),
Icon(Icons.arrow_upward_rounded),
],
),
SizedBox(height: 16),
Text("What's this about? The app visualizes a human life "
"as a series of squares. Each square represents "
"a single week of life.\n\n"
"Light blue squares are already in the past. "
"Light green squares are upcoming active life. "
"Dark green squares are the weeks after that.\n\n"
"How you define ‘active life’ is up to you.\n\n"
"Try to look past the depressing part and instead "
"think about this as motivational.\n\n"
"Inspired by the article ‘Tail End’ "
"at Wait But Why."),
SizedBox(height: 32),
HumanLifeCustomization(settings),
SizedBox(height: 32),
],
);
}
}
class HumanLifeCustomization extends StatefulWidget {
final HumanLifeSettings settings;
const HumanLifeCustomization(this.settings);
@override
_HumanLifeCustomizationState createState() => _HumanLifeCustomizationState();
}
class _HumanLifeCustomizationState extends State<HumanLifeCustomization> {
@override
Widget build(BuildContext context) {
return Column(
children: [
AgeSlider(
label: 'Current age',
initialValue: widget.settings.currentAge,
onChanged: (value) => widget.settings.currentAge = value,
),
AgeSlider(
label: 'Active life expectancy',
initialValue: widget.settings.maxActiveAge,
onChanged: (value) => widget.settings.maxActiveAge = value,
),
AgeSlider(
label: 'Life expectancy',
initialValue: widget.settings.maxAge,
onChanged: (value) => widget.settings.maxAge = value,
),
],
);
}
}
class AgeSlider extends StatefulWidget {
final int initialValue;
final int maxValue = 100;
final String label;
final void Function(int) onChanged;
const AgeSlider({
required this.label,
required this.initialValue,
required this.onChanged,
Key? key,
}) : super(key: key);
@override
_AgeSliderState createState() => _AgeSliderState();
}
class _AgeSliderState extends State<AgeSlider> {
@override
void initState() {
super.initState();
_value = widget.initialValue.toDouble();
_outValue = _value.round();
}
double _value = -1;
int _outValue = -1;
@override
Widget build(BuildContext context) {
return Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: Text(
'${widget.label}: $_outValue years',
style: Theme.of(context).textTheme.headline6,
),
),
Slider(
value: _value,
max: widget.maxValue.toDouble(),
divisions: widget.maxValue,
onChanged: (value) {
setState(() {
_value = value;
});
if (_outValue != _value.round()) {
_outValue = _value.round();
widget.onChanged(_outValue);
}
},
),
],
);
}
}
class HumanLifeSettings extends ChangeNotifier {
int _currentAge = 18;
int get currentAge => _currentAge;
set currentAge(int value) {
_currentAge = value;
notifyListeners();
}
int _maxAge = 79;
int get maxAge => _maxAge;
set maxAge(int value) {
_maxAge = value;
notifyListeners();
}
int _maxActiveAge = 65;
int get maxActiveAge => _maxActiveAge;
set maxActiveAge(int value) {
_maxActiveAge = value;
notifyListeners();
}
}