karvulf / flutter-reorderable-grid-view

BSD 3-Clause "New" or "Revised" License
158 stars 23 forks source link

Bug:ReorderableBuilder子项 添加项目按钮在里面固定,拖动会导致错误 #135

Closed OICQ469 closed 1 month ago

OICQ469 commented 1 month ago

https://github.com/user-attachments/assets/05088e70-5029-4821-8f02-d2cf868cb34d

[Uploading test.dart.zip…]()

OICQ469 commented 1 month ago

test.dart.zip This is the project file

karvulf commented 1 month ago

Hello @OICQ469 Thanks for opening the issue, can you just paste the code from your widget that is using this package? Then I could immediately check if I see the issue

OICQ469 commented 1 month ago

你好@OICQ469感谢您打开这个问题,您能从此软件包的小部件中粘贴代码吗?我可以立即检查我是否看到了问题

test.dart.zip This is a zip of the code

OICQ469 commented 1 month ago

@karvulf

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

void main() {
  runApp(const MaterialApp(home: MyApp()));
}

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

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _scrollController = ScrollController();
  var index = 1;
  var _fruits = <String>[];

  @override
  Widget build(BuildContext context) {
    final generatedChildren = List.generate(_fruits.length + 1, (index) {
      if (index == _fruits.length)
        // Add button
        return GestureDetector(
            key: Key("button"),
            onTap: () {
              _fruits.add("value${index++}");
              setState(() {});
            },
            child: Container(
              color: Colors.lightBlue,
              child: Text("Button"),
            ));
      else {
        // Item
        return Container(
            key: Key(_fruits.elementAt(index)),
            color: Colors.lightBlue,
            child: Text(_fruits.elementAt(index)));
      }
    });

    return Scaffold(
      body: ReorderableBuilder(
        children: generatedChildren,
        scrollController: _scrollController,
        longPressDelay: Duration(milliseconds: 300),
        lockedIndices: [_fruits.length],
        dragChildBoxDecoration:
            BoxDecoration(color: CupertinoColors.transparent),
        onReorder: (ReorderedListFunction reorderedListFunction) {
          setState(() {
            _fruits = reorderedListFunction(_fruits) as List<String>;
          });
        },
        builder: (children) {
          return GridView(
            controller: _scrollController,
            children: children,
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 4,
              mainAxisSpacing: 4,
              crossAxisSpacing: 8,
            ),
          );
        },
      ),
    );
  }
}
karvulf commented 1 month ago

This seems to be a bug combined with lockedIndices on the last item. I will try to fix this this evening, thanks for reporting it @OICQ469

OICQ469 commented 1 month ago

这似乎是最后一个项目上与lockedIndices结合的错误。我今晚会试着解决这个问题,谢谢你的报告@OICQ469

thank

karvulf commented 1 month ago

I found a fix in the code. It should also fix the issue when new items are appearing. Before, the animation also didn't work. I will release this fix with version 5.3.2. @OICQ469

karvulf commented 1 month ago

I will do some more tests, so unfortunately the fix will come in the next days. @OICQ469 When you are updating, you have to update the code in onReorder because there would be still a bug:

        onReorder: (ReorderedListFunction reorderedListFunction) {
          final updatedFruits = reorderedListFunction(
            <String>[..._fruits, 'button'],
          ) as List<String>;
          setState(() {
            _fruits = updatedFruits..removeLast();
          });
        },
karvulf commented 1 month ago

I just published version 5.3.2 and this should fix the issue @OICQ469 Thanks again