【发布时间】:2021-08-10 02:53:15
【问题描述】:
我是 flutter_bloc 的新手,我正在尝试实现 ReorderableListView,以允许用户重新排列列表视图中文本字段的顺序。
不幸的是,它不起作用。将 listtile 放到新位置后,它会变回旧位置:
这是屏幕中的输出:
class _Levels extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<DifficultyLevelsCubit, DifficultyLevelsState>(
builder: (context, state) {
print('rebuild');
return Flexible(
child: ReorderableListView(
shrinkWrap: true,
children: state.levels
.asMap()
.map((i, lvl) => MapEntry(
i,
ListTile(
key: ValueKey(i),
title: TextField(
onChanged: (lvl) => context
.read<DifficultyLevelsCubit>()
.levelChanged(lvl, i),
decoration: InputDecoration(
labelText: 'Level',
helperText: '',
errorText: state.levels[i].invalid
? 'invalid level'
: null,
),
),
trailing: Icon(Icons.menu),
),
))
.values
.toList(),
onReorder: (int oldIndex, int newIndex) => context
.read<DifficultyLevelsCubit>()
.orderChanged(oldIndex, newIndex),
),
);
},
);
}
}
这是我在cubit 类中实现的重新排序功能:
void orderChanged(int oldIndex, int newIndex) {
List<DiffLevel> _list = state.levels;
if (newIndex > oldIndex) {
newIndex -= 1;
}
final items = _list.removeAt(oldIndex);
_list.insert(newIndex, items);
emit(state.copyWith(levels: _list));
}
有人知道为什么会出现这种行为以及如何解决这个问题吗?提前致谢!
注意:print('rebuild)` 不会在重新排序时触发。我真的不明白为什么,因为我在 reorder 函数中发出状态并期望 blocbuilder 对该更改做出反应。
【问题讨论】:
标签: flutter listview dart flutter-bloc