我无法将坐标从 ListView(使用 ListTile 使其可点击)发送到我的谷歌地图。到目前为止,作为一个小测试,我希望地图以该位置为中心。
我的列表屏幕:
class ListScreen extends StatelessWidget {
final List<TrailModel> trails;
ListScreen(this.trails);
@override
Widget build(BuildContext ctxt) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Here are your trails"),
),
body: TrailList(trails),
);
}
}
class TrailList extends StatelessWidget {
final List<TrailModel> trails;
TrailList(this.trails);
Widget build(context) {
return ListView.builder(
itemCount: trails.length,
itemBuilder: (context, int index) {
Object myText = json.encode(trails[index].trails);
List<dynamic> myText2 = json.decode(myText);
return ListTile(
contentPadding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
leading: Container(
decoration: BoxDecoration(
border: Border.all(color:Colors.black),
),
padding: EdgeInsets.all(20.0),
margin: EdgeInsets.all(10.0),
child: Text(myText2[index]['name']),
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => new MapScreen(myText2[index]['latitude'], myText2[index]['longitude']),
),
);
},
);
},
);
}
}
我的地图屏幕:
class MapScreen extends StatefulWidget {
final double latitude;
final double longitude;
MapScreen(this.latitude, this.longitude);
@override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
GoogleMapController mapController;
MapType _currentMapType = MapType.normal;
//error here
final LatLng _center = const LatLng(widget.latitude, widget.longitude);
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
void _onMapTypeButtonPressed() {
if (_currentMapType == MapType.normal) {
mapController.updateMapOptions(
GoogleMapOptions(mapType: MapType.satellite),
);
_currentMapType = MapType.satellite;
} else {
mapController.updateMapOptions(
GoogleMapOptions(mapType: MapType.normal),
);
_currentMapType = MapType.normal;
}
}
void _onAddMarkerButtonPressed() {
mapController.addMarker(
MarkerOptions(
position: LatLng(
mapController.cameraPosition.target.latitude,
mapController.cameraPosition.target.longitude,
),
infoWindowText: InfoWindowText('Random Place', '5 Star Rating'),
icon: BitmapDescriptor.defaultMarker,
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("sample map"),
backgroundColor: Colors.green[700],
),
body: Stack(
children: <Widget>[
GoogleMap(
onMapCreated: _onMapCreated,
options: GoogleMapOptions(
trackCameraPosition: true,
cameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Align(
alignment: Alignment.topRight,
child: Column(
children: <Widget>[
FloatingActionButton(
onPressed: _onMapTypeButtonPressed,
materialTapTargetSize: MaterialTapTargetSize.padded,
backgroundColor: Colors.green,
heroTag: null,
child: const Icon(Icons.map, size: 36.0),
),
const SizedBox(height: 16.0),
FloatingActionButton(
onPressed: _onAddMarkerButtonPressed,
materialTapTargetSize: MaterialTapTargetSize.padded,
backgroundColor: Colors.green,
heroTag: null,
child: const Icon(Icons.add_location, size: 36.0),
),
],
),
),
),
],
),
),
);
}
}
我的错误消息:此常量表达式的求值引发异常
如果我删除 const:在初始化程序中只能访问静态
我最初的尝试只是传递给构造函数,如:
double latitude;
double longitude;
_MapScreenState(this.latitude, this.longitude);
final LatLng _center = const LatLng(latitude, longitude);
但这给了我那两个错误,再加上一个:常量创建的参数必须是 const 表达式。
请,任何帮助将不胜感激。谷歌搜索这些消息似乎太模糊了。提前致谢
删除const
关键字并_center
在构造函数中初始化变量。你可以这样做:
_MapScreenState() {
_center = LatLng(widget.latitude, widget.longitude);
}
您可以参考此问题以更详细地解释为什么这可能会起作用。希望这可以帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句