mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 14:58:34 +00:00
dart migrate place_tracker (#831)
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
|
||||
class Place {
|
||||
@@ -10,33 +9,29 @@ class Place {
|
||||
final LatLng latLng;
|
||||
final String name;
|
||||
final PlaceCategory category;
|
||||
final String description;
|
||||
final String? description;
|
||||
final int starRating;
|
||||
|
||||
const Place({
|
||||
@required this.id,
|
||||
@required this.latLng,
|
||||
@required this.name,
|
||||
@required this.category,
|
||||
required this.id,
|
||||
required this.latLng,
|
||||
required this.name,
|
||||
required this.category,
|
||||
this.description,
|
||||
this.starRating = 0,
|
||||
}) : assert(id != null),
|
||||
assert(latLng != null),
|
||||
assert(name != null),
|
||||
assert(category != null),
|
||||
assert(starRating != null && starRating >= 0 && starRating <= 5);
|
||||
}) : assert(starRating >= 0 && starRating <= 5);
|
||||
|
||||
double get latitude => latLng.latitude;
|
||||
|
||||
double get longitude => latLng.longitude;
|
||||
|
||||
Place copyWith({
|
||||
String id,
|
||||
LatLng latLng,
|
||||
String name,
|
||||
PlaceCategory category,
|
||||
String description,
|
||||
int starRating,
|
||||
String? id,
|
||||
LatLng? latLng,
|
||||
String? name,
|
||||
PlaceCategory? category,
|
||||
String? description,
|
||||
int? starRating,
|
||||
}) {
|
||||
return Place(
|
||||
id: id ?? this.id,
|
||||
|
||||
@@ -15,20 +15,18 @@ class PlaceDetails extends StatefulWidget {
|
||||
final ValueChanged<Place> onChanged;
|
||||
|
||||
const PlaceDetails({
|
||||
@required this.place,
|
||||
@required this.onChanged,
|
||||
Key key,
|
||||
}) : assert(place != null),
|
||||
assert(onChanged != null),
|
||||
super(key: key);
|
||||
required this.place,
|
||||
required this.onChanged,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
PlaceDetailsState createState() => PlaceDetailsState();
|
||||
}
|
||||
|
||||
class PlaceDetailsState extends State<PlaceDetails> {
|
||||
Place _place;
|
||||
GoogleMapController _mapController;
|
||||
late Place _place;
|
||||
GoogleMapController? _mapController;
|
||||
final Set<Marker> _markers = {};
|
||||
final TextEditingController _nameController = TextEditingController();
|
||||
final TextEditingController _descriptionController = TextEditingController();
|
||||
@@ -65,7 +63,7 @@ class PlaceDetailsState extends State<PlaceDetails> {
|
||||
void initState() {
|
||||
_place = widget.place;
|
||||
_nameController.text = _place.name;
|
||||
_descriptionController.text = _place.description;
|
||||
_descriptionController.text = _place.description!;
|
||||
return super.initState();
|
||||
}
|
||||
|
||||
@@ -124,12 +122,10 @@ class _DescriptionTextField extends StatelessWidget {
|
||||
|
||||
final ValueChanged<String> onChanged;
|
||||
const _DescriptionTextField({
|
||||
@required this.controller,
|
||||
@required this.onChanged,
|
||||
Key key,
|
||||
}) : assert(controller != null),
|
||||
assert(onChanged != null),
|
||||
super(key: key);
|
||||
required this.controller,
|
||||
required this.onChanged,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -155,18 +151,16 @@ class _DescriptionTextField extends StatelessWidget {
|
||||
class _Map extends StatelessWidget {
|
||||
final LatLng center;
|
||||
|
||||
final GoogleMapController mapController;
|
||||
final GoogleMapController? mapController;
|
||||
final ArgumentCallback<GoogleMapController> onMapCreated;
|
||||
final Set<Marker> markers;
|
||||
const _Map({
|
||||
@required this.center,
|
||||
@required this.mapController,
|
||||
@required this.onMapCreated,
|
||||
@required this.markers,
|
||||
Key key,
|
||||
}) : assert(center != null),
|
||||
assert(onMapCreated != null),
|
||||
super(key: key);
|
||||
required this.center,
|
||||
required this.mapController,
|
||||
required this.onMapCreated,
|
||||
required this.markers,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -198,12 +192,10 @@ class _NameTextField extends StatelessWidget {
|
||||
|
||||
final ValueChanged<String> onChanged;
|
||||
const _NameTextField({
|
||||
@required this.controller,
|
||||
@required this.onChanged,
|
||||
Key key,
|
||||
}) : assert(controller != null),
|
||||
assert(onChanged != null),
|
||||
super(key: key);
|
||||
required this.controller,
|
||||
required this.onChanged,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -227,7 +219,7 @@ class _NameTextField extends StatelessWidget {
|
||||
|
||||
class _Reviews extends StatelessWidget {
|
||||
const _Reviews({
|
||||
Key key,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
@@ -320,11 +312,10 @@ class _StarBar extends StatelessWidget {
|
||||
final int rating;
|
||||
final ValueChanged<int> onChanged;
|
||||
const _StarBar({
|
||||
@required this.rating,
|
||||
@required this.onChanged,
|
||||
Key key,
|
||||
}) : assert(rating != null && rating >= 0 && rating <= maxStars),
|
||||
assert(onChanged != null),
|
||||
required this.rating,
|
||||
required this.onChanged,
|
||||
Key? key,
|
||||
}) : assert(rating >= 0 && rating <= maxStars),
|
||||
super(key: key);
|
||||
|
||||
@override
|
||||
|
||||
@@ -10,7 +10,7 @@ import 'place_details.dart';
|
||||
import 'place_tracker_app.dart';
|
||||
|
||||
class PlaceList extends StatefulWidget {
|
||||
const PlaceList({Key key}) : super(key: key);
|
||||
const PlaceList({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
PlaceListState createState() => PlaceListState();
|
||||
@@ -69,17 +69,15 @@ class _CategoryButton extends StatelessWidget {
|
||||
final bool selected;
|
||||
final ValueChanged<PlaceCategory> onCategoryChanged;
|
||||
const _CategoryButton({
|
||||
Key key,
|
||||
@required this.category,
|
||||
@required this.selected,
|
||||
@required this.onCategoryChanged,
|
||||
}) : assert(category != null),
|
||||
assert(selected != null),
|
||||
super(key: key);
|
||||
Key? key,
|
||||
required this.category,
|
||||
required this.selected,
|
||||
required this.onCategoryChanged,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
String _buttonText;
|
||||
late String _buttonText;
|
||||
switch (category) {
|
||||
case PlaceCategory.favorite:
|
||||
_buttonText = 'Favorites';
|
||||
@@ -122,12 +120,10 @@ class _ListCategoryButtonBar extends StatelessWidget {
|
||||
|
||||
final ValueChanged<PlaceCategory> onCategoryChanged;
|
||||
const _ListCategoryButtonBar({
|
||||
Key key,
|
||||
@required this.selectedCategory,
|
||||
@required this.onCategoryChanged,
|
||||
}) : assert(selectedCategory != null),
|
||||
assert(onCategoryChanged != null),
|
||||
super(key: key);
|
||||
Key? key,
|
||||
required this.selectedCategory,
|
||||
required this.onCategoryChanged,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -159,12 +155,10 @@ class _PlaceListTile extends StatelessWidget {
|
||||
|
||||
final ValueChanged<Place> onPlaceChanged;
|
||||
const _PlaceListTile({
|
||||
Key key,
|
||||
@required this.place,
|
||||
@required this.onPlaceChanged,
|
||||
}) : assert(place != null),
|
||||
assert(onPlaceChanged != null),
|
||||
super(key: key);
|
||||
Key? key,
|
||||
required this.place,
|
||||
required this.onPlaceChanged,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -21,10 +21,9 @@ class MapConfiguration {
|
||||
final PlaceCategory selectedCategory;
|
||||
|
||||
const MapConfiguration({
|
||||
@required this.places,
|
||||
@required this.selectedCategory,
|
||||
}) : assert(places != null),
|
||||
assert(selectedCategory != null);
|
||||
required this.places,
|
||||
required this.selectedCategory,
|
||||
});
|
||||
|
||||
@override
|
||||
int get hashCode => places.hashCode ^ selectedCategory.hashCode;
|
||||
@@ -53,10 +52,10 @@ class MapConfiguration {
|
||||
}
|
||||
|
||||
class PlaceMap extends StatefulWidget {
|
||||
final LatLng center;
|
||||
final LatLng? center;
|
||||
|
||||
const PlaceMap({
|
||||
Key key,
|
||||
Key? key,
|
||||
this.center,
|
||||
}) : super(key: key);
|
||||
|
||||
@@ -69,15 +68,15 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
|
||||
MapType _currentMapType = MapType.normal;
|
||||
|
||||
LatLng _lastMapPosition;
|
||||
LatLng? _lastMapPosition;
|
||||
|
||||
final Map<Marker, Place> _markedPlaces = <Marker, Place>{};
|
||||
|
||||
final Set<Marker> _markers = {};
|
||||
|
||||
Marker _pendingMarker;
|
||||
Marker? _pendingMarker;
|
||||
|
||||
MapConfiguration _configuration;
|
||||
MapConfiguration? _configuration;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -95,7 +94,7 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
GoogleMap(
|
||||
onMapCreated: onMapCreated,
|
||||
initialCameraPosition: CameraPosition(
|
||||
target: widget.center,
|
||||
target: widget.center!,
|
||||
zoom: 11.0,
|
||||
),
|
||||
mapType: _currentMapType,
|
||||
@@ -173,9 +172,9 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
if (_pendingMarker != null) {
|
||||
// Create a new Place and map it to the marker we just added.
|
||||
final newPlace = Place(
|
||||
id: Uuid().v1(),
|
||||
latLng: _pendingMarker.position,
|
||||
name: _pendingMarker.infoWindow.title,
|
||||
id: const Uuid().v1(),
|
||||
latLng: _pendingMarker!.position,
|
||||
name: _pendingMarker!.infoWindow.title!,
|
||||
category:
|
||||
Provider.of<AppState>(context, listen: false).selectedCategory,
|
||||
);
|
||||
@@ -184,7 +183,7 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
Provider.of<AppState>(context, listen: false).selectedCategory);
|
||||
|
||||
setState(() {
|
||||
final updatedMarker = _pendingMarker.copyWith(
|
||||
final updatedMarker = _pendingMarker!.copyWith(
|
||||
iconParam: placeMarker,
|
||||
infoWindowParam: InfoWindow(
|
||||
title: 'New Place',
|
||||
@@ -263,9 +262,9 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
// changes come from the [place_map], we should only enter this if statement
|
||||
// when returning to the [place_map] after changes have been made from
|
||||
// [place_list].
|
||||
if (_configuration != newConfiguration && mapController != null) {
|
||||
if (_configuration.places == newConfiguration.places &&
|
||||
_configuration.selectedCategory !=
|
||||
if (_configuration != newConfiguration) {
|
||||
if (_configuration!.places == newConfiguration.places &&
|
||||
_configuration!.selectedCategory !=
|
||||
newConfiguration.selectedCategory) {
|
||||
// If the configuration change is only a category change, just update
|
||||
// the marker visibilities.
|
||||
@@ -274,7 +273,7 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
// At this point, we know the places have been updated from the list
|
||||
// view. We need to reconfigure the map to respect the updates.
|
||||
newConfiguration.places
|
||||
.where((p) => !_configuration.places.contains(p))
|
||||
.where((p) => !_configuration!.places.contains(p))
|
||||
.map((value) => _updateExistingPlaceMarker(place: value));
|
||||
|
||||
await _zoomToFitPlaces(
|
||||
@@ -292,7 +291,7 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
setState(() {
|
||||
final newMarker = Marker(
|
||||
markerId: MarkerId(_lastMapPosition.toString()),
|
||||
position: _lastMapPosition,
|
||||
position: _lastMapPosition!,
|
||||
infoWindow: const InfoWindow(title: 'New Place'),
|
||||
draggable: true,
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
|
||||
@@ -333,8 +332,6 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
}
|
||||
|
||||
void _pushPlaceDetailsScreen(Place place) {
|
||||
assert(place != null);
|
||||
|
||||
Navigator.push<void>(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) {
|
||||
@@ -349,7 +346,7 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
Future<void> _showPlacesForSelectedCategory(PlaceCategory category) async {
|
||||
setState(() {
|
||||
for (var marker in List.of(_markedPlaces.keys)) {
|
||||
final place = _markedPlaces[marker];
|
||||
final place = _markedPlaces[marker]!;
|
||||
final updatedMarker = marker.copyWith(
|
||||
visibleParam: place.category == category,
|
||||
);
|
||||
@@ -373,9 +370,9 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
await _showPlacesForSelectedCategory(category);
|
||||
}
|
||||
|
||||
void _updateExistingPlaceMarker({@required Place place}) {
|
||||
void _updateExistingPlaceMarker({required Place place}) {
|
||||
var marker = _markedPlaces.keys
|
||||
.singleWhere((value) => _markedPlaces[value].id == place.id);
|
||||
.singleWhere((value) => _markedPlaces[value]!.id == place.id);
|
||||
|
||||
setState(() {
|
||||
final updatedMarker = marker.copyWith(
|
||||
@@ -390,9 +387,9 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
}
|
||||
|
||||
void _updateMarker({
|
||||
@required Marker marker,
|
||||
@required Marker updatedMarker,
|
||||
@required Place place,
|
||||
required Marker? marker,
|
||||
required Marker updatedMarker,
|
||||
required Place place,
|
||||
}) {
|
||||
_markers.remove(marker);
|
||||
_markedPlaces.remove(marker);
|
||||
@@ -405,10 +402,10 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
var controller = await mapController.future;
|
||||
|
||||
// Default min/max values to latitude and longitude of center.
|
||||
var minLat = widget.center.latitude;
|
||||
var maxLat = widget.center.latitude;
|
||||
var minLong = widget.center.longitude;
|
||||
var maxLong = widget.center.longitude;
|
||||
var minLat = widget.center!.latitude;
|
||||
var maxLat = widget.center!.latitude;
|
||||
var minLong = widget.center!.longitude;
|
||||
var maxLong = widget.center!.longitude;
|
||||
|
||||
for (var place in places) {
|
||||
minLat = min(minLat, place.latitude);
|
||||
@@ -435,12 +432,10 @@ class PlaceMapState extends State<PlaceMap> {
|
||||
return BitmapDescriptor.fromAssetImage(
|
||||
createLocalImageConfiguration(context, size: const Size.square(32)),
|
||||
'assets/heart.png');
|
||||
break;
|
||||
case PlaceCategory.visited:
|
||||
return BitmapDescriptor.fromAssetImage(
|
||||
createLocalImageConfiguration(context, size: const Size.square(32)),
|
||||
'assets/visited.png');
|
||||
break;
|
||||
case PlaceCategory.wantToGo:
|
||||
default:
|
||||
return BitmapDescriptor.defaultMarker;
|
||||
@@ -460,14 +455,11 @@ class _AddPlaceButtonBar extends StatelessWidget {
|
||||
final VoidCallback onCancelPressed;
|
||||
|
||||
const _AddPlaceButtonBar({
|
||||
Key key,
|
||||
@required this.visible,
|
||||
@required this.onSavePressed,
|
||||
@required this.onCancelPressed,
|
||||
}) : assert(visible != null),
|
||||
assert(onSavePressed != null),
|
||||
assert(onCancelPressed != null),
|
||||
super(key: key);
|
||||
Key? key,
|
||||
required this.visible,
|
||||
required this.onSavePressed,
|
||||
required this.onCancelPressed,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -508,14 +500,11 @@ class _CategoryButtonBar extends StatelessWidget {
|
||||
final ValueChanged<PlaceCategory> onChanged;
|
||||
|
||||
const _CategoryButtonBar({
|
||||
Key key,
|
||||
@required this.selectedPlaceCategory,
|
||||
@required this.visible,
|
||||
@required this.onChanged,
|
||||
}) : assert(selectedPlaceCategory != null),
|
||||
assert(visible != null),
|
||||
assert(onChanged != null),
|
||||
super(key: key);
|
||||
Key? key,
|
||||
required this.selectedPlaceCategory,
|
||||
required this.visible,
|
||||
required this.onChanged,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -573,14 +562,11 @@ class _MapFabs extends StatelessWidget {
|
||||
final VoidCallback onToggleMapTypePressed;
|
||||
|
||||
const _MapFabs({
|
||||
Key key,
|
||||
@required this.visible,
|
||||
@required this.onAddPlacePressed,
|
||||
@required this.onToggleMapTypePressed,
|
||||
}) : assert(visible != null),
|
||||
assert(onAddPlacePressed != null),
|
||||
assert(onToggleMapTypePressed != null),
|
||||
super(key: key);
|
||||
Key? key,
|
||||
required this.visible,
|
||||
required this.onAddPlacePressed,
|
||||
required this.onToggleMapTypePressed,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -17,7 +17,7 @@ enum PlaceTrackerViewType {
|
||||
}
|
||||
|
||||
class PlaceTrackerApp extends StatelessWidget {
|
||||
const PlaceTrackerApp({Key key}) : super(key: key);
|
||||
const PlaceTrackerApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -28,7 +28,7 @@ class PlaceTrackerApp extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _PlaceTrackerHomePage extends StatelessWidget {
|
||||
const _PlaceTrackerHomePage({Key key}) : super(key: key);
|
||||
const _PlaceTrackerHomePage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -83,8 +83,7 @@ class AppState extends ChangeNotifier {
|
||||
this.places = StubData.places,
|
||||
this.selectedCategory = PlaceCategory.favorite,
|
||||
this.viewType = PlaceTrackerViewType.map,
|
||||
}) : assert(places != null),
|
||||
assert(selectedCategory != null);
|
||||
});
|
||||
|
||||
List<Place> places;
|
||||
PlaceCategory selectedCategory;
|
||||
|
||||
Reference in New Issue
Block a user