import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; import 'package:http/http.dart' as http; import 'colors.dart'; import 'customBottomAppBar.dart'; import 'customMoment.dart'; import 'generated/i18n.dart'; class GlobalFiresBottomStats extends StatefulWidget { @override _GlobalFiresBottomStatsState createState() => _GlobalFiresBottomStatsState(); } class _GlobalFiresBottomStatsState extends State { late String lastCheck; int activeFires = 0; final firesApiUrl = GetIt.instance(instanceName: "firesApiUrl"); @override void initState() { super.initState(); http.read(Uri.parse('${firesApiUrl}status/last-fire-check')).then((result) { try { var now = Moment.now(); var last = DateTime.parse(json.decode(result)['value']); http .read(Uri.parse('${firesApiUrl}status/active-fires-count')) .then((result) { try { int count = json.decode(result)['total']; setState(() { lastCheck = now.from(context, last); activeFires = count; }); } catch (e) { print('Cannot get the last fire stats'); print(e); } }); } catch (e) { print('Cannot get the last fire check'); print(e); } }); } @override Widget build(BuildContext context) { final List actionWidgets = []; if (activeFires > 0) { actionWidgets.add(new Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, children: [ new Text( S.of(context).activeFiresWorldWide(activeFires.toString())), new Text(S.of(context).updatedLastCheck(lastCheck)) ])); } return new CustomBottomAppBar( fabLocation: FloatingActionButtonLocation.centerDocked, showNotch: true, color: fires100, mainAxisAlignment: MainAxisAlignment.center, actions: actionWidgets); } }