ReorderAbleListView.builder()未在用户输入上重新排序

发布于 2025-02-09 03:13:56 字数 2459 浏览 3 评论 0 原文

我对 reoderablelistview.builder()的期望简直就是Flutter Doc所说的,可以创建一个允许用户在列表视图上下移动/拖动列表项目的小部件。但是,我从仿真器中获得的不是拖动动画,也没有重新排序列表(用户输入),甚至没有呼叫 onreorder callback。

我尝试过的东西:

  1. 确保我的 taskId taskName 列表列表的长度
  2. ItemBuilder OnReorder >回调,出乎意料的是,仅从 itembuilder 回调的回调
  3. 并粘贴了窗口小部件代码及其相应的列表数据,将数据完全添加到其他小部件类(或文件),并且仍然获得完全相同的结果
  4. valuekey text()中的列表视图中。
  5. 尝试使用与 text()呈现的相同列表数据, tasknames ,用于值 valuekey

我唯一没有尝试的是直接将此小部件的官方示例复制到我的代码库中,但是我所拥有的测试代码应该与结构上的官方示例非常相似。

清单。dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'add_task.dart';

class Checklist extends StatefulWidget {
  const Checklist({Key? key}) : super(key: key);

  @override
  State<Checklist> createState() => _ChecklistState();
}

class _ChecklistState extends State<Checklist> {
  final List<int> taskID = <int>[0, 1, 2, 4, 6];
  final List<String> taskNames = <String>['A', 'B', 'C', 'D', 'E'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          children: [
            const Text("TODO"),
            ElevatedButton(
              onPressed: () {

              },
              child: const Text("Google Calendar"),
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.black12),
              ),
            ),
          ]
        ),
      ),
      body: ReorderableListView.builder(
        itemCount: taskNames.length,
        itemBuilder: (BuildContext context, int index) {
          print("B");
          return ListTile(
            key: ValueKey(taskID[index]),
            tileColor: Colors.black12,
            title: Text('Entry ${taskNames[index]}')
          );
        },
        onReorder: (int oldIndex, int newIndex) {
          print("A");
          setState(() {
            if (newIndex > oldIndex) {
              newIndex -= 1;
            }
            final int elTid = taskID.removeAt(oldIndex);
            final String elTnm = taskNames.removeAt(oldIndex);
            taskID.insert(newIndex, elTid);
            taskNames.insert(newIndex, elTnm);
          });
        },
      ),
    );
  }
}

My expectation from ReoderableListView.builder() is simply, as what the Flutter doc says, to create a widget that allows users to move/drag list-items up and down the list view. However, what I was getting from my emulator was no dragging animation, no reordering of the list (upon user input), and not even call to the onReorder callback.

Stuff I have tried:

  1. Made sure my taskID and taskName lists have the same length
  2. Added debug outputs for itemBuilder and onReorder callback, surprisingly receiving debug output only from itemBuilder callback
  3. Copied and pasted the widget code and its corresponding lists data exactly to other widget classes (or files) and still got the same result
  4. Added the exactly same ValueKey in the Text() inside the list-view.
  5. Tried using the same list data as what the Text() is rendering, taskNames, for the value ValueKey

The only thing I did not try was directly copying and pasting the official example of this widget to my codebase, but the test code I have should already be very similar to the official example, structurally.

checklist.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'add_task.dart';

class Checklist extends StatefulWidget {
  const Checklist({Key? key}) : super(key: key);

  @override
  State<Checklist> createState() => _ChecklistState();
}

class _ChecklistState extends State<Checklist> {
  final List<int> taskID = <int>[0, 1, 2, 4, 6];
  final List<String> taskNames = <String>['A', 'B', 'C', 'D', 'E'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          children: [
            const Text("TODO"),
            ElevatedButton(
              onPressed: () {

              },
              child: const Text("Google Calendar"),
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.black12),
              ),
            ),
          ]
        ),
      ),
      body: ReorderableListView.builder(
        itemCount: taskNames.length,
        itemBuilder: (BuildContext context, int index) {
          print("B");
          return ListTile(
            key: ValueKey(taskID[index]),
            tileColor: Colors.black12,
            title: Text('Entry ${taskNames[index]}')
          );
        },
        onReorder: (int oldIndex, int newIndex) {
          print("A");
          setState(() {
            if (newIndex > oldIndex) {
              newIndex -= 1;
            }
            final int elTid = taskID.removeAt(oldIndex);
            final String elTnm = taskNames.removeAt(oldIndex);
            taskID.insert(newIndex, elTid);
            taskNames.insert(newIndex, elTnm);
          });
        },
      ),
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2025-02-16 03:13:56

您的代码确实有效。它仅在长时间拖动上移动。不在正常的阻力上..在正常的阻力上,它会倾向于滚动列表

Your code does work. It moves only on long press and drag. Not on normal drag.. On normal drag it will tend to scroll the list
preview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文