From c93438411499ee3d97d06571b9a78a51bf91a030 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 4 Jan 2024 13:48:31 +0100 Subject: [PATCH] gps --- emma/android/build.gradle | 4 +- emma/ios/Runner/Info.plist | 2 + emma/lib/main.dart | 357 ++++++++++++++---- .../Flutter/GeneratedPluginRegistrant.swift | 2 + emma/pubspec.lock | 62 +++ emma/pubspec.yaml | 1 + 6 files changed, 348 insertions(+), 80 deletions(-) diff --git a/emma/android/build.gradle b/emma/android/build.gradle index e83fb5d..d6ba8cb 100644 --- a/emma/android/build.gradle +++ b/emma/android/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.7.10' + ext.kotlin_version = '1.9.0' repositories { google() mavenCentral() @@ -9,7 +9,7 @@ buildscript { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } - +//$kotlin_version" allprojects { repositories { google() diff --git a/emma/ios/Runner/Info.plist b/emma/ios/Runner/Info.plist index 9af3bb6..593945e 100644 --- a/emma/ios/Runner/Info.plist +++ b/emma/ios/Runner/Info.plist @@ -2,6 +2,8 @@ + NSLocationWhenInUseUsageDescription + Some description CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleDisplayName diff --git a/emma/lib/main.dart b/emma/lib/main.dart index d48529a..a77b430 100644 --- a/emma/lib/main.dart +++ b/emma/lib/main.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:location/location.dart'; import 'dart:async'; + void main() => runApp(const MyApp()); const Duration fakeAPIDuration = Duration(milliseconds: 50); const Duration debounceDuration = Duration(milliseconds: 50); @@ -76,8 +78,41 @@ class _MyFormPageState extends State { super.initState(); } + late bool _serviceEnabled; + late PermissionStatus _permissionGranted; + + LocationData? _userLocation; + + // This function will get user location + Future _getUserLocation() async { + Location location = Location(); + + // Check if location service is enable + _serviceEnabled = await location.serviceEnabled(); + if (!_serviceEnabled) { + _serviceEnabled = await location.requestService(); + if (!_serviceEnabled) { + return; + } + } + + // Check if permission is granted + _permissionGranted = await location.hasPermission(); + if (_permissionGranted == PermissionStatus.denied) { + _permissionGranted = await location.requestPermission(); + if (_permissionGranted != PermissionStatus.granted) { + return; + } + } + + final locationData = await location.getLocation(); + setState(() { + _userLocation = locationData; + }); + } + final _formKey = GlobalKey(); - double _currentSliderValue = 20; + double _currentSliderValue = 1; @override Widget build(BuildContext context) { return Scaffold( @@ -107,22 +142,39 @@ class _MyFormPageState extends State { }, ),*/ const SizedBox(height: 20), - const _AsyncAutocomplete(), - TextFormField( - keyboardType: TextInputType.emailAddress, - decoration: const InputDecoration( - labelText: 'GPS: Hier bin ich', - border: OutlineInputBorder(), - ), - ), + const _AsyncAutocomplete(), + Text("oder"), const SizedBox(height: 20), - TextFormField( + ElevatedButton( + style: ElevatedButton.styleFrom( + elevation: 5, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10.0), + ), + ), + onPressed: _getUserLocation, + child: const Text('GPS :Hier bin ich'), + ), + const SizedBox(height: 25), + // Display latitude & longtitude + _userLocation != null + ? Wrap( + children: [ + Text('Your latitude: ${_userLocation?.latitude}'), + const SizedBox(width: 10), + Text('Your longtitude: ${_userLocation?.longitude}') + ], + ) + : const Text( + 'Please enable location service and grant permission'), + const SizedBox(height: 20), + /*TextFormField( obscureText: true, decoration: const InputDecoration( labelText: 'Punkt auf der Karte', border: OutlineInputBorder(), ), - ), + ),*/ const SizedBox(height: 20), Text( "Umkreis von " + @@ -132,7 +184,8 @@ class _MyFormPageState extends State { ), Slider( value: _currentSliderValue, - max: 100, + min: 1, + max: 10, divisions: 5, label: _currentSliderValue.round().toString() + " km", onChanged: (double value) { @@ -151,7 +204,7 @@ class _MyFormPageState extends State { // reset() setzt alle Felder wieder auf den Initalwert zurück. _formKey.currentState?.reset(); }, - child: const Text('Löschen'), + child: const Text('Zurücksetzen'), ), const SizedBox(width: 25), ElevatedButton( @@ -168,10 +221,10 @@ class _MyFormPageState extends State { } Navigator.push(context, MaterialPageRoute(builder: (context) { - return TypePage(title: 'SecondPage'); + return TypePage(title: 'Type'); })); }, - child: const Text('Next'), + child: const Text('Weiter'), ) ], ) @@ -182,13 +235,13 @@ class _MyFormPageState extends State { )); } - String zahlValidator(value) { + /* String zahlValidator(value) { var zahl = int.tryParse(value.toString()) ?? 0; if (zahl % 2 == 0) { return 'Es sind nur ungerade Zahlen erlaubt'; } return ''; - } + }*/ } class TypePage extends StatelessWidget { @@ -335,13 +388,52 @@ class TypePage extends StatelessWidget { ], ), ), + Expanded( + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.grey, + textStyle: const TextStyle(color: Colors.white)), + onPressed: () { + // reset() setzt alle Felder wieder auf den Initalwert zurück. + }, + child: const Text('Zurücksetzen'), + ), + const SizedBox(width: 25), + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.grey, + textStyle: const TextStyle(color: Colors.white)), + onPressed: () { + // reset() setzt alle Felder wieder auf den Initalwert zurück. + }, + child: const Text('Skip'), + ), + const SizedBox(width: 25), + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.blue, + textStyle: const TextStyle(color: Colors.white)), + onPressed: () { + // Wenn alle Validatoren der Felder des Formulars gültig sind. + Navigator.push(context, MaterialPageRoute(builder: (context) { + return CategoriePage(); + })); + }, + child: const Text('Weiter'), + ) + ], + )), ]), ), ); } } -/*class TypePage extends StatelessWidget { - TypePage({Key? key, required this.title}) : super(key: key); + +class TypePage2 extends StatelessWidget { + TypePage2({Key? key, required this.title}) : super(key: key); final String title; final List myProducts = List.generate(100000, (index) => {"id": index, "name": "Product $index"}) @@ -379,7 +471,7 @@ class TypePage extends StatelessWidget { ), ); } -}*/ +} class CategoriePage extends StatelessWidget { @override @@ -389,75 +481,184 @@ class CategoriePage extends StatelessWidget { title: Text("Kategorie"), ), body: Center( - child: Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - Expanded( - child: Row( - children: [ - Expanded( - child: ElevatedButton( - onPressed: () {}, - child: Text('Button 1'), + child: + Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [ + Expanded( + child: Row( + children: [ + SizedBox( + height: 100, //height of button + width: (MediaQuery.of(context).size.width - 30) / + 3, //width of button + child: ElevatedButton( + style: ElevatedButton.styleFrom( + elevation: 5, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10.0), + ), ), + child: Text('Frühstück'), + onPressed: () { + Navigator.push(context, + MaterialPageRoute(builder: (context) { + return CategoriePage(); + })); + }, ), - Expanded( - child: Text( - 'Lorem ipsum dolor sit amet', - textAlign: TextAlign.center, + ), + SizedBox( + height: 100, //height of button + width: (MediaQuery.of(context).size.width - 30) / + 3, //width of button + child: ElevatedButton( + style: ElevatedButton.styleFrom( + elevation: 5, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10.0), + ), ), - ), - ], - ), - ), - Expanded( - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - ElevatedButton( - onPressed: () {}, - child: Text('Button 1'), - ), - ElevatedButton( - onPressed: () {}, - child: Text('Button 2'), - ), - TextButton( + child: Text('Mittag'), onPressed: () { - Navigator.pop(context); + Navigator.push(context, + MaterialPageRoute(builder: (context) { + return CategoriePage(); + })); }, - child: const Text('Go Back'), ), - ], - ), - ), - Expanded( - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - ElevatedButton( - onPressed: () {}, - child: Text('Button 1'), - ), - ElevatedButton( - onPressed: () {}, - child: Text('Button 2'), - ), - TextButton( + ), + SizedBox( + height: 100, //height of button + width: (MediaQuery.of(context).size.width - 30) / + 3, //width of button + child: ElevatedButton( + style: ElevatedButton.styleFrom( + elevation: 5, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10.0), + ), + ), + child: Text('Abendessen'), onPressed: () { - Navigator.pop(context); + Navigator.push(context, + MaterialPageRoute(builder: (context) { + return CategoriePage(); + })); }, - child: const Text('Go Back'), ), - ], - ), + ), + ], ), - ], - ), + ), + Expanded( + child: Row( + children: [ + SizedBox( + height: 100, //height of button + width: (MediaQuery.of(context).size.width - 30) / + 3, //width of button + child: ElevatedButton( + style: ElevatedButton.styleFrom( + elevation: 5, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10.0), + ), + ), + child: Text('Indisch'), + onPressed: () { + Navigator.push(context, + MaterialPageRoute(builder: (context) { + return CategoriePage(); + })); + }, + ), + ), + SizedBox( + height: 100, //height of button + width: (MediaQuery.of(context).size.width - 30) / + 3, //width of button + child: ElevatedButton( + style: ElevatedButton.styleFrom( + elevation: 5, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10.0), + ), + ), + child: Text('Bayrisch'), + onPressed: () { + Navigator.push(context, + MaterialPageRoute(builder: (context) { + return CategoriePage(); + })); + }, + ), + ), + SizedBox( + height: 100, //height of button + width: (MediaQuery.of(context).size.width - 30) / + 3, //width of button + child: ElevatedButton( + style: ElevatedButton.styleFrom( + elevation: 5, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10.0), + ), + ), + child: Text('Snack'), + onPressed: () { + Navigator.push(context, + MaterialPageRoute(builder: (context) { + return CategoriePage(); + })); + }, + ), + ), + ], + ), + ), + Expanded( + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.grey, + textStyle: const TextStyle(color: Colors.white)), + onPressed: () { + // reset() setzt alle Felder wieder auf den Initalwert zurück. + }, + child: const Text('Zurücksetzen'), + ), + const SizedBox(width: 25), + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.grey, + textStyle: const TextStyle(color: Colors.white)), + onPressed: () { + // reset() setzt alle Felder wieder auf den Initalwert zurück. + }, + child: const Text('Skip'), + ), + const SizedBox(width: 25), + ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: Colors.blue, + textStyle: const TextStyle(color: Colors.white)), + onPressed: () { + // Wenn alle Validatoren der Felder des Formulars gültig sind. + Navigator.push(context, MaterialPageRoute(builder: (context) { + return CategoriePage(); + })); + }, + child: const Text('Weiter'), + ) + ], + )), + ]), ), ); } } + class _AsyncAutocomplete extends StatefulWidget { const _AsyncAutocomplete(); @@ -519,7 +720,7 @@ class _AsyncAutocompleteState extends State<_AsyncAutocomplete> { return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - Text( + /*Text( _networkEnabled ? 'Network is on, toggle to induce network errors.' : 'Network is off, toggle to allow requests to go through.', @@ -531,7 +732,7 @@ class _AsyncAutocompleteState extends State<_AsyncAutocomplete> { _networkEnabled = !_networkEnabled; }); }, - ), + ),*/ const SizedBox( height: 32.0, ), @@ -542,7 +743,7 @@ class _AsyncAutocompleteState extends State<_AsyncAutocomplete> { VoidCallback onFieldSubmitted) { return TextFormField( decoration: InputDecoration( - labelText: 'Wohin solls gehen?', + labelText: 'Wohin solls gehen? (Ort, Location)', border: OutlineInputBorder(), errorText: _networkError ? 'Network error, please try again.' : null, diff --git a/emma/macos/Flutter/GeneratedPluginRegistrant.swift b/emma/macos/Flutter/GeneratedPluginRegistrant.swift index cccf817..6b401e5 100644 --- a/emma/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/emma/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,8 @@ import FlutterMacOS import Foundation +import location func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + LocationPlugin.register(with: registry.registrar(forPlugin: "LocationPlugin")) } diff --git a/emma/pubspec.lock b/emma/pubspec.lock index 4de467f..1593f86 100644 --- a/emma/pubspec.lock +++ b/emma/pubspec.lock @@ -75,6 +75,27 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + url: "https://pub.dev" + source: hosted + version: "4.0.2" + js: + dependency: transitive + description: + name: js + sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 + url: "https://pub.dev" + source: hosted + version: "0.6.7" lints: dependency: transitive description: @@ -83,6 +104,30 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.1" + location: + dependency: "direct main" + description: + name: location + sha256: "06be54f682c9073cbfec3899eb9bc8ed90faa0e17735c9d9fa7fe426f5be1dd1" + url: "https://pub.dev" + source: hosted + version: "5.0.3" + location_platform_interface: + dependency: transitive + description: + name: location_platform_interface + sha256: "8aa1d34eeecc979d7c9fe372931d84f6d2ebbd52226a54fe1620de6fdc0753b1" + url: "https://pub.dev" + source: hosted + version: "3.1.2" + location_web: + dependency: transitive + description: + name: location_web + sha256: ec484c66e8a4ff1ee5d044c203f4b6b71e3a0556a97b739a5bc9616de672412b + url: "https://pub.dev" + source: hosted + version: "4.2.0" matcher: dependency: transitive description: @@ -115,6 +160,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.3" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + sha256: f4f88d4a900933e7267e2b353594774fc0d07fb072b47eedcd5b54e1ea3269f8 + url: "https://pub.dev" + source: hosted + version: "2.1.7" sky_engine: dependency: transitive description: flutter @@ -168,6 +221,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.1" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + url: "https://pub.dev" + source: hosted + version: "1.3.2" vector_math: dependency: transitive description: @@ -186,3 +247,4 @@ packages: version: "0.3.0" sdks: dart: ">=3.2.3 <4.0.0" + flutter: ">=3.3.0" diff --git a/emma/pubspec.yaml b/emma/pubspec.yaml index e22c7d2..af40667 100644 --- a/emma/pubspec.yaml +++ b/emma/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 + location: ^5.0.3 dev_dependencies: flutter_test: