99 lines
2.4 KiB
Dart
99 lines
2.4 KiB
Dart
|
|
// A search bar that allow the user to enter a city name
|
|
import 'package:anyway/structs/landmark.dart';
|
|
import 'package:geocoding/geocoding.dart';
|
|
import 'dart:developer';
|
|
|
|
import 'package:anyway/structs/trip.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class NewTripLocationSearch extends StatefulWidget {
|
|
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
|
|
Trip trip;
|
|
|
|
NewTripLocationSearch(
|
|
this.trip,
|
|
);
|
|
|
|
|
|
@override
|
|
State<NewTripLocationSearch> createState() => _NewTripLocationSearchState();
|
|
}
|
|
|
|
class _NewTripLocationSearchState extends State<NewTripLocationSearch> {
|
|
final TextEditingController _controller = TextEditingController();
|
|
|
|
setTripLocation (String query) async {
|
|
List<Location> locations = [];
|
|
log('Searching for: $query');
|
|
|
|
try{
|
|
locations = await locationFromAddress(query);
|
|
} catch (e) {
|
|
log('No results found for: $query : $e');
|
|
}
|
|
|
|
if (locations.isNotEmpty) {
|
|
Location location = locations.first;
|
|
widget.trip.landmarks.clear();
|
|
widget.trip.addLandmark(
|
|
Landmark(
|
|
uuid: 'pending',
|
|
name: query,
|
|
location: [location.latitude, location.longitude],
|
|
type: start
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
late Widget locationSearchBar = SearchBar(
|
|
hintText: 'Enter a city name or long press on the map.',
|
|
onSubmitted: setTripLocation,
|
|
controller: _controller,
|
|
leading: Icon(Icons.search),
|
|
trailing: [
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
setTripLocation(_controller.text);
|
|
},
|
|
child: Text('Search'),
|
|
)
|
|
]
|
|
);
|
|
|
|
|
|
late Widget useCurrentLocationButton = ElevatedButton(
|
|
onPressed: () {
|
|
// setTripLocation(location);
|
|
// TODO: get current location
|
|
},
|
|
child: Text('Use current location'),
|
|
);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return FutureBuilder(
|
|
future: widget.prefs,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasData) {
|
|
final useLocation = snapshot.data!.getBool('useLocation') ?? false;
|
|
if (useLocation) {
|
|
return Column(
|
|
children: [
|
|
locationSearchBar,
|
|
useCurrentLocationButton,
|
|
],
|
|
);
|
|
} else {
|
|
return locationSearchBar;
|
|
}
|
|
} else {
|
|
return locationSearchBar;
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|