Skip to content

Positioning Issue with Draggable Widget #173

@ibrahimEltayfe

Description

@ibrahimEltayfe

When using the draggable widget or any other widget that uses offset, the positioning appears to be skewed or displaced. it does not retain its intended positions accurately during the interaction, resulting in an inaccurate rendering of the UI.

without.scaled.box.mp4
with.scaled.box.mp4

Here is the code:

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      builder: (context, child) => ResponsiveBreakpoints.builder(
        breakpoints: [
          const Breakpoint(start: 0, end: 450, name: MOBILE),
          const Breakpoint(start: 451, end: 800, name: TABLET),
          const Breakpoint(start: 801, end: 1920, name: DESKTOP),
          const Breakpoint(start: 1921, end: double.infinity, name: '4K'),
        ],
        child: child!,
      ),
      //home: _Home()
      home: Builder(builder: (context) {
        return ResponsiveScaledBox(
          width: ResponsiveValue<double>(context, conditionalValues: const [
            Condition.between(start: 0, end: 450, value: 360),
            Condition.between(start: 450, end: 800, value: 460),
            Condition.between(start: 800, end: 1100, value: 580),
            Condition.between(start: 1100, end: 1400, value: 620),
            Condition.between(start: 1400, end: 9999, value: 900),
          ]).value,
          child: const _Home(),
        );
      }),
    );
  }
}

class _Home extends StatefulWidget {
  const _Home({super.key});

  @override
  State<_Home> createState() => _HomeState();
}

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

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

  void _onDragEnd(BuildContext context, DraggableDetails details) {
    setState(() {});
  }

  @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),),
              ),
            ),
          ]
        ),
      ),
    );
  }
}

Flutter (Channel stable, 3.19.3)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions