我有一个动画的“花”,它由花瓣图像组成,这些图像旋转到Widget构建中的位置。花瓣图像可以具有各种长度。
因为我不知道如何将每个花瓣PNG的枢轴点移动到底部中心进行旋转,所以我将每个花瓣图像设为透明正方形,花瓣的底部位于图像的中心,所以我只需围绕中心旋转整个正方形图像,就好像花瓣围绕其底部旋转一样。
我将其中的5个动画设置为一个堆栈,并且每个动画都需要一个手势检测器,以便在点击其中的任何一个时可以采取措施。我在用于花朵中心的图像上已经有一个GestureDetector,它可以工作,但是没有一个花瓣起作用。
我尝试使用HitTestBehavior.translucent却没有运气...
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'),
),
),
),
],
),
),
),
);
}
}
是否可以检测堆栈中非半透明图像上的拍子?
欢呼
答案 0 :(得分:0)
如果使用Dart Dev Tools的波动检查器运行应用程序,则可以看到每个Center
小部件都填满了整个屏幕。这可能会影响手势检测,具体取决于放置手势检测器的位置。
这是一些适合您的代码。我将旋转的图像重构为可重用的小部件。
class PFMainScreen extends StatefulWidget {
@override
_PFMainScreenState createState() => _PFMainScreenState();
}
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: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
RotatingImage(
rotationController: rotationController,
onTap: () {
print("Image 1 tapped");
},
imageHeight: 400,
tweenEnd: 0.2,
),
RotatingImage(
rotationController: rotationController,
onTap: () {
print("Image 2 tapped");
},
imageHeight: 350,
tweenEnd: 0.4,
),
RotatingImage(
rotationController: rotationController,
onTap: () {
print("Image 3 tapped");
},
imageHeight: 250,
tweenEnd: 0.6,
),
RotatingImage(
rotationController: rotationController,
onTap: () {
print("Image 4 tapped");
},
imageHeight: 200,
tweenEnd: 0.8,
),
],
),
),
);
}
}
class RotatingImage extends StatelessWidget {
final AnimationController rotationController;
final double imageHeight;
final double tweenEnd;
final Function onTap;
const RotatingImage({
this.onTap,
this.imageHeight,
this.tweenEnd,
@required this.rotationController,
});
@override
Widget build(BuildContext context) {
return RotationTransition(
turns: Tween(begin: 0.0, end: tweenEnd).animate(new CurvedAnimation(
parent: rotationController,
curve: Curves.decelerate,
reverseCurve: Curves.decelerate)),
child: GestureDetector(
onTap: onTap,
behavior: HitTestBehavior.translucent,
child: Image.asset('images/petal-square.png', height: imageHeight)),
);
}
}