Flutter如果自定义一个“控件”?如何使用?
比如我要写一个控件继承之前有的功能,并加上自己的功能。
class XGestureDetector extends StatelessWidget {
const XGestureDetector({Key? key, this.onTap, this.child}) : super(key: key);
final Function? onTap;
final Widget? child;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () async {
DialogUtils.showPrivacyDialog(context, showCallback: (value) {
if (value) {
} else {
if (onTap != null) {
onTap!();
}
}
});
},
child: child,
);
}
}
class SearchWidget extends StatelessWidget {
UnitList data;
int? index;
SearchWidget({Key? key, required this.data, this.index}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: XGestureDetector(
onTap: () {
RouterUtil.pushToWeb(data.repeatUrl);
},
child: Container(
padding: EdgeInsets.only(left: 10),
height: 30,
decoration: const BoxDecoration(
color: ColorConfig.COLOR_BG_1,
borderRadius: BorderRadius.all(Radius.circular(25)),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(
Icons.search,
color: ColorConfig.COLOR_TEXT_1,
size: 15,
),
UIText(
data.tagName ?? "",
textColor: ColorConfig.COLOR_TEXT_1,
)
],
),
),
));
}
}