Tec*_*lor 6 flutter flutter-layout
我有一个ReorderableList
由ListTiles
和组成ExpansionTiles
,并且希望 的子项ExpansionTiles
可以重新排序。在它们也是一个混合的孩子是递归ListTiles
和ExpansionTiles
。这是我当前的代码:
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)
);
}
Run Code Online (Sandbox Code Playgroud)
wheredata
是一个硬编码的测试数据字典(只有两层深)。但是,这会引发BoxConstraints forces an infinite height
错误。如何解决此问题或以其他方式实现嵌套的可重新排序列表?
Jan*_*Jan -1
我尝试过类似的事情,但我只是没有看到它以这种方式工作(也许它有效,我不确定),相反,我提供了解决方案,其中多个 ReorderableListView (也可以 ReorderableListView.builder)小部件嵌套在SingleChildScrollView 小部件,它也可以嵌套在其他小部件内,正如您从示例中看到的那样。在我看来,这对于操作问题也很有效
玩得开心 飘飘然
导入“包: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')),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
253 次 |
最近记录: |