美文网首页Flutter相关
Flutter怎么自定义一个“控件”

Flutter怎么自定义一个“控件”

作者: 码农朱同学 | 来源:发表于2022-07-29 10:35 被阅读0次

    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,
                    )
                  ],
                ),
              ),
            ));
      }
    }
    

    相关文章

      网友评论

        本文标题:Flutter怎么自定义一个“控件”

        本文链接:https://www.haomeiwen.com/subject/dzcxwrtx.html