加了从上往下动画,又有叠加stack,导致点击事件穿透了,可以给底层作点击事件代替点击
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../common/Global.dart';
import '../../common/avatar.dart';
import '../../common/globalEventBus.dart';
import '../../router.dart';
import '../widget/splitLine.dart';
class IMNoticeDialog extends StatefulWidget {
String? id;
String? name;
String? avatar;
String? context;
bool? canShow;
Function(bool closeDialog) onClick;
Function(bool routeConversation) onClickRoute;
int? unReadCount;
IMNoticeDialog(
{Key? key,
this.id,
this.name,
this.avatar,
this.context,
this.canShow,
required this.onClick,
required this.onClickRoute,
this.unReadCount})
: super(key: key);
@override
State<StatefulWidget> createState() => IMNoticeDialogState();
}
class IMNoticeDialogState extends State<IMNoticeDialog>
with SingleTickerProviderStateMixin {
late AnimationController controller;
late Animation<Offset> animation;
late StreamSubscription<RefreshNoticeDialogEvent> refreshNoticeEvent;
String noticeContent = "";
String? userID = "";
String? nickName = "";
String? avatar = "";
int? messageNum = 0;
@override
void initState() {
refreshNoticeEvent = EventBusUtil.listen((event) {
if (mounted) {
setState(() {
noticeContent = event.noticeData;
userID = event.userID;
nickName = event.nickName;
avatar = event.avatar;
messageNum = event.messageNum;
});
}
});
controller = AnimationController(
duration: const Duration(milliseconds: 200), vsync: this);
animation = Tween(begin: const Offset(0, 0), end: const Offset(0, 1))
.animate(controller);
if (widget.context != null) {
noticeContent = widget.context!;
}
if (widget.id != null) {
userID = widget.id;
}
if (widget.avatar != null) {
avatar = widget.avatar;
}
if (widget.unReadCount != null) {
messageNum = widget.unReadCount;
}
if (widget.name != null) {
nickName = widget.name;
}
if (controller.isCompleted || controller.isDismissed) {
controller.forward();//动画开始执行
}
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.topCenter,
children: [
GestureDetector(
onTap: () {
widget.onClickRoute(true);
print("设置了点击事件");
MyRouter.pushMyMap(
context, MyRouter.conversion, {'conversationID': 'c2c_$userID'});
},
child: Container(//这里居然要对叠在底层的控件进行点击事件
color: Colors.transparent,
margin: EdgeInsets.only(top: Global.topHeight+10),
alignment: Alignment.topCenter,
height: 70.r,
width: 347.r,
),
),
Positioned(
top: -(70.r + Global.topHeight + 6),//负的在屏幕外面
child: Container(
alignment: Alignment.topCenter,
child: SlideTransition(//平移控件
position: animation,
child: Container(
margin: EdgeInsets.only(top: Global.topHeight + 6),
width: 347.r,
height: 70.r,
decoration: BoxDecorationUtil().setFillBoxDecorationImg(
'assets/images/bg_im_notice.png'),
child: Stack(
alignment: Alignment.topRight,
children: [
Row(
children: [
Container(
decoration: BoxDecoration(
border: Border.all(
width: 0.5,
color: const Color(0xFFE7E7E7)),
borderRadius: BorderRadius.circular(50)),
margin:
EdgeInsets.only(left: 15.r, right: 14.r),
child: Avatar(
width: 42.r,
height: 42.r,
avtarUrl: "$avatar",
svgaAvtarUrl: Global.defUserImag,
radius: 50,
),
),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
Container(
alignment: Alignment.centerLeft,
child: Text(
"$nickName",
style: TextStyle(
color: const Color(0xFF4E4E4E),
fontSize: 14.sp,
fontWeight: FontWeight.w500),
),
),
],
),
Row(
children: [
Container(
alignment: Alignment.centerLeft,
child: LimitedBox(
maxWidth: 200,
child: Text(noticeContent,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 10.sp,
color:
const Color(0xFFA3A3A3),
)),
),
)
],
),
],
)),
GestureDetector(
onTap: () {
widget.onClick(true);
},
child: Container(
padding: EdgeInsets.only(
top: 5.r,
bottom: 5.r,
left: 13.r,
right: 13.r),
child: Image(
image: const AssetImage(
'assets/images/icon_right_back.png'),
width: 9.r,
height: 15.r,
gaplessPlayback: true),
),
)
],
),
],
)),
),
),
),
],
);
}
}