Skip to content

ReorderableListView Weird Behaviour on reordering the second last child #90856

Closed

Description

@Mayank-9018

Steps to Reproduce

  1. Run this Code:
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> items = List.generate(5, (index) => 'Tile $index');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        body: ReorderableListView.builder(
            buildDefaultDragHandles: false,
            itemBuilder: (context, index) {
              return ListTile(
                trailing: ReorderableDragStartListener(
                  index: index,
                  child: const Icon(Icons.drag_indicator),
                ),
                key: Key('$index'),
                title: Text(items[index]),
              );
            },
            itemCount: items.length,
            onReorder: (int oldIndex, int newIndex) {
              setState(() {
                if (oldIndex < newIndex) {
                  newIndex -= 1;
                }
                final item = items.removeAt(oldIndex);
                items.insert(newIndex, item);
              });
            }));
  }
}

Expected results:
It should behave like all the children.

Actual results:

R.mp4

The Issue is not platform-specific.

Details
Analyzing reorderablelistview_bug...                                    
No issues found! (ran in 1.5s)
[✓] Flutter (Channel stable, 2.5.0, on Ubuntu 21.04 5.11.0-16-generic, locale en_IN)
    • Flutter version 2.5.0 at /home/mayank/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4cc385b4b8 (3 weeks ago), 2021-09-07 23:01:49 -0700
    • Engine revision f0826da7ef
    • Dart version 2.14.0

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /home/mayank/Android/Sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /usr/local/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 12.0.0-3ubuntu1~21.04.1
    • cmake version 3.18.4
    • ninja version 1.10.1
    • pkg-config version 0.29.2

[✓] Android Studio (version 2020.3)
    • Android Studio at /usr/local/android-studio
    • Flutter plugin version 60.1.2
    • Dart plugin version 203.8292
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] VS Code (version 1.60.2)
    • VS Code at /usr/share/code
    • Flutter extension version 3.26.0

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 21.04 5.11.0-16-generic
    • Chrome (web)    • chrome • web-javascript • Google Chrome 93.0.4577.63

• No issues found!

Activity

  1. danagbemava-nc commented on Sep 28, 2021

    @danagbemava-nc
    Member

    Issue is reproducible on stable and master.

    See attached video.

    flutter doctor -v
    [✓] Flutter (Channel stable, 2.5.1, on macOS 11.5.1 20G80 darwin-arm, locale en-GB)
        • Flutter version 2.5.1 at /Users/nexus/dev/sdks/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision ffb2ecea52 (11 days ago), 2021-09-17 15:26:33 -0400
        • Engine revision b3af521a05
        • Dart version 2.14.2
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/nexus/Library/Android/sdk
        • Platform android-31, build-tools 31.0.0
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.5.1, Build version 12E507
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2020.3)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    
    [✓] VS Code (version 1.60.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.26.0
    
    [✓] Connected device (3 available)
        • M2007J20CG (mobile) • 5dd3be00 • android-arm64  • Android 11 (API 30)
        • macOS (desktop)     • macos    • darwin-arm64   • macOS 11.5.1 20G80 darwin-arm
        • Chrome (web)        • chrome   • web-javascript • Google Chrome 94.0.4606.61
    
    • No issues found!
    [✓] Flutter (Channel master, 2.6.0-12.0.pre.115, on macOS 11.5.1 20G80 darwin-arm, locale en-GB)
        • Flutter version 2.6.0-12.0.pre.115 at /Users/nexus/dev/sdks/flutters
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 7c505894a1 (6 hours ago), 2021-09-28 00:58:02 -0400
        • Engine revision 933661d3e9
        • Dart version 2.15.0 (build 2.15.0-153.0.dev)
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/nexus/Library/Android/sdk
        • Platform android-31, build-tools 31.0.0
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 12.5.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2020.3)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    
    [✓] VS Code (version 1.60.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.26.0
    
    [✓] Connected device (3 available)
        • M2007J20CG (mobile) • 5dd3be00 • android-arm64  • Android 11 (API 30)
        • macOS (desktop)     • macos    • darwin-arm64   • macOS 11.5.1 20G80 darwin-arm
        • Chrome (web)        • chrome   • web-javascript • Google Chrome 94.0.4606.61
    
    • No issues found!
    video
    RPReplay_Final1632824488.MP4
  2. added
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on Sep 28, 2021
  3. HansMuller commented on Sep 29, 2021

    @HansMuller
    Contributor
  4. olof-dev commented on May 24, 2022

    @olof-dev
    Contributor

    This can be seen in the sample code in the docs. Drag the second-to-last item below the bottom item, and then (without having dropped it) back to its original position. The animation is then incorrect: the dragged item starts moving down like it was going to the bottom, even though it should just drop back into its original position.

  5. olof-dev commented on May 24, 2022

    @olof-dev
    Contributor

    This seems to be caused by the - 1 in widget.itemCount - 1 in:

    void _dragEnd(_DragInfo item) {
    setState(() {
    if (_insertIndex! < widget.itemCount - 1) {
    // Find the location of the item we want to insert before
    _finalDropPosition = _itemOffsetAt(_insertIndex! + (_reverse ? 1 : 0));
    } else {
    // Inserting into the last spot on the list. If it's the only spot, put

    Either that's the issue, or there is something else funky going on with the logic in this sliver:

    1. If an item is moved only upwards, then back down and dropped in its original position, then the onReorder callback is not called, due to the oldIndex and newIndex matching.
    2. If an item is moved downwards beyond its current index, and then back up and dropped in its original position, then onReorder is called with oldIndex being the original index and newIndex being the original index + 1. (cf the onReorder code in the sample code in the docs, which does newIndex -= 1 if oldIndex < newIndex.)

    I'm not sure if there's a reason behind this different-newIndex-depending-on-whether-up-or-down behavior, but it's either that that's the problem, or it's the - 1 in _dragEnd.

  6. chaudharydeepanshu commented on Oct 25, 2022

    @chaudharydeepanshu

    Hey, over a year after it was reported, we're still having this problem:

    Untitled Project

    Therefore, kindly inform us when this will be fixed.
    And, if at all possible, please offer any temporary workarounds.

    Thank you.

  7. Albertbol commented on Dec 17, 2022

    @Albertbol

    Hey, same issue.
    Any update?

  8. olof-dev commented on Dec 18, 2022

    @olof-dev
    Contributor

    @chaudharydeepanshu @Albertbol Make sure to thumbs-up the original issue (ie the top post here), as the Flutter team uses that to help guide what to prioritize.

  9. 28 remaining items

  10. added 5 commits that reference this issue on Jul 28, 2025
    7a353b0
    6bb0730
    34a22ad
    4a1b87d
    1bc028f
  11. frankpape commented on Aug 1, 2025

    @frankpape

    @lukemmtt You're the MVP for digging into this enough to figure out the root issue, and to work through those options with tests!

  12. github-actions commented on Aug 25, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  13. locked as resolved and limited conversation to collaborators on Aug 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 2.5Found to occur in 2.5found in release: 2.6Found to occur in 2.6frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions