Created
September 6, 2022 10:13
-
-
Save AhmedAbouelkher/6e5aff1ce7164400c908ab6baffca140 to your computer and use it in GitHub Desktop.
Video Moving Water Mark
This file contains hidden or 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 'dart:async'; | |
import 'dart:developer'; | |
import 'dart:math' hide log; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/widgets.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
title: 'Material App', | |
home: Page(), | |
); | |
} | |
} | |
class Info { | |
final Size size; | |
final Offset position; | |
const Info({ | |
required this.size, | |
required this.position, | |
}); | |
} | |
class Page extends StatefulWidget { | |
const Page({Key? key}) : super(key: key); | |
@override | |
State<Page> createState() => _PageState(); | |
} | |
class _PageState extends State<Page> { | |
GlobalKey stickyKey = GlobalKey(); | |
OverlayEntry? entry; | |
Timer? timer; | |
@override | |
void initState() { | |
WidgetsBinding.instance.addPostFrameCallback((_) { | |
init(); | |
}); | |
super.initState(); | |
} | |
Info? getInfo() { | |
final keyContext = stickyKey.currentContext; | |
if (keyContext == null) return null; | |
final box = keyContext.findRenderObject() as RenderBox; | |
final pos = box.localToGlobal(Offset.zero); | |
return Info( | |
size: box.size, | |
position: pos, | |
); | |
} | |
void init() { | |
final info = getInfo(); | |
if (info == null) { | |
log("info is null"); | |
return; | |
} | |
timer = Timer.periodic(const Duration(milliseconds: 200), (_) { | |
setOverlay(info.size, info.position); | |
}); | |
} | |
void setOverlay(Size size, Offset offset) { | |
final overlay = Overlay.of(context); | |
const s = Size(90, 40); | |
double y = offset.dy; | |
double x = offset.dx; | |
final rand = Random(); | |
final h = rand.nextInt(size.height.toInt()) % size.height.toInt(); | |
final w = rand.nextInt(size.width.toInt()) % size.width.toInt(); | |
y += (h - s.height / 2); | |
x += (w - s.width / 2); | |
entry?.remove(); | |
log("$x, $y"); | |
entry = OverlayEntry( | |
builder: (context) { | |
return Positioned( | |
top: y, | |
left: x, | |
child: Container( | |
height: s.height, | |
width: s.width, | |
color: Colors.redAccent, | |
child: const Material( | |
child: Text( | |
"Ahmed Mahmoud", | |
style: TextStyle( | |
fontSize: 10, | |
), | |
), | |
), | |
), | |
); | |
}, | |
); | |
overlay?.insert(entry!); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Material App Bar'), | |
), | |
body: Center( | |
child: Container( | |
key: stickyKey, | |
height: 300, | |
width: 300, | |
color: Colors.red.shade200, | |
child: const Center( | |
child: Text('Hello World'), | |
), | |
), | |
), | |
floatingActionButton: Row( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
FloatingActionButton( | |
heroTag: "-", | |
onPressed: getInfo, | |
), | |
const SizedBox(width: 10), | |
FloatingActionButton( | |
heroTag: "--", | |
backgroundColor: Colors.black, | |
onPressed: () { | |
timer?.cancel(); | |
}, | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment