【发布时间】:2019-10-10 07:14:10
【问题描述】:
只要我单击名为 GET LOCATION AGAIN 的按钮,我就会获取值并将这些值分配给纬度和经度的全局变量,但同时,我想更改latitude 和 longitude 的文本字段,一旦我单击 Get LOCATION AGAIN BUTTON,因为一旦我获得此页面,有时我无法获得位置值。
我怎么做?
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);
},
),
),
],
),
),
),
);
}
}
【问题讨论】:
-
使用 setState 方法,它会在加载 lat long 后重建您的小部件