Created
November 2, 2020 08:36
-
-
Save daohoangson/1a0b6ef52f265692a225d973d6361a1e to your computer and use it in GitHub Desktop.
StickerWidget (panning only)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: StickerWidget(), | |
), | |
); | |
} | |
} | |
class StickerWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Stack( | |
children: [ | |
Source(), | |
Sticker('1', 0, 0, 100, 100), | |
Sticker('2', 200, 200, 100, 200), | |
], | |
); | |
} | |
} | |
class Source extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return SizedBox.expand( | |
child: Container( | |
color: Colors.blue, | |
child: Center(child: Text('source')), | |
), | |
); | |
} | |
} | |
class Sticker extends StatefulWidget { | |
final String text; | |
final double left; | |
final double top; | |
final double width; | |
final double height; | |
Sticker( | |
this.text, | |
this.left, | |
this.top, | |
this.width, | |
this.height, | |
); | |
@override | |
State<Sticker> createState() => _StickerState(); | |
} | |
class _StickerState extends State<Sticker> { | |
ValueNotifier<Rect> rect; | |
initState() { | |
super.initState(); | |
rect = ValueNotifier(Rect.fromLTWH( | |
widget.left, | |
widget.top, | |
widget.width, | |
widget.height, | |
)); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedBuilder( | |
animation: rect, | |
builder: (_, child) => Stack( | |
children: [ | |
Positioned.fromRect( | |
child: GestureDetector( | |
child: child, | |
onPanUpdate: (details) { | |
final r = rect.value; | |
final offset = r.topLeft; | |
final delta = details.delta; | |
rect.value = Rect.fromLTWH( | |
offset.dx + delta.dx, | |
offset.dy + delta.dy, | |
r.width, | |
r.height, | |
); | |
}, | |
), | |
rect: rect.value, | |
), | |
Positioned( | |
child: Icon(Icons.close), | |
left: rect.value.left, | |
top: rect.value.top, | |
), | |
], | |
), | |
child: SizedBox.expand( | |
child: Container( | |
child: Center( | |
child: Text(widget.text), | |
), | |
color: Colors.red, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment