From 94b781173ff534c225f25a113856d2e332922da9 Mon Sep 17 00:00:00 2001 From: alper-savas <alpersavas1998@gmail.com> Date: Tue, 4 Apr 2023 12:30:05 +0200 Subject: [PATCH] Add config, delete unnecessary code --- lib/config/config.dart | 7 +- lib/main.dart | 611 ++++++++++++++--------------- lib/widgets/directions.dart | 48 --- lib/widgets/directionsRepo.dart | 28 -- lib/widgets/getStreetNumber.dart | 2 +- lib/widgets/returnDestination.dart | 2 +- lib/widgets/returnOrigin.dart | 2 +- 7 files changed, 300 insertions(+), 400 deletions(-) delete mode 100644 lib/widgets/directions.dart delete mode 100644 lib/widgets/directionsRepo.dart diff --git a/lib/config/config.dart b/lib/config/config.dart index 5c7118b..5b45658 100644 --- a/lib/config/config.dart +++ b/lib/config/config.dart @@ -1,5 +1,6 @@ class Config { - final String GOOGLE_API = ''; - final String WEATHER_API = ''; - final String OSM_API = ''; + final String GOOGLE_API = 'AIzaSyAzedQacDEZmyxAOUEEeocvehT8MEdMWys'; + final String WEATHER_API = '07cd339d66d846c7b33192823232003'; + final String OSM_API = + '5b3ce3597851110001cf6248c6ba0beaf1a7480ba9f460646ae0c47b'; } diff --git a/lib/main.dart b/lib/main.dart index 78136d0..67e7e4b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,4 @@ -// ignore_for_file: prefer_const_constructors, sort_child_properties_last, import_of_legacy_library_into_null_safe, prefer_is_not_empty, unnecessary_null_comparison, deprecated_member_use, depend_on_referenced_packages, avoid_function_literals_in_foreach_calls, prefer_final_fields, prefer_typing_uninitialized_variables +// ignore_for_file: prefer_const_constructors, sort_child_properties_last, import_of_legacy_library_into_null_safe, prefer_is_not_empty, unnecessary_null_comparison, deprecated_member_use, depend_on_referenced_packages, avoid_function_literals_in_foreach_calls, prefer_final_fields, prefer_typing_uninitialized_variables, use_build_context_synchronously import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:intl/intl.dart'; @@ -13,9 +13,6 @@ import 'dart:convert'; import 'package:weather_icons/weather_icons.dart'; import 'config/config.dart'; import 'package:latlong/latlong.dart' as latlng; -// import 'widgets/directionsRepo.dart'; -// import 'package:google_maps_flutter/google_maps_flutter.dart'; -// import 'widgets/directions.dart'; void main() => runApp(MyApp()); @@ -56,9 +53,6 @@ class _MyHomePageState extends State<MyHomePage> { Config config = Config(); // Initial map coords and map controller to control camera movements. late final MapController _mapController = MapController(); - // static const _initialCameraPosition = - // CameraPosition(target: LatLng(52.5163, 13.3777), zoom: 12); - // late GoogleMapController _mapController; // Input field texts for origin/dest points. String originText = 'Starting Point...'; @@ -71,8 +65,6 @@ class _MyHomePageState extends State<MyHomePage> { Set<Marker> _markers = {}; var _data; List<latlng.LatLng> _polyline = <latlng.LatLng>[]; - // late Directions _info; - // Set<Polyline> _polyline = {}; String _totalDuration = ''; String _totalDistance = ''; String _totalDaysFormatted = ''; @@ -233,6 +225,7 @@ class _MyHomePageState extends State<MyHomePage> { // Get coords between origin/dest points corresponding to shortest path. Future<void> _getDirections() async { _resetPolyline(); + try { var originlat = _originCoordinates.latitude; var originlon = _originCoordinates.longitude; @@ -254,13 +247,15 @@ class _MyHomePageState extends State<MyHomePage> { _data = jsonDecode(response.body); if (_data.containsKey('error_code') && _data['error_code'] != 0) { - switch(_data['error_code']) { - case 1: - _showMessage('The address is outside the solution space. Please enter an address within Berlin-Brandenburg.', context); - return; - case 2: - _showMessage('The address could not be found.', context); - return; + switch (_data['error_code']) { + case 1: + _showMessage( + 'The address is outside the solution space. Please enter an address within Berlin-Brandenburg.', + context); + return; + case 2: + _showMessage('The address could not be found.', context); + return; } } @@ -356,44 +351,6 @@ class _MyHomePageState extends State<MyHomePage> { _dateRange.end.difference(DateTime.now()).inDays); } - // Get coords between origin/dest points corresponding to shortest path and update _info. - // This part is going to be updated to take directions from backend instead of Direction API. - // Future<Directions> _getDirections() async { - // final directions = await DirectionsRepo().getDirections( - // origin: _originCoordinates, destination: _destCoordinates); - - // setState(() { - // _info = directions; - // }); - - // return _info; - // } - - // Draw Polyline between given list of coordinates and update _polyline - // void _drawPolyline() { - // _getDirections().then((value) { - // _polyline.add(Polyline( - // polylineId: PolylineId("polylineId"), - // color: Theme.of(context).primaryColor, - // width: 4, - // points: value.polylinePoints - // .map((e) => LatLng(e.latitude, e.longitude)) - // .toList(), - // )); - // _totalDuration = value.totalDuration; - // _totalDistance = value.totalDistance; - // setState(() { - // _polyline; - // _totalDuration; - // _totalDistance; - // }); - // // Animate camera to the shortest path. - // // _mapController.animateCamera( - // // CameraUpdate.newLatLngBounds(_info.bounds, 110.0), - // // ); - // }); - // } - // Helper function to reset polyline before calculating new polyline for another route. void _resetPolyline() { _polyline = []; @@ -403,7 +360,7 @@ class _MyHomePageState extends State<MyHomePage> { } // Helper function to reset marker. - void _resetOriginMarker() { + void _resetMarker() { _markers = {}; setState(() { _markers; @@ -429,7 +386,7 @@ class _MyHomePageState extends State<MyHomePage> { latlng.LatLng(position.latitude, position.longitude); }); _mapController.move(_originCoordinates, 13); - _resetOriginMarker(); + _resetMarker(); _addOriginMarker(); _resetPolyline(); }, @@ -445,7 +402,7 @@ class _MyHomePageState extends State<MyHomePage> { // Add origin marker on touch. void _appearOriginMarkerOnTouch(latlng.LatLng pos) async { _resetPolyline(); - _resetOriginMarker(); + _resetMarker(); _markers.add( Marker( width: 100, @@ -1033,141 +990,19 @@ class _MyHomePageState extends State<MyHomePage> { itemBuilder: (BuildContext context, int index) { return Stack( children: [ - _isLoading ? Center(child: const CircularProgressIndicator()) : Column( - children: [ - Container( - margin: EdgeInsets.only(bottom: 15, top: 5), - decoration: BoxDecoration( - color: Color.fromRGBO(53, 56, 63, 1), - borderRadius: - BorderRadius.all(Radius.circular(25))), - padding: EdgeInsets.only( - bottom: 15, top: 15, left: 20), - child: Row( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Container( - padding: EdgeInsets.only(right: 5, top: 2), - child: Icon( - Icons.calendar_month, - color: Theme.of(context).accentColor, - size: 20, - ), - ), - Text( - 'Picked Date: ', - style: TextStyle( - letterSpacing: 0.6, - fontSize: 21, - fontWeight: FontWeight.w500, - color: Theme.of(context).accentColor), - ), - Text( - displayDate(), - style: TextStyle( - fontSize: 20, - fontWeight: FontWeight.w400, - color: Theme.of(context).accentColor), - ), - ], - ), - ), - Container( - margin: EdgeInsets.only(bottom: 10), - padding: EdgeInsets.only(bottom: 15), - decoration: BoxDecoration( - color: Color.fromRGBO(53, 56, 63, 1), - borderRadius: - BorderRadius.all(Radius.circular(25))), - child: Column( + _isLoading + ? Center(child: const CircularProgressIndicator()) + : Column( children: [ Container( + margin: EdgeInsets.only(bottom: 15, top: 5), + decoration: BoxDecoration( + color: Color.fromRGBO(53, 56, 63, 1), + borderRadius: BorderRadius.all( + Radius.circular(25))), padding: EdgeInsets.only( - bottom: 5, top: 15, left: 17), + bottom: 15, top: 15, left: 20), child: Row( - crossAxisAlignment: - CrossAxisAlignment.start, - children: [ - Container( - padding: EdgeInsets.only(right: 8), - child: Icon( - WeatherIcons.cloudy, - color: - Theme.of(context).accentColor, - size: 18, - ), - ), - Text( - 'Undesired Weather: ', - style: TextStyle( - letterSpacing: 0.6, - fontSize: 21, - fontWeight: FontWeight.w500, - color: Theme.of(context) - .accentColor), - ), - ], - ), - ), - Row( - crossAxisAlignment: CrossAxisAlignment.end, - children: [ - SizedBox( - width: 20, - ), - if (!_selectedOptions.isEmpty) - ...returnWidget(), - ], - ), - ], - ), - ), - Container( - padding: EdgeInsets.only(top: 7, bottom: 7), - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - ElevatedButton( - onPressed: _collapse, - child: Container( - padding: EdgeInsets.only( - top: 10, - bottom: 10, - left: 135, - right: 135), - child: Text( - 'Info', - style: TextStyle(fontSize: 20), - ), - ), - style: ButtonStyle( - backgroundColor: MaterialStatePropertyAll( - Color.fromRGBO(13, 108, 114, 1), - ), - shape: MaterialStateProperty.all< - RoundedRectangleBorder>( - RoundedRectangleBorder( - borderRadius: - BorderRadius.circular(20), - ), - ), - ), - ), - ], - ), - ), - if (_availableDatesForTrip != null && _isCollapsed) - Container( - decoration: BoxDecoration( - color: Color.fromRGBO(53, 56, 63, 1), - borderRadius: - BorderRadius.all(Radius.circular(25))), - padding: EdgeInsets.only( - top: 15, left: 20, bottom: 15), - margin: EdgeInsets.only(top: 10, bottom: 10), - child: Column( - children: [ - Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ @@ -1175,14 +1010,14 @@ class _MyHomePageState extends State<MyHomePage> { padding: EdgeInsets.only(right: 5, top: 2), child: Icon( - Icons.access_time_filled_outlined, + Icons.calendar_month, color: Theme.of(context).accentColor, size: 20, ), ), Text( - 'Duration: ', + 'Picked Date: ', style: TextStyle( letterSpacing: 0.6, fontSize: 21, @@ -1191,7 +1026,7 @@ class _MyHomePageState extends State<MyHomePage> { .accentColor), ), Text( - _totalDuration, + displayDate(), style: TextStyle( fontSize: 20, fontWeight: FontWeight.w400, @@ -1200,155 +1035,295 @@ class _MyHomePageState extends State<MyHomePage> { ), ], ), - SizedBox( - height: 15, - ), - Row( - crossAxisAlignment: - CrossAxisAlignment.start, + ), + Container( + margin: EdgeInsets.only(bottom: 10), + padding: EdgeInsets.only(bottom: 15), + decoration: BoxDecoration( + color: Color.fromRGBO(53, 56, 63, 1), + borderRadius: BorderRadius.all( + Radius.circular(25))), + child: Column( children: [ Container( - padding: - EdgeInsets.only(right: 5, top: 2), - child: Icon( - Icons.directions_car_rounded, - color: - Theme.of(context).accentColor, - size: 20, + padding: EdgeInsets.only( + bottom: 5, top: 15, left: 17), + child: Row( + crossAxisAlignment: + CrossAxisAlignment.start, + children: [ + Container( + padding: + EdgeInsets.only(right: 8), + child: Icon( + WeatherIcons.cloudy, + color: Theme.of(context) + .accentColor, + size: 18, + ), + ), + Text( + 'Undesired Weather: ', + style: TextStyle( + letterSpacing: 0.6, + fontSize: 21, + fontWeight: FontWeight.w500, + color: Theme.of(context) + .accentColor), + ), + ], ), ), - Text( - 'Distance: ', - style: TextStyle( - letterSpacing: 0.6, - fontSize: 21, - fontWeight: FontWeight.w500, - color: Theme.of(context) - .accentColor), - ), - Text( - _totalDistance, - style: TextStyle( - fontSize: 20, - fontWeight: FontWeight.w400, - color: Theme.of(context) - .accentColor), + Row( + crossAxisAlignment: + CrossAxisAlignment.end, + children: [ + SizedBox( + width: 20, + ), + if (!_selectedOptions.isEmpty) + ...returnWidget(), + ], ), ], ), - SizedBox( - height: 15, - ), - Row( - crossAxisAlignment: - CrossAxisAlignment.start, + ), + Container( + padding: EdgeInsets.only(top: 7, bottom: 7), + child: Row( + mainAxisAlignment: + MainAxisAlignment.center, children: [ - Container( - padding: - EdgeInsets.only(right: 5, top: 2), - child: Icon( - Icons.date_range, - color: - Theme.of(context).accentColor, - size: 20, + ElevatedButton( + onPressed: _collapse, + child: Container( + padding: EdgeInsets.only( + top: 10, + bottom: 10, + left: 135, + right: 135), + child: Text( + 'Info', + style: TextStyle(fontSize: 20), + ), + ), + style: ButtonStyle( + backgroundColor: + MaterialStatePropertyAll( + Color.fromRGBO(13, 108, 114, 1), + ), + shape: MaterialStateProperty.all< + RoundedRectangleBorder>( + RoundedRectangleBorder( + borderRadius: + BorderRadius.circular(20), + ), + ), ), - ), - Text( - 'Date Range: ', - style: TextStyle( - letterSpacing: 0.6, - fontSize: 21, - fontWeight: FontWeight.w500, - color: Theme.of(context) - .accentColor), - ), - Text( - _totalDaysFormatted, - style: TextStyle( - fontSize: 20, - fontWeight: FontWeight.w400, - color: Theme.of(context) - .accentColor), ), ], ), - ], - ), - ), - if (_availableDatesForTrip != null && _isCollapsed) - Container( - margin: EdgeInsets.only(top: 10), - padding: EdgeInsets.only(left: 5, top: 10), - decoration: BoxDecoration( - color: Color.fromRGBO(53, 56, 63, 1), - borderRadius: - BorderRadius.all(Radius.circular(25))), - child: Column( - children: [ - Row(children: [ - Container( - padding: EdgeInsets.only( - right: 3, - top: 4, - bottom: 7, - left: 15), - child: Icon( - Icons.event_available, - color: Theme.of(context).accentColor, - size: 24, - ), - ), - Container( - padding: EdgeInsets.only( - top: 5, left: 5, bottom: 10), - child: Text( - 'Available Dates:', - style: TextStyle( - letterSpacing: 0.6, - fontSize: 21, - fontWeight: FontWeight.w500, - color: Theme.of(context) - .accentColor), - ), + ), + if (_availableDatesForTrip != null && + _isCollapsed) + Container( + decoration: BoxDecoration( + color: Color.fromRGBO(53, 56, 63, 1), + borderRadius: BorderRadius.all( + Radius.circular(25))), + padding: EdgeInsets.only( + top: 15, left: 20, bottom: 15), + margin: + EdgeInsets.only(top: 10, bottom: 10), + child: Column( + children: [ + Row( + crossAxisAlignment: + CrossAxisAlignment.start, + children: [ + Container( + padding: EdgeInsets.only( + right: 5, top: 2), + child: Icon( + Icons + .access_time_filled_outlined, + color: Theme.of(context) + .accentColor, + size: 20, + ), + ), + Text( + 'Duration: ', + style: TextStyle( + letterSpacing: 0.6, + fontSize: 21, + fontWeight: FontWeight.w500, + color: Theme.of(context) + .accentColor), + ), + Text( + _totalDuration, + style: TextStyle( + fontSize: 20, + fontWeight: FontWeight.w400, + color: Theme.of(context) + .accentColor), + ), + ], + ), + SizedBox( + height: 15, + ), + Row( + crossAxisAlignment: + CrossAxisAlignment.start, + children: [ + Container( + padding: EdgeInsets.only( + right: 5, top: 2), + child: Icon( + Icons.directions_car_rounded, + color: Theme.of(context) + .accentColor, + size: 20, + ), + ), + Text( + 'Distance: ', + style: TextStyle( + letterSpacing: 0.6, + fontSize: 21, + fontWeight: FontWeight.w500, + color: Theme.of(context) + .accentColor), + ), + Text( + _totalDistance, + style: TextStyle( + fontSize: 20, + fontWeight: FontWeight.w400, + color: Theme.of(context) + .accentColor), + ), + ], + ), + SizedBox( + height: 15, + ), + Row( + crossAxisAlignment: + CrossAxisAlignment.start, + children: [ + Container( + padding: EdgeInsets.only( + right: 5, top: 2), + child: Icon( + Icons.date_range, + color: Theme.of(context) + .accentColor, + size: 20, + ), + ), + Text( + 'Date Range: ', + style: TextStyle( + letterSpacing: 0.6, + fontSize: 21, + fontWeight: FontWeight.w500, + color: Theme.of(context) + .accentColor), + ), + Text( + _totalDaysFormatted, + style: TextStyle( + fontSize: 20, + fontWeight: FontWeight.w400, + color: Theme.of(context) + .accentColor), + ), + ], + ), + ], ), - ]), - for (var i = 0; - i < _availableDatesForTrip.length; - i++) - Container( - padding: const EdgeInsets.only( - top: 5, bottom: 15, left: 5), - child: Row( - crossAxisAlignment: - CrossAxisAlignment.center, - children: [ + ), + if (_availableDatesForTrip != null && + _isCollapsed) + Container( + margin: EdgeInsets.only(top: 10), + padding: + EdgeInsets.only(left: 5, top: 10), + decoration: BoxDecoration( + color: Color.fromRGBO(53, 56, 63, 1), + borderRadius: BorderRadius.all( + Radius.circular(25))), + child: Column( + children: [ + Row(children: [ Container( padding: EdgeInsets.only( - right: 6, left: 12), + right: 3, + top: 4, + bottom: 7, + left: 15), child: Icon( - Icons.check_rounded, + Icons.event_available, color: Theme.of(context) .accentColor, - size: 20, + size: 24, ), ), - Text( - _availableDatesForTrip[i], - style: TextStyle( - letterSpacing: 0.6, - fontSize: 20, - fontWeight: FontWeight.w400, - color: Theme.of(context) - .accentColor), + Container( + padding: EdgeInsets.only( + top: 5, left: 5, bottom: 10), + child: Text( + 'Available Dates:', + style: TextStyle( + letterSpacing: 0.6, + fontSize: 21, + fontWeight: FontWeight.w500, + color: Theme.of(context) + .accentColor), + ), ), - ], - ), - ) - ], - ), + ]), + for (var i = 0; + i < _availableDatesForTrip.length; + i++) + Container( + padding: const EdgeInsets.only( + top: 5, bottom: 15, left: 5), + child: Row( + crossAxisAlignment: + CrossAxisAlignment.center, + children: [ + Container( + padding: EdgeInsets.only( + right: 6, left: 12), + child: Icon( + Icons.check_rounded, + color: Theme.of(context) + .accentColor, + size: 20, + ), + ), + Text( + _availableDatesForTrip[i], + style: TextStyle( + letterSpacing: 0.6, + fontSize: 20, + fontWeight: + FontWeight.w400, + color: Theme.of(context) + .accentColor), + ), + ], + ), + ) + ], + ), + ), + ], ), - ], - ), ], ); }, diff --git a/lib/widgets/directions.dart b/lib/widgets/directions.dart deleted file mode 100644 index 2e66c6f..0000000 --- a/lib/widgets/directions.dart +++ /dev/null @@ -1,48 +0,0 @@ -// // ignore_for_file: import_of_legacy_library_into_null_safe - -// import 'package:flutter_polyline_points/flutter_polyline_points.dart'; -// import 'package:google_maps_flutter/google_maps_flutter.dart'; - -// class Directions { -// final LatLngBounds bounds; -// final List<PointLatLng> polylinePoints; -// final String totalDistance; -// final String totalDuration; - -// const Directions({ -// required this.bounds, -// required this.polylinePoints, -// required this.totalDistance, -// required this.totalDuration, -// }); - -// factory Directions.fromMap(Map<String, dynamic> map) { -// // Get route information -// final data = Map<String, dynamic>.from(map['routes'][0]); - -// // Bounds -// final northeast = data['bounds']['northeast']; -// final southwest = data['bounds']['southwest']; -// final bounds = LatLngBounds( -// northeast: LatLng(northeast['lat'], northeast['lng']), -// southwest: LatLng(southwest['lat'], southwest['lng']), -// ); - -// // Distance and Duration -// String distance = ''; -// String duration = ''; -// if ((data['legs'] as List).isNotEmpty) { -// final leg = data['legs'][0]; -// distance = leg['distance']['text']; -// duration = leg['duration']['text']; -// } - -// return Directions( -// bounds: bounds, -// polylinePoints: -// PolylinePoints().decodePolyline(data['overview_polyline']['points']), -// totalDistance: distance, -// totalDuration: duration, -// ); -// } -// } diff --git a/lib/widgets/directionsRepo.dart b/lib/widgets/directionsRepo.dart deleted file mode 100644 index feb6c53..0000000 --- a/lib/widgets/directionsRepo.dart +++ /dev/null @@ -1,28 +0,0 @@ -// ignore_for_file: file_names - -// // import 'package:google_maps_flutter/google_maps_flutter.dart'; -// import '../config/config.dart'; -// import 'package:latlong/latlong.dart' as latlng; -// import 'package:http/http.dart' as http; - -// class DirectionsRepo { -// Config config = Config(); -// latlng.LatLng origin; -// latlng.LatLng destination; -// DirectionsRepo(this.origin, this.destination); - -// Future<http.Response> getDirections() async { -// var originlat = origin.latitude; -// var originlon = origin.longitude; -// var destlat = destination.latitude; -// var destlon = destination.longitude; -// final response = await http.get( -// Uri.parse( -// 'https://api.openrouteservice.org/v2/directions/driving-car?api_key=${config.OSM_API}&start=$originlon,$originlat&end=$destlon,$destlat', -// ), -// ); - -// // Check if response is successful -// return response; -// } -// } diff --git a/lib/widgets/getStreetNumber.dart b/lib/widgets/getStreetNumber.dart index a69e45e..ef406a6 100644 --- a/lib/widgets/getStreetNumber.dart +++ b/lib/widgets/getStreetNumber.dart @@ -28,7 +28,7 @@ class _GetStreetNumberState extends State<GetStreetNumber> { ); var jsonData = jsonDecode(response.body); suggestions = []; - for (int i = 0; i < jsonData['predictions'][i].length; i++) { + for (int i = 0; i < jsonData['predictions'].length; i++) { suggestions.add(jsonData['predictions'][i]['description'].toString()); } return suggestions; diff --git a/lib/widgets/returnDestination.dart b/lib/widgets/returnDestination.dart index 2b7681e..c6ba8e0 100644 --- a/lib/widgets/returnDestination.dart +++ b/lib/widgets/returnDestination.dart @@ -27,7 +27,7 @@ class _ReturnDestinationState extends State<ReturnDestination> { ); var jsonData = jsonDecode(response.body); suggestions = []; - for (int i = 0; i < jsonData['predictions'][i].length; i++) { + for (int i = 0; i < jsonData['predictions'].length; i++) { suggestions.add(jsonData['predictions'][i]['description'].toString()); } return suggestions; diff --git a/lib/widgets/returnOrigin.dart b/lib/widgets/returnOrigin.dart index 6ea59f9..017f7c5 100644 --- a/lib/widgets/returnOrigin.dart +++ b/lib/widgets/returnOrigin.dart @@ -27,7 +27,7 @@ class _ReturnOriginState extends State<ReturnOrigin> { ); var jsonData = jsonDecode(response.body); suggestions = []; - for (int i = 0; i < jsonData['predictions'][i].length; i++) { + for (int i = 0; i < jsonData['predictions'].length; i++) { suggestions.add(jsonData['predictions'][i]['description'].toString()); } return suggestions; -- GitLab