Flutter / Draggable 드래그해서 값 변경하기

에러전문·2022년 9월 27일
0

Draggable class - widgets library - Dart API

Stateful 위젯이며 해당값들을 변환시킬때 유용하다.

ex)장바구니 담기/쓰레기통으로 이동 등과같은 단순하게 사용할수있지만 유용한것들

Flutter docs 찾아보면 재밌는 class가 많은것같다.

Row(children: [
            Draggable<int>(
              // Data is the value this Draggable stores.
              data: 50,
              feedback: Container(
                color: Colors.blue,
                height: 100,
                width: 100,
                child: const Icon(Icons.directions_run),
              ),
              childWhenDragging: Container(
                height: 100.0,
                width: 100.0,
                color: Colors.pinkAccent,
                child: const Center(
                  child: Text('이동중'),
                ),
              ),
              child: Container(
                height: 100.0,
                width: 100.0,
                color: Colors.lightGreenAccent,
                child: const Center(
                  child: Text('추가하기'),
                ),
              ),
            ),
            const Spacer(),
            DragTarget<int>(
              builder: (
                BuildContext context,
                List<dynamic> accepted,
                List<dynamic> rejected,
              ) {
                return Container(
                  height: 100.0,
                  width: 100.0,
                  color: Colors.cyan,
                  child: Center(
                    child: Text('현재 값:  ${free}!'),
                  ),
                );
              },
              onAccept: (int data) {
                setState(() {
                  free += data;
                });
              },
            )
          ]),

해당 위젯을 드래그하여 이동할경우 data값만큼 증가하는것이다.

profile
초보자입니다. 많은지적 감사합니다.

0개의 댓글