Giter Club home page Giter Club logo

widget_mask's Introduction

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 TerwestenGitHub @blaugoldTwitter @GTerwestenMedium @gabriel.terwesten

widget_mask's People

Contributors

blaugold avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

widget_mask's Issues

Question

Can i Play video inside mask widget?

Doesn't appear to work

I have white text
A fully opaque image
And no blend mode will actually mask the image

It always shows up with a black background

Screenshot 2023-05-01 at 10 18 03 PM

WidgetMask(
  blendMode: BlendMode.dstIn,
  mask: Text('Foo')
      .foregroundColor(Colors.white),
  child: Image.asset(
    "assets/space-1.png",
    fit: BoxFit.cover,
  ),
)

WidgetMask is not working flutter web using HTML version.

WidgetMask is not working on the flutter web HTML version. While it is working on the canvas kit version.

step to reproduce:
run the example project with this command.
flutter run -d chrome --web-renderer html --release <- Not working

Expected to run this example project as it runs on another platform, (eg. android/OSX/flutter web canvas kit). rather than showing the mask widget it is showing a stack view.
flutter build web --web-renderer canvaskit --release <- working

Not working with Image mask with transparent pixels

I'm trying to create a mask using an image which has a transparency (where it should be hidden). The result is that it's black where the pixels are transparent

image:
image

mask: (white with transparent)
bg_baixaria_forma_inner_redonda_1_2_mask

result:
res

I want that black parts become transparent (behind have the rest of the application)

code:
WidgetMask(
blendMode: BlendMode.dstATop,
mask: Image.asset(
"path_mask",
),
child: Image.asset(
"path_image",
),
)

Black mask on web for mobile

With

      blendMode: BlendMode.dstIn,
      childSaveLayer: true,

On a PC browser it works, but when it's viewed from a browser in a mobile, appears all the image and the mask is in black.

PC browser:
imagen

Smartphone browser:
imagen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.