`我正在处理一个Listview,我想重新排序,所以我找到了ReorderableListView.builder,我正在通过官方Flutter文档使用它:https://api.flutter.dev/flutter/material/ReorderableListView/ReorderableListView.builder.html
但我无法重新订购。
分享我写的代码Sniffy:
`import 'package:flutter/material.dart';
class PositionInterChange extends StatefulWidget {
const PositionInterChange({super.key});
@override
State<PositionInterChange> createState() => _PositionInterChangeState();
}
class _PositionInterChangeState extends State<PositionInterChange> {
final List<int> _items = List.generate(50, (index) => index);
@override
Widget build(BuildContext context) {
final ColorScheme colorScheme = Theme.of(context).colorScheme;
final oddItemColor = colorScheme.primary.withOpacity(0.15);
final evenItemColor = colorScheme.primary.withOpacity(0.30);
return Scaffold(
body: SafeArea(
child: ReorderableListView.builder(
itemCount: _items.length,
buildDefaultDragHandles: false,
itemBuilder: (BuildContext context, int index) {
return ListTile(
key: Key('$index'),
tileColor:
_items[index].isOdd ? oddItemColor : evenItemColor,
title: Text('Item ${_items[index]}', style: TextStyle(
color: Colors.white
),),
);
},
onReorder: (int oldIndex, int newIndex) {
if (newIndex > oldIndex) {
setState(() {
newIndex -= 1;
});
}
final int item = _items.removeAt(oldIndex);
_items.insert(newIndex, item);
}),
),
);
}
}
字符串
显示我的main.dart的实现
import 'package:flutter/material.dart';
import 'package:position_interchange/screens/position_interchange.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple ,),
useMaterial3: true,
),
home: PositionInterChange(),
);
}
}
型
我期待一个适当的Reoder讨论在Flutter文档,上传截图我得到什么. enter image description here `
1条答案
按热度按时间72qzrwbm1#
将
ListTile
-key: Key('$index')
更改为key: ValueKey(index)