美文网首页
flutter之ReorderableListView

flutter之ReorderableListView

作者: wwq2020 | 来源:发表于2020-07-19 22:11 被阅读0次

    背景

    ListView本身没办法进行移动ListTile,所以使用ReorderableListView

    例子

    class Demo extends StatefulWidget {
      @override
      _DemoState createState() => _DemoState();
    }
    
    class _DemoState extends State<Demo> {
      List<int> items = [];
    
      @override
      void initState() {
        super.initState();
        for (int i = 0; i < 10; i++) {
          items.add(i);
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return ReorderableListView(
            children: items.map(toWidget).toList(),
            header: Text('header'),
            onReorder: (oldIndex, newIndex) {
              setState(() {
                switchItem(oldIndex, newIndex);
              });
            });
      }
    
      void switchItem(int oldIndex, int newIndex) {
        tmp = list[oldIndex];
        list[oldIndex] = list[newIndex];
        list[newIndex] = tmp;
      }
    
      Widget f(int i) {
        return ListTile(
          key: ValueKey("item $i"),
          title: Text('item $i'),
        );
      }
    }
    
    

    相关文章

      网友评论

          本文标题:flutter之ReorderableListView

          本文链接:https://www.haomeiwen.com/subject/oitdkktx.html