单击按钮后从方法中获取值后如何更改文本

时间:2019-10-10 07:14:10

标签: flutter dart

当我单击名为再次获取位置的按钮时,我得到了该值并将这些值分配给纬度和经度的全局变量,但是同时,我想更改纬度经度的文本字段,一旦我点击再次获取位置,因为一旦获得此页面,有时我就无法位置值。
 我怎么做?

 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);
                  },
                ),
              ),
            ],
          ),
        ),
      ),

    );
  }
}


3 个答案:

答案 0 :(得分:1)

使用setState():

 void getLocation() async {
    Position position = await Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    setState(() {
          longitude = position.longitude;
          latitude = position.latitude;
    });
  }

答案 1 :(得分:1)

使用setState方法,一旦加载LAT,LON,它将重新构建您的小部件。在getlocation方法中更新以下代码

setState(() {
          longitude = position.longitude;
          latitude = position.latitude;
    });

答案 2 :(得分:1)

* 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());
 }

快乐编码....:))