在创建状态或设置状态时,连续按下颤振按钮的按下功能

时间:2019-05-19 07:25:43

标签: flutter setstate

我正在使用Google Maps插件,并且有一个按钮可以在地图上添加标记。

问题是当屏幕首次加载按钮功能(_onAddMarkerButtonPressed)时,立即调用该功能并将其放置在地图上。我猜这是来自createSate()方法。

我还有一个textview,每当地图移动(_onCameraMove)时,该视图就会使用地图上的新位置地址进行更新。更改该文本小部件中显示的String的值后,这将调用setState()。但是,这不仅是更新文本,它还调用_onAddMarkerButtonPressed并在每次相机在地图上移动时添加新的标记。所以我最后要筛选一系列标记。

感谢您的帮助。

 class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  Completer<GoogleMapController> _controller = Completer();
  static const LatLng _center  = const LatLng(-31.950260, 115.851840);
  final Set<Marker> _markers = {};
  LatLng _lastMapPosition = _center;

  @override
  Widget build(BuildContext context) {

    double mapWidth = MediaQuery.of(context).size.width;
    double mapHeight = MediaQuery.of(context).size.height - 215;
    double iconSize = 30.0;

    return Scaffold(
      appBar: AppBar(

        title: Text(widget.title),
      ),
      body: Stack(
        alignment: Alignment(0.0, 0.0),
        children: <Widget>[
          GoogleMap(
            onMapCreated: _onMapCreated,
            initialCameraPosition: CameraPosition(target: _center, zoom: 16.0),
            mapType: MapType.normal,
            markers: _markers,
            onCameraMove: _onCameraMove,
          ),

          Positioned(
            top: 0,
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Container(
                  decoration: BoxDecoration( borderRadius: BorderRadius.circular(10.0), color: Colors.white),
                  height: 75,
                  width: mapWidth,
                  child: Center(
                      child: Text(address))),
            ),
          ),

         Positioned(
          top: (mapHeight - iconSize)/ 2,
          right: (mapWidth - iconSize)/ 2,
          child: Icon(Icons.my_location, size: iconSize, color: Colors.black87,),
        ),

         Positioned(
           bottom: 24.0,
           left: 24.0,
           child: Icon(Icons.search, size: 40, color: Colors.blue,) ,
        ),

          Positioned(
            bottom: 24.0 - (42/2),
            right: (mapWidth - 150)/ 2,
            child: RoundedButton(title: 'Add Alert', color: Colors.lightBlueAccent, onPressed: _onAddMarkerButtonPressed()),
          ),

        Positioned(
          bottom: 24.00,
          right: 24.0,
          child: Icon(Icons.my_location, size: 40, color: Colors.blue,) ,
        ),
      ],
    ),
  );
}


  _onAddMarkerButtonPressed (){
    setState(() {
      _markers.add(Marker(
          markerId: MarkerId(_lastMapPosition.toString()),
          position: _lastMapPosition,
          infoWindow: InfoWindow(
            title: 'This is the title',
            snippet: 'This is the snippet',
          ),
          icon: BitmapDescriptor.defaultMarker,
        ),
      );
    });
  }

  _onCameraMove(CameraPosition position) async{
    _lastMapPosition = position.target;
    final coordinates = new Coordinates(position.target.latitude, position.target.longitude);
    var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
    var first = addresses.first;

    setState(() {
      address = "${first.addressLine}";
    });
  }

1 个答案:

答案 0 :(得分:1)

您在此行的错误 onPressed: _onAddMarkerButtonPressed()

因为只要将此小部件呈现到视图中,您就可以执行函数

您必须将其传递给这样的箭头函数

onPressed: () => _onAddMarkerButtonPressed()