如何动态更改小部件类型?

问题描述 投票:0回答:1

我暂时尝试更改小部件类型,但它不起作用,我无法解决这个问题。 我的假设是,当我单击小部件时,它应该是可拖动的,并且在我不单击时应该是拖动目标。


          child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          GestureDetector(
              dragStartBehavior: DragStartBehavior.start,
              onVerticalDragStart: (d) {
                c.index.value = 1;
              },
              onVerticalDragUpdate: (d) {
                c.index.value = 1;
              },
              onVerticalDragEnd: (d) {
                c.index.value = 0;
              },
              child: Obx(
                () => IndexedStack(
                  index: c.index.value,
                  children: [
                    DragTarget(
                        onAccept: (data) {
                          print(data);
                        },
                        builder: (context, candidateData, rejectedData) =>
                            Container(
                              width: 40,
                              height: 40,
                              color: Colors.grey,
                            )),
                    Draggable(
                        data: "data",
                        childWhenDragging: Container(
                          width: 40,
                          height: 40,
                          color: Colors.white,
                        ),
                        child: Container(
                          width: 40,
                          height: 40,
                          color: Colors.amber,
                        ),
                        feedback: Container(
                          width: 40,
                          height: 40,
                          color: Colors.blue,
                        )),
                  ],
                ),
              ))
        ],
      )

flutter dart stack draggable
1个回答
0
投票

您可以使用条件语句。在你更新的代码中

Obx
包含条件语句。如果
c.index.value
为零,它返回一个
DragTarget
小部件,如果它是1,它返回一个
Draggable
一个:

Row(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    GestureDetector(
      dragStartBehavior: DragStartBehavior.start,
      onVerticalDragStart: (d) {
        c.index.value = 1;
      },
      onVerticalDragUpdate: (d) {
        c.index.value = 1;
      },
      onVerticalDragEnd: (d) {
        c.index.value = 0;
      },
      child: Obx(
        () {
          if (c.index.value == 0) {
            return DragTarget(
              onAccept: (data) {
                print(data);
              },
              builder: (context, candidateData, rejectedData) => Container(
                width: 40,
                height: 40,
                color: Colors.grey,
              ),
            );
          } else {
            return Draggable(
              data: "data",
              childWhenDragging: Container(
                width: 40,
                height: 40,
                color: Colors.white,
              ),
              child: Container(
                width: 40,
                height: 40,
                color: Colors.amber,
              ),
              feedback: Container(
                width: 40,
                height: 40,
                color: Colors.blue,
              ),
            );
          }
        },
      ),
    ),
  ],
),

快乐编码...

© www.soinside.com 2019 - 2024. All rights reserved.