Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

flutter中Draggable的回调函数不立即生效

Draggable的回调函数想删除数组元素,执行后的打印确实是删掉了,但页面上还是维持原先的数量,直到下次拖拽时才更新视图;

而DragTarget的回调则是立马生效没有问题的;

这里是循环有几个不同的boxArr的,每个boxArr都是一个DragTarget,而boxArr里面的box是Draggable;

尝试了很久都不行,希望有人能帮忙解答下,不胜感激。

      List<Widget> d = <Widget>[];
      for (var i = 0; i < a.length; i++) {
        List<Widget> boxArr = <Widget>[];
        var times = double.parse(b[i]) / 0.5;
        var dataColor = Color.fromARGB(
          255,
          Random.secure().nextInt(255),
          Random.secure().nextInt(255),
          Random.secure().nextInt(255),
        );
        for(var i = 0; i < times; i++ ) {
          boxArr.add(
            Draggable(
              data: dataColor,
              child: Container(
                  key: ValueKey(i),
                  width: 16.0,
                  height: 16.0,
                  color: Colors.orange
              ),
              feedback: Container(
                  width: 16.0,
                  height: 16.0,
                  color: Colors.orange
              ),
              onDragCompleted: (){
                setState(() {
                  boxArr = boxArr.sublist(0, boxArr.length-1);
                });
                print(boxArr); //这里打印出来数组已经减少了
              },
            )
          );
        }

        d.add(
            new Row(
              key: ValueKey(prefs.getInt('tillHour')),
              children: <Widget>[
                DragTarget<Color>(
                  builder: (BuildContext context, List<Color> candidateData, List<dynamic> rejectedData) {
                    return Container(
                      width: 180.0,
                      child: Wrap(
                        children: boxArr, //但是这里没有变化,需得到下次拖拽时才更新已经减少的数组
                        spacing: 10,
                        runSpacing: 10,
                        alignment: WrapAlignment.start,
                        runAlignment: WrapAlignment.end,
                      ),
                      margin: EdgeInsets.fromLTRB(10, 10, 10, 10),
                    );
                  },
                  onWillAccept: (Color color) {
                    var isSame = color == dataColor;
                    return !isSame;
                  },
                  onAccept: (Color color) {
                    setState(() {
                      _dragData = color;
                    });
                    //这里的数组更新则是立马生效的
                    boxArr = [...boxArr, Draggable(
                      data: dataColor,
                      child: Container(
                        key: ValueKey(boxArr.length+1),
                        width: 16.0,
                        height: 16.0,
                        color: Colors.orange
                      ),
                      feedback: Container(
                        width: 16.0,
                        height: 16.0,
                        color: Colors.orange
                      ),
                    )];
                  },
                ),
              ],
            )
        );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...