【发布时间】:2021-11-24 20:16:29
【问题描述】:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Mainpage extends StatefulWidget {
const Mainpage({Key? key}) : super(key: key);
@override
_MainpageState createState() => _MainpageState();
}
class _MainpageState extends State<Mainpage> {
@override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
}
@override
Widget build(BuildContext context) {
return Container(color: Colors.white, child: const Cards());
}
}
class Cards extends StatefulWidget {
const Cards({Key? key}) : super(key: key);
@override
State<Cards> createState() => _CardsState();
}
class _CardsState extends State<Cards> {
@override
Widget build(BuildContext context) {
List<String> list = [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
];
return Container(
color: Colors.white,
child: ReorderableListView.builder(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
scrollDirection: Axis.horizontal,
onReorder: (int oldIndex, int newIndex) {
setState(() {
if (oldIndex < newIndex) {
newIndex--;
}
final String item = list.removeAt(oldIndex);
list.insert(newIndex, item);
});
},
itemBuilder: (BuildContext context, int index) {
return Card(
key: ValueKey(index),
child: Container(
height: MediaQuery.of(context).size.height / 4,
width: MediaQuery.of(context).size.width / 19,
alignment: Alignment.bottomCenter,
child: Text(list[index]),
),
);
},
itemCount: list.length,
));
}
}
当我拖动时,列表值会得到更新。但国家并没有重建。列表值已正确重新排序。但是重新排序的列表不起作用。我尝试使用 ReorderableListView.builder 和 ReorderableListView。这是我的完整代码。我不知道我犯错的地方。这是我的完整代码。有人帮我解决这个问题。
【问题讨论】:
-
在构建上下文之外声明你的列表。并按照答案。
-
它的工作谢谢...但我需要同时使用 reorderlistview.builder 和 draggable。例如:我需要将特定列表值拖到另一个位置,而不是内部列表。用一个可拖动的小部件包裹文本容器,但它的小部件没有被拖动。
-
干得好。快乐编码