Flutter - ReorderableListView does not work on my emulator

62 Views Asked by At

ReorderableListView works well when I run it on chrome. However, when I run it on an emulator - I cannot reorder my list.

Does anybody know how to fix this?

class _MyMainAppState extends State<MyMainApp>{
  late List<ValueKey> keys;

  @override
  void initState(){
    super.initState();
    keys = List.generate(10, (index) => ValueKey(index));
  }

  @override
  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: const Text('reorder list'),
      ),
      body: ReorderableListView(
        onReorder: (oldIndex, newIndex) {},
        children: List.generate(
          10,
              (index)=> ListTile(
            key: keys[index],
            title: Text("Name $index"),
            leading: const CircleAvatar(child: Icon(Icons.person)),
          ),
        ),
      ),
    );
  }
}
1

There are 1 best solutions below

0
fluttergogo On

To get ReorderableListView to work on the emulator. You need to add ReorderableDragStartListener widget.

body: ReorderableListView(
        onReorder: (oldIndex, newIndex) {
          dev.log("about to reset the reorder", name: "onReorder()");
          setState(() {
            if (newIndex > oldIndex) {
              newIndex -= 1;
            }
            dynamic element = keys.removeAt(oldIndex);
            keys.insert(newIndex, element);
          });
        },
        children: List.generate(
          10,
          (index) => ListTile(
            key: keys[index],

                title: ReorderableDragStartListener(
                  index: index,
                  child: Row(
                    children: <Widget>[