import 'package:flutter/material.dart'; //触摸回调组件 class TouchCallBack extends StatefulWidget { //子组件 final Widget child; //回调函数 final VoidCallback onPressed; final bool isfeed; //背景色 final Color backgroud; //传入参数列表 const TouchCallBack({Key key, @required this.onPressed, this.isfeed, this.backgroud,@required this.child}) : super(key: key); @override _TouchCallBackState createState() => _TouchCallBackState(); } class _TouchCallBackState extends State { Color color = Colors.transparent; @override Widget build(BuildContext context) { return GestureDetector( //使用Container 包裹 child: Container( color: color, child: widget.child, ), onTap: widget.onPressed, onPanDown: (d){ if(widget.isfeed == false) return; setState(() { color = widget.backgroud; }); }, onPanCancel: (){ setState(() { color = Colors.transparent; }); }, ); } }