Skip to content

Instantly share code, notes, and snippets.

@jezell
Last active June 12, 2024 15:58
Show Gist options
  • Save jezell/235a4943272a993bf8e59bacb972765e to your computer and use it in GitHub Desktop.
Save jezell/235a4943272a993bf8e59bacb972765e to your computer and use it in GitHub Desktop.
draggable
// Copyright 2019 the Dart project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
super.key,
required this.title,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double getScale(BuildContext context) {
final box = context.findRenderObject() as RenderBox?;
if (box == null) {
return 1.0;
}
final size =
box.localToGlobal(Offset(1, 1)) - box.localToGlobal(Offset.zero);
return size.dx;
}
double scale = 1.0;
@override
Widget build(BuildContext context) {
double w = 100;
double h = 100;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: InteractiveViewer(
child: Container(
color: Colors.white,
child: Builder(builder: (context) {
return Stack(children: [
Center(
child: Draggable(
dragAnchorStrategy: (draggable, context, position) {
scale = getScale(context);
final anchor =
childDragAnchorStrategy(draggable, context, position);
return anchor * scale;
},
childWhenDragging: Container(
width: w,
height: h,
color: Colors.red.withAlpha(100)), // boxDrag,
feedback: Builder(
builder: (context) => Container(
width: (w) * scale,
height: (h) * scale,
alignment: Alignment.center,
child: OverflowBox(
alignment: Alignment.center,
minWidth: 0,
minHeight: 0,
maxWidth: double.infinity,
maxHeight: double.infinity,
child: Transform.scale(
scale: scale,
child: Container(
width: w,
height: h,
color: Colors.red))))),
child: Container(width: w, height: h, color: Colors.red),
))
]);
})),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment