Answer the question
In order to leave comments, you need to log in
How to make Flutter ReorderableListView drag and drop several elements at once?
The list of "ReorderableListView" elements can be dragged in places one at a time, I also added the ability to select each one, how to make it so that the selected elements are dragged along with the dragged one?
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: MyApp(),
),
),
);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class ResJson {
final int id;
final String name;
ResJson(this.id, this.name);
}
class _MyAppState extends State<MyApp> {
// List<String> _list = ["Apple", "Ball", "Cat", "Dog", "Elephant"];
List<ResJson> _list = [
ResJson(1, "Apple"),
ResJson(2, "Ball"),
ResJson(3, "Cat"),
ResJson(4, "Dog"),
ResJson(5, "Elephant"),
];
Set<int> _listId = {};
@override
Widget build(BuildContext context) {
return ReorderableListView(
// создаем список виджетов на основе списка строк
children: _list
.map(
(item) => ListTile(
key: Key("${item.id}"),
title: Text(item.name),
onTap: () {
if (_listId.contains(item.id)) {
_listId.remove(item.id);
} else {
_listId.add(item.id);
}
print(_listId);
// перерисовка состояния
setState(() {});
},
tileColor: _listId.contains(item.id) ? Colors.grey : Colors.white,
),
)
.toList(),
// редактируем список строк и запускаем перерисовку
onReorder: (int start, int current) {
// перетаскивание сверху вниз
if (start < current) {
int end = current - 1;
ResJson startItem = _list[start];
int i = 0;
int local = start;
do {
_list[local] = _list[++local];
i++;
} while (i < end - start);
_list[end] = startItem;
}
// перетаскивание снизу вверх
else if (start > current) {
ResJson startItem = _list[start];
for (int i = start; i > current; i--) {
_list[i] = _list[i - 1];
}
_list[current] = startItem;
}
// перерисовка состояния
setState(() {});
},
);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question