Код: Выделить всё
class MyApp extends StatelessWidget {
MyApp({super.key});
final ValueNotifier onTappedLocation =
ValueNotifier(ui.Offset.zero);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
debugShowCheckedModeBanner: false,
home: Container(
color: Colors.indigo,
child: GestureDetector(
onTapDown: (details) {
onTappedLocation.value = details.globalPosition;
},
child: Center(
child: FutureBuilder(
future: getImage(),
builder:
(BuildContext context, AsyncSnapshot snapshot) {
Widget centerChild;
if (snapshot.hasData) {
centerChild = CustomPaint(
painter: FootballPitchPainter(
snapshot.data!, onTappedLocation, () {
print("object");
}),
size: Size(360, 480),
);
} else {
centerChild = const CircularProgressIndicator();
}
return centerChild;
})),
),
),
);
}
}
class FootballPitchPainter extends CustomPainter {
final ui.Image? image;
ValueNotifier onTappedLocation;
VoidCallback onTap;
FootballPitchPainter(this.image, this.onTappedLocation, this.onTap)
: super(repaint: onTappedLocation);
@override
void paint(Canvas canvas, Size size) {
final w = size.width;
final h = size.height;
// Other Code
canvas.drawImage(image!, ui.Offset(100, 100), playerPaint);
if (onTappedLocation.value == ui.Offset(100, 100)) {
onTap();
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
Future getImage() async {
ByteData bd = await rootBundle.load('assets/logo.png');
final Uint8List bytes = Uint8List.view(bd.buffer);
final ui.Codec codec =
await ui.instantiateImageCodec(bytes, targetHeight: 20, targetWidth: 20);
final ui.Image image = (await codec.getNextFrame()).image;
return image;
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... om-painter