Anonymous
Нажмите событие на изображении в пользовательском художнике
Сообщение
Anonymous » 26 июн 2024, 11:31
Я пытаюсь щелкнуть по моему drawImage, но не знаю, как это реализовать. Я предоставляю код ниже.
Код: Выделить всё
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;
}
Я пытался реализовать это с помощью детектора жестов, но позиции, которые я получаю в localPosition и globalPoistion, отличаются от позиции смещения пользовательского рисовальщика, поэтому я не могу это реализовать.>
Подробнее здесь:
https://stackoverflow.com/questions/786 ... om-painter
1719390662
Anonymous
Я пытаюсь щелкнуть по моему drawImage, но не знаю, как это реализовать. Я предоставляю код ниже. [code]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; } [/code] Я пытался реализовать это с помощью детектора жестов, но позиции, которые я получаю в localPosition и globalPoistion, отличаются от позиции смещения пользовательского рисовальщика, поэтому я не могу это реализовать.> Подробнее здесь: [url]https://stackoverflow.com/questions/78671192/click-event-on-image-in-custom-painter[/url]