Reputation: 25
As soon as I click the button called GET LOCATION AGAIN, I get the value and assign those values to the global variable of latitude and longitude, but at the same time, i want to change the text field of latitude and longitude once I click the Get LOCATION AGAIN BUTTON because as soon as I get this page, sometimes I can't get the location values.
How do I do that?
void getLocation() async {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
longitude = position.longitude;
latitude = position.latitude;
print("latitude :" + latitude.toString());
print("longitude :" + longitude.toString());
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
backgroundColor: Colors.amber,
title: Text("Location Sender"),
actions: <Widget>[
Padding(
child: Icon(Icons.search),
padding: const EdgeInsets.only(right: 10.0),
)
],
),
body: new Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
"ID: ${widget.id}",
style: Theme.of(context).textTheme.title,
),
new Text(
"DESCRIPTION: ${widget.desc}",
style: Theme.of(context).textTheme.title,
),
new Text(
"LATITUDE: ${latitude != null ? latitude.toString() : '0'}",
style: Theme.of(context).textTheme.title,
),
new Text(
"LONGITUDE: ${longitude != null ? longitude.toString() : '0'}",
style: Theme.of(context).textTheme.title,
),
SizedBox(
height: 150,
),
new ListTile(
title: new RaisedButton(
padding: EdgeInsets.all(25),
textTheme: ButtonTextTheme.primary,
color: Colors.amber,
child: new Text("GET LOCATION AGAIN"),
onPressed: () {
getLocation();
},
),
),
new ListTile(
title: new RaisedButton(
padding: EdgeInsets.all(25),
textTheme: ButtonTextTheme.primary,
color: Colors.amber,
child: new Text("POST LOCATION"),
onPressed: () {
sendRequest(id, description, latitude, longitude);
},
),
),
],
),
),
),
);
}
}
Upvotes: 0
Views: 95
Reputation: 2054
* use setState((){}) .. it's recreate the build(context).
* Your class should be extends StatefulWidget not StatelessWidget
void getLocation() async {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
setState((){
longitude = position.longitude;
latitude = position.latitude;
});
print("latitude :" + latitude.toString());
print("longitude :" + longitude.toString());
}
Happy Coding.... :))
Upvotes: 1
Reputation: 4027
Use setState method, it will rebuild your widget once LAT,LON is loaded.. update below code inside getlocation method
setState(() {
longitude = position.longitude;
latitude = position.latitude;
});
Upvotes: 1
Reputation: 510
use setState():
void getLocation() async {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
setState(() {
longitude = position.longitude;
latitude = position.latitude;
});
}
Upvotes: 1