ELEC60013-ES-CW1/lib/view/steps/steps_page.dart

55 lines
2 KiB
Dart
Raw Normal View History

import 'package:firebase_auth/firebase_auth.dart';
2022-02-09 17:52:00 +00:00
import 'package:flutter/material.dart';
import 'package:leg_barkr_app/model/steps_series.dart';
2022-02-14 20:19:04 +00:00
import 'package:leg_barkr_app/service/steps_service.dart';
2022-02-09 17:52:00 +00:00
import 'package:leg_barkr_app/view/steps/steps_chart.dart';
2022-02-09 18:29:27 +00:00
import 'package:leg_barkr_app/view/steps/steps_today.dart';
import 'package:shared_preferences/shared_preferences.dart';
2022-02-09 17:52:00 +00:00
class StepsPage extends StatefulWidget {
const StepsPage({ Key? key }) : super(key: key);
@override
_StepsPageState createState() => _StepsPageState();
}
class _StepsPageState extends State<StepsPage> {
2022-02-14 23:28:27 +00:00
Future<List<int>> onStepsRetrieved() async{
final prefs = await SharedPreferences.getInstance();
final String deviceId = prefs.getString("current_device") ?? "";
final user = await FirebaseAuth.instance.currentUser!;
final String token = await user.getIdToken();
return await StepsService().getStepsLastFiveDays(deviceId, token);
2022-02-14 20:19:04 +00:00
}
2022-02-09 17:52:00 +00:00
@override
Widget build(BuildContext context) {
2022-02-09 18:29:27 +00:00
return Padding(
padding: EdgeInsets.fromLTRB(0.0, 50.0, 10.0, 0.0),
2022-02-14 20:19:04 +00:00
child: FutureBuilder(
future: onStepsRetrieved(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
2022-02-14 23:28:27 +00:00
int stepsToday = 0;
List<StepsSeries> stepsSeries = [];
if(snapshot.hasData) {
List<int> stepsLastFiveDays = snapshot.data;
stepsToday = stepsLastFiveDays[0];
for(int i = 0; i < stepsLastFiveDays.length; i++){
DateTime now = DateTime.now();
stepsSeries.add(StepsSeries(DateTime(now.year, now.month, now.day-i), stepsLastFiveDays[i]));
}
stepsSeries = List.from(stepsSeries.reversed);
}
return Column(
2022-02-14 20:19:04 +00:00
crossAxisAlignment: CrossAxisAlignment.center,
children: [
2022-02-14 23:28:27 +00:00
StepsToday(stepsToday),
new Expanded(child: StepsChart(stepsSeries))
2022-02-14 20:19:04 +00:00
],
);
},
2022-02-09 18:29:27 +00:00
)
2022-02-09 17:52:00 +00:00
);
}
}