itisnajim / reorderable_staggered_scroll_view

MIT License
4 stars 7 forks source link

Issue with dragging display after changing application size #9

Open himeope opened 5 months ago

himeope commented 5 months ago

When I run the example, I notice that resizing the application can lead to abnormal display of items under the grid when they are being dragged. This issue does not arise if I reload the page.

https://github.com/itisnajim/reorderable_staggered_scroll_view/assets/86174769/e1a65b16-12d7-4266-9705-a20ff895ea6a

fedpinx commented 3 months ago

I had to clone the repo and do some modifications, looks like onChangedSize is not being called, RenderBoxSize child is wrapped with a NotificationListener and there is no one notifying about size changes.

I would suggest (as a workaround), to fork/clone it, and add a didUpdateWidget override to RenderBoxSize widget, and call onChangeSize() there, like:

import 'package:flutter/material.dart';

class RenderBoxSize extends StatefulWidget {
  const RenderBoxSize(this.child, this.onChangeSize, {super.key});

  final Widget child;
  final void Function(Size size) onChangeSize;

  @override
  State<StatefulWidget> createState() => RenderBoxSizeState();
}

class RenderBoxSizeState extends State<RenderBoxSize> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
      onChangeSize();
    });
  }

  void onChangeSize() {
    final RenderBox? renderBox = context.findRenderObject() as RenderBox?;
    if (renderBox != null) {
      widget.onChangeSize(renderBox.size);
    }
  }

  @override
  void didUpdateWidget(covariant RenderBoxSize oldWidget) {
    super.didUpdateWidget(oldWidget);
    WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
      onChangeSize();
    });
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

You can do more checks in didUpdateWidget to see if the widget actually changed before calling the onChangeSize, also, not sure if its necessary to call onChangeSize on the next frame, or is enough to call it right there, but that's what worked for me.

Hope it helps!