how to control the ReorderableListView from outside flutter?

94 views Asked by At

how I can control the elements of ReorderableListView in flutter from out side button for examble or can I simulate the animations of ReorderableListView to resort the list from external button?

class ListReorder extends StatefulWidget {
  const ListReorder({super.key});

  @override
  State<ListReorder> createState() => _ListReorderState();
}

class _ListReorderState extends State<ListReorder> {
  ScrollController scrollController = ScrollController();
  List<int> list = List.generate(20, (index) => index);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ReorderableListView.builder(
        scrollController: scrollController,
        buildDefaultDragHandles: true,
        itemBuilder: (context, index) {
          return Container(
            key: ValueKey(index),
            width: 360,
            height: 100,
            color: Colors.red,
            margin: EdgeInsets.all(8),
            alignment: Alignment.center,
            child: Text(
              list[index].toString(),
              style: TextStyle(color: Colors.white, fontSize: 25),
            ),
          );
        },
        itemCount: list.length,
        onReorder: (oldIndex, newIndex) {
          final element = list[oldIndex];
          list.remove(element);
          list.insert(newIndex, element);
          setState(() {});
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          //how I can control the elements from here to move it to special index

          // for example move element 6 to the index 2 with the same animation when I use the screen
        },
        child: Text(
          'Move',
          style: TextStyle(color: Colors.white, fontSize: 15),
        ),
      ),
    );
  }
}

is there a way to do the that with flutter framework?

0

There are 0 answers