flutter 如何动态更改widget类型?

2j4z5cfb  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(173)

我试图改变小部件类型一会儿,它不工作,我不能解决这个问题。我的假设是,当我点击小部件它应该是可拖动的,它应该是拖动目标,而我不点击。

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,
                        )
                    ),
                ],
            ),
        ))
    ],
)
xjreopfe

xjreopfe1#

你可以使用条件语句。在你更新的代码中,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,
              ),
            );
          }
        },
      ),
    ),
  ],
),

快乐编码...

相关问题