【问题标题】:how can I change the text after getting the value from the method after clicking the button单击按钮后从方法中获取值后如何更改文本
【发布时间】:2019-10-10 07:14:10
【问题描述】:

只要我单击名为 GET LOCATION AGAIN 的按钮,我就会获取值并将这些值分配给纬度和经度的全局变量,但同时,我想更改latitudelongitude 的文本字段,一旦我单击 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 后重建您的小部件

标签: flutter dart


【解决方案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());
 }

快乐编码.... :))

【讨论】:

    【解决方案2】:

    使用 setState():

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2022-12-04
        • 2021-12-08
        • 1970-01-01
        • 2016-02-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        相关资源
        最近更新 更多