Flutter:文本仅在点击按钮两次后更新
Flutter: text only updates after button is tapped twice
我刚开始在 Flutter 中构建一个天气应用程序,我 运行 遇到了一个问题,即获取用户位置并将其显示为文本小部件需要轻按 2 次 FlatButton。我想做到这一点,以便用户只需点击 FlatButton 位置一次,文本就会更新到他们当前的位置。这是我的代码(我已经删除了不必要的部分 + 样板代码):
class _ClimaState extends State<Clima> {
Position position;
List<Placemark> placemark;
String location;
void getLocation() async {
position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.lowest);
placemark = await Geolocator()
.placemarkFromCoordinates(position.latitude, position.longitude);
location = placemark[0].locality + ", " + placemark[0].country;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
//I've left out the other children for now
//This is where I want the user's location to be displayed after they tap the location icon
Text(
placemark != null ? location : "Tap the location icon...",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w800,
),
//This is the location icon
Padding(
padding: const EdgeInsets.only(top: 100.0),
child: FlatButton(
child: Icon(
Icons.my_location,
color: Colors.white,
),
color: Colors.transparent,
onPressed: () {
setState(() {
getLocation();
});
},
),
),
),
],
),
),
),
),
);
}
}
我想我通过按你现在的方式调用 set state 解决了你的问题在你重建之前你没有等待你的 Geolocator
数据到 return 这就是为什么需要两次点击才能得到要显示的数据。试试这个设置
Future<void> getLocation() async {
position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.lowest);
placemark = await Geolocator()
.placemarkFromCoordinates(position.latitude, position.longitude);
location = placemark[0].locality + ", " + placemark[0].country;
setState(() {});
}
//then the onPressed function in your padding widget should be
onPressed: () async {
await getLocation();
},
我刚开始在 Flutter 中构建一个天气应用程序,我 运行 遇到了一个问题,即获取用户位置并将其显示为文本小部件需要轻按 2 次 FlatButton。我想做到这一点,以便用户只需点击 FlatButton 位置一次,文本就会更新到他们当前的位置。这是我的代码(我已经删除了不必要的部分 + 样板代码):
class _ClimaState extends State<Clima> {
Position position;
List<Placemark> placemark;
String location;
void getLocation() async {
position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.lowest);
placemark = await Geolocator()
.placemarkFromCoordinates(position.latitude, position.longitude);
location = placemark[0].locality + ", " + placemark[0].country;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
//I've left out the other children for now
//This is where I want the user's location to be displayed after they tap the location icon
Text(
placemark != null ? location : "Tap the location icon...",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w800,
),
//This is the location icon
Padding(
padding: const EdgeInsets.only(top: 100.0),
child: FlatButton(
child: Icon(
Icons.my_location,
color: Colors.white,
),
color: Colors.transparent,
onPressed: () {
setState(() {
getLocation();
});
},
),
),
),
],
),
),
),
),
);
}
}
我想我通过按你现在的方式调用 set state 解决了你的问题在你重建之前你没有等待你的 Geolocator
数据到 return 这就是为什么需要两次点击才能得到要显示的数据。试试这个设置
Future<void> getLocation() async {
position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.lowest);
placemark = await Geolocator()
.placemarkFromCoordinates(position.latitude, position.longitude);
location = placemark[0].locality + ", " + placemark[0].country;
setState(() {});
}
//then the onPressed function in your padding widget should be
onPressed: () async {
await getLocation();
},