Flutter Sortable Drag And Drop ListView

后端 未结 5 1056
旧巷少年郎
旧巷少年郎 2020-12-10 04:06

So I\'m starting to learn Flutter and would like to use a material design drag and drop list just like the one seen on the material guidelines website.

https://stora

5条回答
  •  独厮守ぢ
    2020-12-10 04:38

    You can use native flutter widget, ReorderableListView to achieve it, here is the example of doing it.

    List _list = ["Apple", "Ball", "Cat", "Dog", "Elephant"];
    
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(),
        body: ReorderableListView(
          children: _list.map((item) => ListTile(key: Key("${item}"), title: Text("${item}"), trailing: Icon(Icons.menu),)).toList(),
          onReorder: (int start, int current) {
            // dragging from top to bottom
            if (start < current) {
              int end = current - 1;
              String startItem = _list[start];
              int i = 0;
              int local = start;
              do {
                _list[local] = _list[++local];
                i++;
              } while (i < end - start);
              _list[end] = startItem;
            }
            // dragging from bottom to top
            else if (start > current) {
              String startItem = _list[start];
              for (int i = start; i > current; i--) {
                _list[i] = _list[i - 1];
              }
              _list[current] = startItem;
            }
            setState(() {});
          },
        ),
      );
    }
    

提交回复
热议问题