I have an animated "flower" made up of petal images that rotate into position on Widget build. The petal images can be of various lengths.
Because I didn't know how to move the pivot point of each petal PNG to the bottom centre for the rotation, I made each petal image a transparent square with the bottom of the petal in the centre of the image, so I can just rotate the entire square image around the centre and it looks like the petal is rotating around its base.
I have 5 of these in a stack, animated, and I want a gesture detector on each one so I can take action when any of them are tapped. I already have a GestureDetector on an image I use for the centre of the flower and it works, but none of the petals do.
I have tried using HitTestBehavior.translucent with no luck...
class _PFMainScreenState extends State<PFMainScreen>
with TickerProviderStateMixin {
AnimationController rotationController;
@override
void initState() {
super.initState();
rotationController = AnimationController(
duration: const Duration(milliseconds: 2000), vsync: this);
}
void onAfterBuild(BuildContext context) {
Future.delayed(const Duration(milliseconds: 1000), () {
rotationController.forward();
});
}
@override
void dispose() {
rotationController.dispose();
super.dispose();
}
Widget build(BuildContext context) {
WidgetsBinding.instance.addPostFrameCallback((_) => onAfterBuild(context));
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: DecoratedBox(
position: DecorationPosition.background,
decoration: BoxDecoration(
color: Colors.black,
image: DecorationImage(
image: AssetImage('images/background.jpg'),
fit: BoxFit.contain),
),
child: Stack(
children: <Widget>[
Center(
child: GestureDetector(
onTap: () {
print('1st image tapped');
},
behavior: HitTestBehavior.translucent,
child: Image.asset('images/petal-square.png', height: 350)),
),
Center(
child: RotationTransition(
turns: Tween(begin: 0.0, end: 0.2).animate(
new CurvedAnimation(
parent: rotationController,
curve: Curves.decelerate,
reverseCurve: Curves.decelerate)),
child: GestureDetector(
onTap: () {
print('2nd image tapped');
},
behavior: HitTestBehavior.translucent,
child:
Image.asset('images/petal-square.png', height: 250)),
),
),
Center(
child: RotationTransition(
turns: Tween(begin: 0.0, end: 0.4).animate(
new CurvedAnimation(
parent: rotationController,
curve: Curves.decelerate,
reverseCurve: Curves.decelerate)),
child: Image.asset('images/petal-square.png', height: 400),
),
),
Center(
child: RotationTransition(
turns: Tween(begin: 0.0, end: 0.6).animate(
new CurvedAnimation(
parent: rotationController,
curve: Curves.decelerate,
reverseCurve: Curves.decelerate)),
child: Image(
image: AssetImage('images/petal-square.png'),
),
),
),
Center(
child: RotationTransition(
turns: Tween(begin: 0.0, end: 0.8).animate(
new CurvedAnimation(
parent: rotationController,
curve: Curves.decelerate,
reverseCurve: Curves.decelerate)),
child: Image.asset('images/petal-square.png', height: 200),
),
),
Center(
child: GestureDetector(
onTap: () {
//rotationController.forward(from: 0.0);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PFMenuScreen(),
),
);
},
behavior: HitTestBehavior.translucent,
child: Image(
height: 100.0,
width: 100.0,
image: AssetImage('images/centre.png'),
),
),
),
],
),
),
),
);
}
}
Is there a way to detect taps on non-translucent images in a Stack?
Cheers