嵌套可重新排序的列表?

Posted

技术标签:

【中文标题】嵌套可重新排序的列表?【英文标题】:Nesting reorderable lists? 【发布时间】:2019-11-05 15:42:08 【问题描述】:

我有一个由ListTilesExpansionTiles 组成的ReorderableList,并且希望ExpansionTiles 的子代可以重新排序。孩子是递归的,因为它们也是ListTilesExpansionTiles 的混合体。这是我当前的代码:

Widget _buildList() 
  return Scrollbar(
    child: ReorderableListView(
      padding: const EdgeInsets.all(25),
      children: topLevelOrder.map<Widget>(_buildItem).toList(),
      onReorder: (oldIndex, newIndex) 
      // ...
      ,
    ),
  );


Widget _buildItem(String id) 
  Map item = data[id];
  List children = item["children"];
  if (children.length > 0) 
    List<Widget> childWidgets = [];
    for (var child in children)
      childWidgets.add(_buildItem(child));
    return ExpansionTile(
      key: Key(id),
      initiallyExpanded: true,
      title: Text(id),
      children: [
        ReorderableListView(
          children: childWidgets,
          onReorder: (oldIndex, newIndex) 
            // ...
          ,
        )
      ],
    );
  
  return ListTile(
    key: Key(id),
    title: Text(id)
  );

其中data 是测试数据的硬编码字典(只有两层深)。但是,这会引发 BoxConstraints forces an infinite height 错误。如何解决此问题或以其他方式实现嵌套的可重新排序列表?

【问题讨论】:

这个有什么更新吗? 【参考方案1】:

我尝试过类似的事情,但我只是没有看到它以这种方式工作,(也许它工作,我不确定),相反,我提供了多个 ReorderableListView(也可以是 ReorderableListView.builder)小部件的解决方案嵌套在 SingleChildScrollView 小部件中,也可以嵌套在其他小部件中,如您从示例中看到的那样。在我看来,这也适用于操作问题

玩得开心飞来飞去

导入'package:flutter/material.dart';

/// This is the main application widget.

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget 
  const MyStatefulWidget(Key? key) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();


/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> 
  final List<int> _items = List<int>.generate(10, (int index) => index);
  final List<int> _items2 = List<int>.generate(15, (int index2) => index2);

  @override
  Widget build(BuildContext context) 
    final ColorScheme colorScheme = Theme.of(context).colorScheme;
    final Color oddItemColor = colorScheme.primary.withOpacity(0.05);
    final Color evenItemColor = colorScheme.primary.withOpacity(0.15);
    int index = 0;
    int index2 = 0;

    return Scaffold(
      appBar: AppBar(
        title: Text('Example'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            ListTile(title: Text('test 1')),
            ListTile(title: Text('test 2')),
            ReorderableListView(
              physics: ClampingScrollPhysics(),
              shrinkWrap: true,
              padding: const EdgeInsets.symmetric(horizontal: 40),
              children: <Widget>[
                for (index = 0; index < _items.length; index++)
                  ListTile(
                    key: Key('$index'),
                    tileColor:
                        _items[index].isOdd ? oddItemColor : evenItemColor,
                    title: Text('Item $_items[index]'),
                  ),
              ],
              onReorder: (int oldIndex, int newIndex) 
                setState(() 
                  if (oldIndex < newIndex) 
                    newIndex -= 1;
                  
                  final int item = _items.removeAt(oldIndex);
                  _items.insert(newIndex, item);
                );
              ,
            ),
            ListTile(title: Text('test 3')),
            ListTile(title: Text('test 4')),
            ReorderableListView(
              physics: ClampingScrollPhysics(),
              shrinkWrap: true,
              padding: const EdgeInsets.symmetric(horizontal: 40),
              children: <Widget>[
                for (index2 = 0; index2 < _items2.length; index2++)
                  ListTile(
                    key: Key('$index2'),
                    tileColor:
                        _items2[index2].isOdd ? oddItemColor : evenItemColor,
                    title: Text('Item $_items2[index2]'),
                  ),
              ],
              onReorder: (int oldIndex2, int newIndex2) 
                setState(() 
                  if (oldIndex2 < newIndex2) 
                    newIndex2 -= 1;
                  
                  final int item = _items2.removeAt(oldIndex2);
                  _items2.insert(newIndex2, item);
                );
              ,
            ),
            ListTile(title: Text('test 5')),
            ListTile(title: Text('test 6')),
          ],
        ),
      ),
    );
  

【讨论】:

以上是关于嵌套可重新排序的列表?的主要内容,如果未能解决你的问题,请参考以下文章

嵌套可排序 AngularJS

嵌套对象的 RESTful 重新排序

是否可以在颤动的可重新排序的列表视图中禁用对单个项目的重新排序?

如果您已经有列表视图构建器,如何添加可重新排序的列表视图

拖放、可重新排序的列表的低级(无框架)示例?

如何使列表视图构建器在颤振中成为可重新排序的列表视图(优先任务应用程序(待办事项应用程序))