Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Created February 26, 2023 21:34
Show Gist options
  • Save DaisukeNagata/2258cb81c45650896edfc2eefb449964 to your computer and use it in GitHub Desktop.
Save DaisukeNagata/2258cb81c45650896edfc2eefb449964 to your computer and use it in GitHub Desktop.
Various coordinates can be tapped
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',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String tex = 'You have pushed the button this many times:';
final keys = GlobalKey();
Rect rect = const Rect.fromLTRB(0, 0, 0, 0);
@override
void initState() {
super.initState();
Future.delayed(const Duration(seconds: 1), () {
setState(() {
rect = keys.globalPaintBounds ?? const Rect.fromLTRB(0, 0, 0, 0);
});
});
}
@override
Widget build(BuildContext context) {
return Material(
child: Stack(
children: [
GestureDetector(
onTap: () {
setState(() {
/// not changed
tex = '';
});
},
child: Container(
color: Colors.teal,
transform: Matrix4.translationValues(40, 333, 0),
width: 300,
height: 20,
child: Text(
key: keys,
tex,
),
),
),
_widget(),
],
),
);
}
Widget _widget() {
return Positioned(
top: rect.top,
left: rect.left,
child: GestureDetector(
onTap: () {
setState(() {
tex = '';
});
},
child: Container(
color: Colors.transparent,
width: rect.width,
height: rect.height,
),
),
);
}
}
extension GlobalKeyEx on GlobalKey {
Rect? get globalPaintBounds {
final renderObject = currentContext?.findRenderObject();
final translation = renderObject?.getTransformTo(null).getTranslation();
if (translation != null && renderObject?.paintBounds != null) {
return renderObject!.paintBounds
.shift(Offset(translation.x, translation.y));
} else {
return null;
}
}
}
@DaisukeNagata
Copy link
Author

2023-02-27.6.29.57.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment