From 40842dc4e42829fd008f4d43cd4f2c4babf30179 Mon Sep 17 00:00:00 2001 From: vjrj Date: Sun, 10 Jun 2018 21:12:54 +0200 Subject: [PATCH] Polish and refactor --- lib/globalFiresBottomStats.dart | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lib/globalFiresBottomStats.dart diff --git a/lib/globalFiresBottomStats.dart b/lib/globalFiresBottomStats.dart new file mode 100644 index 0000000..1e03170 --- /dev/null +++ b/lib/globalFiresBottomStats.dart @@ -0,0 +1,65 @@ +import 'package:flutter/material.dart'; +import 'package:simple_moment/simple_moment.dart'; +import 'dart:convert'; +import 'globals.dart' as globals; +import 'package:http/http.dart' as http; +import 'package:comunes_flutter/comunes_flutter.dart'; +import 'customBottomAppBar.dart'; +import 'colors.dart'; + +class GlobalFiresBottomStats extends StatefulWidget { + @override + _GlobalFiresBottomStatsState createState() => _GlobalFiresBottomStatsState(); +} + +class _GlobalFiresBottomStatsState extends State { + String lastCheck; + int activeFires = 0; + + @override + void initState() { + super.initState(); + http.read('${globals.firesApiUrl}status/last-fire-check').then((result) { + try { + var now = Moment.now(); + var last = DateTime.parse(json.decode(result)['value']); + setState(() { + lastCheck = now.from(last); + }); + } catch (e) { + print('Cannot get the last fire check'); + } + }); + http.read('${globals.firesApiUrl}status/active-fires-count').then((result) { + try { + int count = json.decode(result)['total']; + setState(() { + activeFires = count; + }); + } catch (e) { + print('Cannot get the last fire stats'); + } + }); + } + + @override + Widget build(BuildContext context) { + return new CustomBottomAppBar( + fabLocation: FloatingActionButtonLocation.centerDocked, + showNotch: true, + color: fires100, + mainAxisAlignment: MainAxisAlignment.center, + actions: listWithoutNulls([ + activeFires > 0 && lastCheck != null + ? new Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + new Text('${activeFires} active fires worldwide'), + new Text('Updated ${lastCheck}') + ]) + : null, + SizedBox(width: 10.0) + ])); + } +}