搜索插件、代码仓与 APP 分类

widget_mask 封面
插件详情v1.0.0+0

widget_mask

使用一个控件来遮罩并混合另一个控件,例如将文本印在表面上。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
10
喜欢数
70
Pub 分数
150

安装插件

flutter pub add widget_mask

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

widget_mask 包使开发者能够使用一个 widget 作为遮罩来裁剪或混合另一个 widget,从而实现诸如将文本印在表面上等创意视觉效果。它利用 Flutter 的组合功能应用透明度遮罩,非常适合具有透明度和图层的高级 UI 设计。该包轻量且可无缝集成到现有的跨平台 Flutter 项目中。它支持静态和动态遮罩场景,增强了自定义 widget 组合的灵活性。

适合场景

  • 在图像上创建文本叠加
  • 设计分层的用户界面元素
  • 实现自定义裁剪路径
  • 构建动画蒙版过渡
  • 使用透明度制作视觉效果
  • 通过遮罩图形增强应用品牌标识

主要特点

  • 支持任何小部件作为遮罩
  • 高效渲染,开销最小
  • 跨平台兼容性
  • 灵活的混合模式
  • 通过简单 API 实现轻松集成
  • 动态遮罩更新

适合对象

  • 寻求高级视觉效果的UI设计师
  • 开发品牌化或艺术性界面的开发者
  • 需要文本表面效果的应用程序
  • 使用复杂图层和透明度的项目
  • Flutter 开发人员探索创意构图

使用建议

  • 可能会影响使用大量遮罩时的性能
  • 需要理解 Flutter 的绘图系统
  • 限制为可渲染为蒙版的小部件
  • 未经优化,不适合高频动画
  • 最好与可预测的、稳定的遮罩形状配合使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

pub.dev package page GitHub Actions CI GitHub Stars

Use a widget to mask and blend another widget, for example to imprint text onto surfaces.


If you're looking for a database solution, check out cbl, another project of mine. It brings Couchbase Lite to standalone Dart and Flutter, with support for:

  • Full-Text Search,
  • Expressive Queries,
  • Data Sync,
  • Change Notifications

and more.


Limitations

Widgets that are used as children of WidgetMask or SaveLayer must not need compositing, or contain widgets which need compositing, such as RepaintBoundary.

This is because this package makes use of save layers, which cannot encompass compositing layers.

Masks and blending

A mask is an image, which is positioned in front of another image and affects this images in some way, where it is not empty.

Blending is a process which takes two images and produces a new image by applying a mathematical function to each pair of pixels from the input images. For some of these functions, the order of the arguments matters. That's why the input images are labeled with src and dst. A mask is usually the image labeled with src.

Getting started

The example below paints some text onto an image. The text is filled with the negative of the image.

WidgetMask(
  // `BlendMode.difference` results in the negative of `dst` where `src`
  // is fully white. That is why the text is white.
  blendMode: BlendMode.difference,
  mask: Center(
    child: Text(
      'Negative',
      style: TextStyle(
        fontSize: 50,
        color: Colors.white,
      ),
    ),
  ),
  child: Image.asset('images/my_image.jpg'),
);

WidgetMask delegates to child to lay itself out. It always has the same size as child. mask is forced to adopt the same size as child and positioned on top it.

During hit testing mask is positioned over child.

The different BlendModes use src and dst to describe how the colors of two images are blended with each other. In the context of WidgetMask mask is the src and child the dst.

Examples

The NegativeMaskedImageDemo widget, in the example app, implements the image at the top.

SaveLayer

This widget paints its child into a save layer and allows you to fully specify the Paint to use with the save layer. If you need to use a custom Paint or require a different layout of the widgets, you can use SaveLayer.

WidgetMask is implement using two SaveLayers to blend the mask and child:

@override
Widget build(BuildContext context) {
  Widget child = Stack(
    textDirection: TextDirection.ltr,
    fit: StackFit.passthrough,
    children: [
      this.child,
      Positioned.fill(
        child: SaveLayer(
          paint: Paint()..blendMode = blendMode,
          child: mask,
        ),
      ),
    ],
  );

  if (childSaveLayer) {
    child = SaveLayer(
      child: child,
    );
  }

  return child;
}

Gabriel Terwesten • GitHub @blaugold • Twitter @GTerwesten • Medium @gabriel.terwesten

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目