Flutter中间镂空的二维码扫描控件

1、UI效果图:

2、中间镂空UI:

复制代码
class CenterTransparentMask extends CustomClipper<Path> {
  final double? width;

  CenterTransparentMask({this.width});

  @override
  Path getClip(Size size) {
    final path = Path()
      ..addRect(Rect.fromLTWH(0, 0, size.width,
          size.height + MediaQuery.of(Get.context!).padding.bottom))
      ..addRect(Rect.fromLTWH(
          (size.width - (width ?? 200)) / 2,
          (size.height +
                  MediaQuery.of(Get.context!).padding.bottom -
                  (width ?? 200)) /
              2,
          width ?? 200,
          width ?? 200));

    return path..fillType = PathFillType.evenOdd; 
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }
}
复制代码
3、扫码UI:
复制代码
class ScanWidget extends StatefulWidget {
  const ScanWidget({super.key});

  @override
  State<ScanWidget> createState() => _ScanWidgetState();
}

class _ScanWidgetState extends State<ScanWidget> with TickerProviderStateMixin {
  late Animation<double> animation;
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    initAnima();
  }

  void initAnima() {
    _controller = AnimationController(
      duration: const Duration(seconds: 4),
      vsync: this,
    );

    animation = Tween(begin: -100.0, end: 100.0).animate(_controller)
      ..addListener(() {
        if (mounted) setState(() => {});
      });
    _controller.repeat();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        _scanBorder(),
        _scanCenter(context),
        Transform.translate(
          offset: Offset(0, animation.value),
          child: _scanLine(),
        ),
      ],
    );
  }

  Widget _scanCenter(BuildContext context) {
    return ClipPath(
      clipper: CenterTransparentMask(), 
      child: BackdropFilter(
        filter: ImageFilter.blur(sigmaX: 2, sigmaY: 2),
        child: Container(
          color: Colors.black.withOpacity(0.5),
          width: SystemUtil().getScreenWidth(context),
          height: SystemUtil().getScreenHeight(context),
        ),
      ),
    );
  }

  Widget _scanBorder() {
    return Image.asset(
      ImageUtils.getImgPath("img_border", "scan"),
      width: 200,
      height: 200,
    );
  }

  Widget _scanLine() {
    return Image.asset(
      ImageUtils.getImgPath("image_line", "scan"),
      width: 200,
      height: 6,
    );
  }
}

3、图片加载工具类:

复制代码
class ImageUtils {
  static String getImgPath(String name, String moduleName,
      {String format = "png"}) {
    return "assets/images/$moduleName/$name.$format";
  }
}
相关推荐
路灯上の马陈18 分钟前
Java File类2 练习 学习笔记
java·笔记·学习
Shaoshing19 分钟前
LeetCode100 155.最小栈
java·数据结构·算法
SummerJokes27 分钟前
Java 多线程专项实验|两种创建线程方式、synchronized 线程同步、银行存取款实战
java·开发语言·python
treasure14031 分钟前
Flutter低级教程(2027年1月重制版)
android·flutter·ios·gui·web·dart·移动端
二宝哥1 小时前
07.Spring AI 接入智谱文生图:从配置到实战
java·人工智能·spring
秋天的怀念_David1 小时前
算法一千零一夜 · 第四十夜 · 错层的状态码
java·程序人生·算法
xyphf_和派孔明1 小时前
第九章: 上下文工程五大策略与六大模块交叉矩阵纵览
java·开发语言
zhanlang02171 小时前
1.2 CSS 盒模型深度剖析:标准与怪异模式的博弈
前端·css
徐同保1 小时前
模板里的图标组件找不到定义?聊聊 @tabler/icons-vue 与自动导入
前端·javascript·vue.js