flutter 封装工具类

核心思路就是你想要什么就返回什么需要padding就写返回padding的方法,需要BoxDecoration就写返回BoxDecoration的方法,需要一个文本效果就返回一个Text,需要什么就写个方法return什么,可以写一个类,也可以写一个类里面的方法,可以返回就可以了
//写一个类,继承没状态,传一个距离进来,返回Padding是最关键的

class TopPadding extends StatelessWidget {
  TopPadding(this.topPadding, {Key? key}) : super(key: key);

  double topPadding;

  @override
  Widget build(BuildContext context) {
    return Padding(padding: EdgeInsets.only(top: topPadding));
  }
}

调用就是使用该类,传距离

TopPadding(12),

//写一个类,里面写多个方法

class BoxDecorationUtil {
  ///设置背景
  BoxDecoration setFillBoxDecoration(color, radius,
      {topLeft, topRight, bottomLeft, bottomRight}) {
    return BoxDecoration(
        //背景
        color: color,
        //设置四周圆角 角度
        borderRadius: radius == 0.0
            ? BorderRadius.only(
                topLeft: Radius.circular(topLeft ?? 0),
                topRight: Radius.circular(topRight ?? 0),
                bottomLeft: Radius.circular(bottomLeft ?? 0),
                bottomRight: Radius.circular(bottomRight ?? 0))
            : BorderRadius.all(Radius.circular(radius)));
  }
  
  ///设置背景图片
  BoxDecoration setFillBoxDecorationImg(bgUrl,
      {fit, topLeft, topRight, bottomLeft, bottomRight}) {
    return BoxDecoration(
        //设置四周圆角 角度
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(topLeft ?? 0),
            topRight: Radius.circular(topRight ?? 0),
            bottomLeft: Radius.circular(bottomLeft ?? 0),
            bottomRight: Radius.circular(bottomRight ?? 0)),
        image: DecorationImage(
            fit: fit ?? BoxFit.fill,
            image: const BackgroundWall().loadImagesProvider(bgUrl)));
  }
}

正常写代码是

Container(
                                          decoration: BoxDecoration(//直接写BoxDecoration()
                                              borderRadius:
                                                  BorderRadius.circular(50),
                                              border: Border.all(
                                                  width: 2.r,
                                                  color: Colors.white)),
                                          child: PhysicalModel(
                                              color: Colors.transparent,
                                              borderRadius:
                                                  BorderRadius.circular(50),
                                              clipBehavior: Clip.antiAlias,
                                              child: Image(
                                                  image: AssetImage(
                                                    avatarList[index],
                                                  ),
                                                  width: 79.r,
                                                  height: 79.r)),
                                        );

而使用封装好的类方法就是

Container(
                  decoration: BoxDecorationUtil()
                      .setFillBoxDecoration(Colors.white, 14.0),margin: const EdgeInsets.only(top: 35, right: 52, left: 52),

文字居中的方法

class ContentCenterText extends StatelessWidget {
  ContentCenterText(this.contentText, this.contentSize, this.color,
      {Key? key, this.height = 1.2})
      : super(key: key);

  String contentText;
  double contentSize;
  double height;

  Color color;

  @override
  Widget build(BuildContext context) {
    return Text(contentText,
        textAlign: TextAlign.center,
        style: TextStyle(color: color, fontSize: contentSize, height: height));
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutter 是一个很好的跨平台开发框架,可以使开发者在一个代码库中同时构建 iOS 和 Android 应用。Flutter 的强大之处在于其插件系统,可以让开发者轻松地使用原生设备功能和第三方服务。以下是几个封装 Flutter 插件的步骤: 1. 确定插件的功能和 API:定义插件的功能和 API,这样就可以在 Flutter 中使用。例如,如果你想封装一个照相机插件,你需要决定插件支持哪些功能,例如拍照、录像、闪光灯等。 2. 创建 Flutter 插件项目:创建一个 Flutter 插件项目,可以使用 Flutter CLI 命令行工具创建。Flutter CLI 工具提供了一个简单的命令:flutter create --template=plugin my_plugin。这个命令会创建一个基本的插件模板,包括 Dart 和 Kotlin/Swift 代码。 3. 实现插件的功能和 API:在 Flutter 插件项目中实现插件的功能和 API。对于照相机插件,需要在原生代码中实现拍照、录像等功能,并在 Dart 代码中暴露这些功能。 4. 测试插件:在测试应用程序中测试插件的功能和 API。创建一个简单的 Flutter 应用程序,并将插件添加到应用程序中。确保插件在应用程序中能够正常工作。 5. 发布插件:将插件发布到公共存储库中,例如 pub.dev。在发布前,需要确保插件的文档清晰易懂,同时代码质量和可用性符合标准。 以上是封装 Flutter 插件的基本步骤,当然具体实现还需要根据具体的需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值