ReorderableListView
https://api.flutter.dev/flutter/material/ReorderableListView-class.html
ReorderableListView class - material library - Dart API
A list whose items the user can interactively reorder by dragging. This sample shows by dragging the user can reorder the items of the list. The onReorder parameter is required and will be called when a child widget is dragged to a new position. link To cr
api.flutter.dev
This sample shows by dragging the user can reorder the items of the list.
The onReorder parameter is required and will be called when a child widget is dragged to a new position.
1. ReorderableListView
ReorderableListView()는 화면에서 아이템 순서를 재정렬하는 역할을 한다
이것을 사용하는 자체만으로 데이터에서 순서가 바뀌지는 않고
onReorder 파라미터를 이용해 데이터에서 순서를 바꿀 수 있다
onReorder: (int oldIndex, int newIndex) {
setState(
() {
//oldIndex와 newIndex 모두 이동되기 전에 산정!
if (oldIndex < newIndex) {
newIndex -= 1;
}
final int item = nums.removeAt(oldIndex);
nums.insert(newIndex, item);
},
);
},
2. ReorderableListView.builder
ReorderableListView.builder constructor - ReorderableListView - material library - Dart API
ReorderableListView.builder constructor const ReorderableListView.builder({Key? key, required IndexedWidgetBuilder itemBuilder, required int itemCount, required ReorderCallback onReorder, void onReorderStart(int index )?, void onReorderEnd(int index )?, do
api.flutter.dev
생성된 위젯 항목에서 재정렬 가능한 목록을 만든다
이 생성자는 실제로 표시되는 하위 항목에 대해서만 빌더가 호출된다
-> 하위 항목이 많은 목록 보기에 적합하다
콜백 itemBuilder는 0보다 크거나 같고 itemCount보다 작은 인덱스로만 호출된다
itemBuilder는 항상 null이 아닌 위젯을 반환해야 하며 호출 시 실제로 위젯 인스턴스를 생성해야 한다
이전에 구성된 위젯을 반환하는 빌더를 사용하지 말 것!
목록 보기의 하위 항목이 미리 생성되거나 ReorderableListView 자체가 생성 될 때 한꺼번에 생성되는 경우에는
ReorderableListView 생성자를 사용하는 것이 더 효율적이다
그러나 훨씬 더 효율적인 방법은 이 itemBuilder 생성자의 콜백을 사용해 요청 시 인스턴스를 생성하는 것이다