下面给出了代码,我想从飞镖文件getLocation()
中调用文件home.dart
的方法addLoc.dart
,具体地说,我想从以下位置调用getLocation()
的方法
Container(
height: 50,
padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
child: RaisedButton(
textColor: Colors.white,
color: Colors.blue,
child: Text('Use my current location'),
onPressed: () {
Navigator.pop(context);
},
)
),
位于文件addLoc.dart
中。
文件如下。
home.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void mapCreated(controller) {
setState(() {
_controller = controller;
});
}
List<Marker> allMarkers = [];
GoogleMapController _controller;
var pose;
@override
void initState() {
super.initState();
allMarkers.add(Marker(
markerId: MarkerId('mk1'),
draggable: false,
onTap: () {
print('Marker Tapped');
},
position: LatLng(21.2205, 72.8750)));
allMarkers.add(Marker(
markerId: MarkerId('mk2'),
draggable: false,
onTap: () {
print('Marker Tapped');
},
position: LatLng(21.1897, 72.8270)));
}
Future<void> getLocation() async{
final pose = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.best);
setState(() {
allMarkers.add(Marker(
markerId: MarkerId('myLoc'),
draggable: false,
onTap: () {
print('Marker Tapped');
},
position: LatLng(pose.latitude,pose.longitude)));
});
return pose;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Fire & Emergency'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.account_circle),
onPressed: () {
Navigator.pushNamed(context, '/login');
},
),
],
),
body: Stack(children: [
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: GoogleMap(
initialCameraPosition:
CameraPosition(target: LatLng(21.1702, 72.8311), zoom: 12.0),
markers: Set.from(allMarkers),
onMapCreated: mapCreated,
),
),
Container(
alignment: Alignment.bottomLeft,
padding: EdgeInsets.fromLTRB(10, 0, 0, 10),
child: FloatingActionButton(
heroTag: "btn1",
child: Tooltip(
message: 'add new location',
child: Icon(
Icons.gps_fixed,
color: Colors.grey[800],
),
),
backgroundColor: Colors.brown[50],
onPressed: () {
getLocation();
},
),
),
Container(
alignment: Alignment.bottomLeft,
padding: EdgeInsets.fromLTRB(10, 0, 0, 80),
child: FloatingActionButton(
heroTag: "btn2",
child: Tooltip(
message: 'add new location',
child: Icon(
Icons.location_on,
color: Colors.grey[800],
size: 30,
),
),
backgroundColor: Colors.brown[50],
onPressed: () {
Navigator.pushNamed(context, '/addLoc');
},
),
),
]),
);
}
}
addLoc.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'home.dart';
// ignore: camel_case_types
class addLoc extends StatefulWidget{
@override
_addLocState createState() => _addLocState();
}
// ignore: camel_case_types
class _addLocState extends State<addLoc> {
MyHomePage mhp=new MyHomePage();
TextEditingController LongCont = TextEditingController();
TextEditingController LattCont = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Fire & Emergency'),
),
body:Container(
child: ListView(
children: <Widget>[
Container(
alignment: Alignment.center,
padding: EdgeInsets.all(10),
child: Text(
'Add New Location',
style: TextStyle(fontSize: 20,color: Colors.blue),
)
),
Container(
padding: EdgeInsets.all(10),
child: TextField(
controller: LongCont,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Longitude',
),
),
),
Container(
padding: EdgeInsets.all(10),
child: TextField(
controller: LattCont,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Lattitude',
),
),
),
Container(
height: 50,
padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
child: RaisedButton(
textColor: Colors.white,
color: Colors.blue,
child: Text('Add New'),
onPressed: () {
Navigator.pop(context);
},
)
),
Container(
alignment: Alignment.center,
padding: EdgeInsets.all(10),
child: Text(
'Or',
style: TextStyle(fontSize: 15,color: Colors.blue),
)
),
Container(
height: 50,
padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
child: RaisedButton(
textColor: Colors.white,
color: Colors.blue,
child: Text('Use my current location'),
onPressed: () {
Navigator.pop(context);
},
)
),
],
),
),
);
}
}
答案 0 :(得分:0)
看,可以满足需求的方法有n种,但最重要的是编写代码以达到目标的效率。
为了利用distanceFromCoordinatesInKilometers
中的getLocation()
方法,而不是在home.dart
本身中定义方法,请先 home.dart
将文件导入到您想要的任何页面
helper.dart
make a separate file, call it helper.dart
然后导入文件,并在任何需要的地方使用它,如下所示:
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class Helper{
// If you are returning something out of the future class, try giving a
// dynamic if you are confused of the datatype it returns
static Future<dynamic> getLocation() async{
final pose = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.best);
return pose;
}
}
请阅读有关如何使用Future Class的详细信息。如果您想进一步澄清。
值不过是您的
import helper.dart // This is how you call the method from Helper class Helpers.getLocation().then((value){ //your operation setState(() { allMarkers.add(Marker( markerId: MarkerId('myLoc'), draggable: false, onTap: () { print('Marker Tapped'); }, position: LatLng(value.latitude,value.longitude))); }); });
方法返回的值。通过getLocation()
调用Future方法,因为它首先执行操作,然后返回值
如果您不了解概念或遇到的任何问题,请告诉我。学习愉快