ниже приведен код, и я хочу вызвать метод 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);
},
)
),
],
),
),
);
}
}