【问题标题】:ReorderableListView.builder is not reorderReorderableListView.builder 没有重新排序
【发布时间】: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。例如:我需要将特定列表值拖到另一个位置,而不是内部列表。用一个可拖动的小部件包裹文本容器,但它的小部件没有被拖动。
  • 干得好。快乐编码

标签: flutter flutter-listview


【解决方案1】:

改变你的 setState

setState(() {
            if (newIndex > oldIndex) {
              newIndex -= 1;
            }
              final String item = _products.removeAt(oldIndex);
              list.insert(newIndex, item);

          });

【讨论】:

  • 还是不行
  • 更新了我的答案,因为从右到左你的条件也是假的
【解决方案2】:

编辑: 查看您添加的完整代码后,您似乎在小部件的构建方法的开头声明了列表。每次重建小部件时,列表都会重置为其初始值。

将列表声明移到类的开头:

class Cards extends StatefulWidget {
  const Cards({Key? key}) : super(key: key);
  List<String> list = ['1', '2', '3', '4', '5', '6', '7'];

编辑结束

问题出在这部分代码中:

if (oldIndex > newIndex) {
  newIndex = newIndex - 1;
  final String item = list.removeAt(oldIndex);
  list.insert(newIndex, item);
}

仅当项目从右向左拖动时才会更新列表。在执行此操作时,它会将新项目索引向左移动 1。

您可能想要做的是在每次拖动时更新列表。由于您将在添加新项目之前删除旧项目,因此您希望减少 newIndex 如果拖动是从左到右

代码应该是:

onReorder: (int oldIndex, int newIndex){
  setState(() {
    if (oldIndex < newIndex){
      newIndex--;
    }
    final String item = list.removeAt(oldIndex);
    list.insert(newIndex, item);
  });
}

【讨论】:

  • 不,它不起作用。不过,我有一个问题。
  • 您能否详细说明您的问题? “国家不重建”实际上没有任何意义。当我们不知道您想要实现什么重新排序时,“重新排序列表不起作用”。如果我提供的代码不能满足您的需求,会发生什么情况?
  • List list = ['1', '2', '3', '4', '5', '6', '7'];这是我的清单。我用容器包装重新排序列表生成器。 SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeLeft]);我的屏幕方向是横向的。
  • 我在顶部更新的完整代码。当我拖动时,列表会在 onReorder 中更新。但里面的itembuilder列表没有变化。
  • 共享代码时总是更容易发现问题。我用你的代码不起作用的原因更新了我的答案。
猜你喜欢
  • 2011-08-03
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
相关资源
最近更新 更多