Home > Back-end >  How to Disable ReorderableListView Reorder capability but still allow scrolling?
How to Disable ReorderableListView Reorder capability but still allow scrolling?

Time:10-08

How do we deactivate the ReorderableListView dragging/reordering capabilities in Flutter?

I would like to disable the dragging function but still allow scrolling.

Any ideas? Thank you

CodePudding user response:

You could wrap your list item in an IgnorePointer widget and control with a bool, like this

class ReorderableList extends StatefulWidget {
  @override
  _ReorderableListState createState() => _ReorderableListState();
}

class _ReorderableListState extends State<ReorderableList> {
  bool _allowReorder = true;

  final List<int> _items = List<int>.generate(50, (int index) => index);

  void reorderData(int oldindex, int newindex) {
    setState(() {
      if (oldindex < newindex) {
        newindex -= 1;
      }
      final int item = _items.removeAt(oldindex);
      _items.insert(newindex, item);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(onPressed: () {
        setState(() {
          _allowReorder = !_allowReorder;
        });
      }),
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: ReorderableListView(
                onReorder: (one, two) => reorderData(one, two),
                children: <Widget>[
                  for (int index = 0; index < _items.length; index  )
                    if (_allowReorder)
                      ListTile(
                        key: Key('$index'),
                        title: Text('Item ${_items[index]}'),
                      )
                    else
                      IgnorePointer(
                        key: Key('$index'),
                        child: ListTile(
                          title: Text('Item ${_items[index]}'),
                        ),
                      )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

You can activate it using the property onReorder property like:

ReorderableListView(
        children: <Widget>[
          for(final items in widget.item)
            Card(
              color: Colors.blueGrey,
              key: ValueKey(items),
              elevation: 2,
              child: ListTile(
                title: Text(items),
                leading: Icon(Icons.work,color: Colors.black,),
              ),
            ),
        ],
        onReorder: reorderData,

      ),
    );

    void reorderData(int oldindex, int newindex){
      setState(() {
        if(newindex>oldindex){
          newindex-=1;
        }
        final items =widget.item.removeAt(oldindex);
        widget.item.insert(newindex, items);
      });

}
  • Related