|
@@ -0,0 +1,129 @@
|
|
|
|
|
+import 'dart:async';
|
|
|
|
|
+
|
|
|
|
|
+import 'package:flutter/material.dart';
|
|
|
|
|
+import 'package:flutter_activity_recognition/flutter_activity_recognition.dart';
|
|
|
|
|
+import 'package:geolocator/geolocator.dart';
|
|
|
|
|
+
|
|
|
|
|
+class WalkingPage extends StatefulWidget {
|
|
|
|
|
+ WalkingPage({Key? key}) : super(key: key);
|
|
|
|
|
+
|
|
|
|
|
+ @override
|
|
|
|
|
+ State<WalkingPage> createState() => _WalkingPageState();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+class _WalkingPageState extends State<WalkingPage> {
|
|
|
|
|
+ final FlutterActivityRecognition activityRecognition = FlutterActivityRecognition.instance;
|
|
|
|
|
+ final StreamController<Position> _streamController = StreamController.broadcast();
|
|
|
|
|
+ num distance = 0;
|
|
|
|
|
+ Position? lastPosition;
|
|
|
|
|
+ @override
|
|
|
|
|
+ void dispose() {
|
|
|
|
|
+ _streamController.close();
|
|
|
|
|
+ super.dispose();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @override
|
|
|
|
|
+ Widget build(BuildContext context) {
|
|
|
|
|
+ Geolocator.getPositionStream(locationSettings: LocationSettings(distanceFilter: 1)).listen((position) {
|
|
|
|
|
+ _streamController.add(position);
|
|
|
|
|
+ print(position);
|
|
|
|
|
+ });
|
|
|
|
|
+ return Scaffold(
|
|
|
|
|
+ body: Center(
|
|
|
|
|
+ child: Column(
|
|
|
|
|
+ mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
+ children: [
|
|
|
|
|
+ FutureBuilder<bool>(
|
|
|
|
|
+ future: isPermissionGrants(),
|
|
|
|
|
+ builder: (context, snapshot) {
|
|
|
|
|
+ if (snapshot.hasData && snapshot.data!) {
|
|
|
|
|
+ return StreamBuilder<Activity>(
|
|
|
|
|
+ stream: activityRecognition.activityStream,
|
|
|
|
|
+ builder: (context, snapshot) {
|
|
|
|
|
+ if (snapshot.hasData) {
|
|
|
|
|
+ return Text("${snapshot.data!.type}, ${snapshot.data!.confidence}");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return CircularProgressIndicator();
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ );
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return CircularProgressIndicator();
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ ),
|
|
|
|
|
+ StreamBuilder<Position>(
|
|
|
|
|
+ stream: _streamController.stream,
|
|
|
|
|
+ builder: (context, snapshot) {
|
|
|
|
|
+ if (snapshot.hasData) {
|
|
|
|
|
+ final position = snapshot.data!;
|
|
|
|
|
+ final latitude = position.latitude;
|
|
|
|
|
+ final longitude = position.longitude;
|
|
|
|
|
+ final currentActivity = activityRecognition.activityStream.last;
|
|
|
|
|
+ if (lastPosition != null) {
|
|
|
|
|
+ distance += Geolocator.distanceBetween(
|
|
|
|
|
+ lastPosition!.latitude, lastPosition!.longitude, position.latitude, position.longitude);
|
|
|
|
|
+ }
|
|
|
|
|
+ lastPosition = position;
|
|
|
|
|
+ return Text("${latitude}, ${longitude}, DISTANCE: ${distance.round()}");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return CircularProgressIndicator();
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ],
|
|
|
|
|
+ ),
|
|
|
|
|
+ ),
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Future<bool> isPermissionGrants() async {
|
|
|
|
|
+ // Check if the user has granted permission. If not, request permission.
|
|
|
|
|
+ PermissionRequestResult reqResult;
|
|
|
|
|
+ reqResult = await activityRecognition.checkPermission();
|
|
|
|
|
+ if (reqResult == PermissionRequestResult.PERMANENTLY_DENIED) {
|
|
|
|
|
+ print('Permission is permanently denied.');
|
|
|
|
|
+ return false;
|
|
|
|
|
+ } else if (reqResult == PermissionRequestResult.DENIED) {
|
|
|
|
|
+ reqResult = await activityRecognition.requestPermission();
|
|
|
|
|
+ if (reqResult != PermissionRequestResult.GRANTED) {
|
|
|
|
|
+ print('Permission is denied.');
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Future<void> _getPermissions() async {
|
|
|
|
|
+ bool serviceEnabled;
|
|
|
|
|
+ LocationPermission permission;
|
|
|
|
|
+
|
|
|
|
|
+ // Test if location services are enabled.
|
|
|
|
|
+ serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
|
|
|
|
+ if (!serviceEnabled) {
|
|
|
|
|
+ // Location services are not enabled don't continue
|
|
|
|
|
+ // accessing the position and request users of the
|
|
|
|
|
+ // App to enable the location services.
|
|
|
|
|
+ return Future.error('Location services are disabled.');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ permission = await Geolocator.checkPermission();
|
|
|
|
|
+ if (permission == LocationPermission.denied) {
|
|
|
|
|
+ permission = await Geolocator.requestPermission();
|
|
|
|
|
+ if (permission == LocationPermission.denied) {
|
|
|
|
|
+ // Permissions are denied, next time you could try
|
|
|
|
|
+ // requesting permissions again (this is also where
|
|
|
|
|
+ // Android's shouldShowRequestPermissionRationale
|
|
|
|
|
+ // returned true. According to Android guidelines
|
|
|
|
|
+ // your App should show an explanatory UI now.
|
|
|
|
|
+ return Future.error('Location permissions are denied');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (permission == LocationPermission.deniedForever) {
|
|
|
|
|
+ // Permissions are denied forever, handle appropriately.
|
|
|
|
|
+ return Future.error('Location permissions are permanently denied, we cannot request permissions.');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|