Dart/Flutter 将坐标从 ListTile 传递到另一个页面上的谷歌地图

迈克1982

我无法将坐标从 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从ListTile导航到另一个页面

将WooCommerce订单ID从另一个页面传递到当前页面上的JavaScript变量

将数据传递到另一个页面并在该页面上提交表单

如何将谷歌地理位置坐标传递给另一个函数?

将值从页面传递到另一个

将变量从一个jsp页面传递到另一个页面?

将页面上发生的点击事件复制到另一个页面

将jscolor从一个php页面传递到另一个

如何将文本或段落从页面传递到另一个页面

如何将数据从当前页面传递到另一个页面?

使用 VB 将值从页面传递到另一个页面 ASP.net

如何将数据从html页面传递到另一个页面?

将变量从页面传递到另一个页面。颤动/飞镖

你可以让 ListTile 中的 onTap 打开它下面的另一个 ListTile 吗?

将两个 php 变量从链接传递到另一个页面

将js变量传递给另一个页面上的php变量并转到目标页面

将值从另一个页面对象传递到一个页面对象

将 javascript 对象变量从一个 HTML 页面传递到另一个页面

如何将数据从一个 html 页面传递到另一个页面?

将ID从一个页面小部件传递到另一个页面方法

通过 DrawerNavigator 中的 StackNavigator 将数据从一个页面传递到另一个页面

将 JSON 数据从一个 php 页面传递到另一个页面

使用 Messaging Center 将数据从一个页面传递到另一个页面

使用 Post 方法将值传递到另一个页面

javascript使用Cookie将变量传递到另一个页面

将ID为的URL传递到由另一个URL调用的HTML页面

将数据传递到另一个页面

React Native:将变量传递到另一个页面

将错误消息JSON传递到另一个页面模板