(Flutter):如何使用DraggableScrollableSheet捕获拖动事件

时间:2020-06-29 19:53:09

标签: android flutter

Image of the App

嗨,我正在尝试在拖动DraggableScrollableSheet时调整窗口小部件(GoogleMap)的大小。这两个小部件在堆栈中。到目前为止,我只能使用GestureDetector(onVerticalDragStart,onVerticalDragDown,onPanDown)在拖动动作的开始和结束时捕获到拖动动作。我还可以使用GlobalKey(_globalKeyResultatRech)获得DraggableScrollableSheet的大小和位置。 有没有一种方法可以连续获取拖动事件,然后设置状态(更改GoogleMap的高度)并在拖动时重建视图?

谢谢。

以下是代码(简体):

           Stack(
            children: <Widget>[
              Container(
                height: //variable size to add later
                child: GoogleMap(
                  initialCameraPosition: _initialCamera,
                  mapToolbarEnabled: false,
                  onMapCreated: (GoogleMapController controller) {
                    controller.setMapStyle(
                    //map style
                    _mapController.complete(controller);
                    
                  },
                  onCameraMove: _onCameraMove,
                  markers: _markers,
                  polylines: _polylines,
                  circles: Set.of((circle != null) ? [circle] : []),
                ),
              ),
              SizedBox.expand(
                child: GestureDetector(
                        onPanDown: (panDowndetails) => {
                        print('onPanDown called')
                        },
                        onVerticalDragStart: (verticalDragStartdetails) => {
                        print('onVerticalDragStart called')
                        },
                        onVerticalDragDown: (VerticalDragDowndetails) {
                        print('onVerticalDragDown called');
                          },
                        child: SizedBox.expand(
                            child: DraggableScrollableSheet(
                                initialChildSize: 0.3,
                                minChildSize: 0.1,
                                maxChildSize: 0.5,
                                builder: (BuildContext c, s) {
                                  return Container(
                                      key: _globalKeyResultatRech,
                                      child: SingleChildScrollView(
                                          controller: s,
                                          child: ListView(
                                            controller: s,
                                            shrinkWrap: true,
                                            children: <Widget>[
                                              // Items 
                                            ],
                                          )));
                                })),
                      )
              ),
              // Positionned Item 1 (TextFiels)
              // Positionned Item 2 (Focus Button)
              // Positionned Item 3 (GPS Button)
            ],
          ), 
        ),

编辑: 没关系。在我的情况下,GestureDetector没有用。我发现一个内置的Flutter类NotificationListener作为参数<DraggableScrollableNotification>。这里是那些被打扰的人的代码:

(简化代码)

Stack(
            children: <Widget>[
              Container(
                height: //variable height to add later
                child: GoogleMap(
                  initialCameraPosition: _initialCamera,
                  mapToolbarEnabled: false,
                  onMapCreated: (GoogleMapController controller) {
                    controller.setMapStyle(
                    //map style
                    _mapController.complete(controller);
                    
                  },
                  onCameraMove: _onCameraMove,
                  markers: _markers,
                  polylines: _polylines,
                  circles: Set.of((circle != null) ? [circle] : []),
                ),
              ),
              SizedBox.expand(
                child: NotificationListener<DraggableScrollableNotification>(

                      onNotification: (notification){
                        //TODO
                        setState(() {
                          
                        });
                        return true;
                      },
                        child: SizedBox.expand(
                            child: DraggableScrollableSheet(
                                initialChildSize: 0.3,
                                minChildSize: 0.1,
                                maxChildSize: 0.5,
                                builder: (BuildContext c, s) {
                                  return Container(
                                      key: _globalKeyResultatRech,
                                      child: SingleChildScrollView(
                                          controller: s,
                                          child: ListView(
                                            controller: s,
                                            shrinkWrap: true,
                                            children: <Widget>[
                                              // Items 
                                            ],
                                          )));
                                })),
                      )
              ),
              // Positionned Item 1 (TextFiels)
              // Positionned Item 2 (Focus Button)
              // Positionned Item 3 (GPS Button)
            ],
          ), 
        ),

0 个答案:

没有答案