Coder Social home page Coder Social logo

Comments (5)

abigotado avatar abigotado commented on June 9, 2024 1

@ibrahimEltayfe Thank you very much for sharing your solution!

Dunno why, but it my case it doesn't work - feedback is still misplaced. But I made a workaround this way:

 dragAnchorStrategy: (final draggable, final context, final position) {
      final responsiveBreakpoints = ResponsiveBreakpoints.of(context);
      final mediaQuery = MediaQuery.of(context);
      final double widthOffset =
          mediaQuery.size.width - responsiveBreakpoints.screenWidth;
      final double heightOffset =
          mediaQuery.size.height - responsiveBreakpoints.screenHeight;

      final RenderBox renderObject = context.findRenderObject()! as RenderBox;
      return renderObject.globalToLocal(
        Offset(
          position.dx - widthOffset / 2,
          position.dy - heightOffset / 2,
        ),
      );
    },

It's not quite proper, I think, but better, than nothing. And when you are moving your feedback widgets, it's sliding from your finger a little bit (further you move, more it slides).

Maybe it'll be a good idea to leave a report in flutter issues too.

from responsiveframework.

abigotado avatar abigotado commented on June 9, 2024

I have the same problem. In my case I'm dragging a chip with some label:

2024-03-28.20.05.32.mov

feedback appears misplaced in both directions and offset of this misplacement changes on moving.

No big difference with the code above - Chip as a child of draggable and the same chip as feedback and no onDragCallbacks.

#17 this topic was closed without any solution provided

@rayliverified Could you please help us to resolve this?

from responsiveframework.

ibrahimEltayfe avatar ibrahimEltayfe commented on June 9, 2024

@abigotado

Nice, your solution worked fine like this:

Offset adjustPosition(BuildContext context, Offset position){
  final RenderBox? renderObject = context.findRenderObject() as RenderBox?;
  return renderObject?.globalToLocal(
    Offset(
      position.dx,
      position.dy,
    ),
  ) ?? position;
}

without any further calculations, because globalToLocal is converting the global position (coordinates relative to the entire screen) to the coordinates relative to the widget itself. This takes into account any transformations or scaling applied to the widget's layout.

class _HomeState extends State<_Home> {
  Offset position = const Offset(0, 0);
  Offset currentPos = const Offset(0, 0);

  void _onDragUpdate(BuildContext context, DragUpdateDetails details) {
    currentPos += details.delta;
  }

  void _onDragEnd(BuildContext context, DraggableDetails details) {
    setState(() {
      position = adjustPosition(context, currentPos);
    });
  }

  Offset adjustPosition(BuildContext context, Offset position){
    final RenderBox? renderObject = context.findRenderObject() as RenderBox?;
    return renderObject?.globalToLocal(position,) ?? position;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Stack(
          children: [
            Positioned(
              top: position.dy,
              left: position.dx,
              child: Draggable(
                onDragUpdate: (details) => _onDragUpdate(context, details),
                onDragEnd: (details) => _onDragEnd(context, details),

                childWhenDragging: const SizedBox.shrink(),
                feedback: const Material(child: Text("DRAGGABLE",style: TextStyle(fontSize: 50),)),
                child: const Text("DRAGGABLE",style: TextStyle(fontSize: 50),),
              ),
            ),
          ]
        ),
      ),
    );
  }
}

The feedback positioning is working fine without handling its offset, I do not know why the feedback position not working properly on your side, my problem was with placing the item on the scaled screen. but the coordinates coming from the DraggableDetails are fine.

My remaining problem is the scale of the feedback item, I will try to make a calculation for the scale.

Screen.Recording.2024-04-01.at.7.58.29.AM.mov

from responsiveframework.

abigotado avatar abigotado commented on June 9, 2024

@ibrahimEltayfe Well, I have some suggestions about the difference. Do you place ResponsiveScaledBox at home parameter of MaterialApp? In my case I use builder. And when I move it to home - I see the same behavior as you describe - wrong scale. And when return it to builder - it has wrong position. Flutter team has confirmed the problem, see here: flutter/flutter#146002

from responsiveframework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.