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

zwidget 封面
插件详情v0.0.2

zwidget

将几乎任何小部件转换为伪3D版本,可轻松进行动画处理。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
38
喜欢数
68
Pub 分数
150

安装插件

flutter pub add zwidget

插件介绍

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

ZWidget 是一个 Flutter 包,可将任何 widget 转换为伪 3D 版本,实现流畅且直观的 3D 类动画效果。通过应用深度和透视效果,它在无需复杂 3D 渲染的情况下增强视觉吸引力。非常适合创建沉浸式 UI 元素,如带有动态深度响应的卡片、按钮或容器。该包支持所有主要平台,包括移动设备、网页和桌面端,确保在各种设备上行为一致。

适合场景

  • 创建动画3D卡片
  • 增强按钮交互
  • 构建沉浸式 UI 组件
  • 为静态小部件增加深度
  • 设计交互式产品预览
  • 动画布局过渡

主要特点

  • 伪3D变换
  • 基于深度的平滑动画
  • 跨平台兼容性
  • 与现有小部件的轻松集成
  • 可自定义的视角和倾斜
  • 支持触控和手势输入

适合对象

  • 寻求视觉深度的UI设计师
  • 开发交互式原型的开发者
  • 需要引人入胜的微交互的应用程序
  • 针对多个平台的项目
  • 创意数字体验

使用建议

  • 对低端设备的性能影响
  • 有限的真实3D几何
  • 需要仔细调整动画参数
  • 不适合复杂的3D场景
  • 可能会略微增加构建大小

原始 README

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

ZWidget creates a 3D-like visual by stacking widgets one above the other and transforming them adequately.

This package is heavily inspired by ztext.js.

Since Text is a Widget, and since everything is a Widget in Flutter, apply your 3D effect to any element of your UI!

It was created for the Flutter Puzzle Hack.

Features

Tilt your widgets and simulate 3D easily. Here are a few examples with Text, FlutterLogo, Ìmage, Icon and CustomPaint:

Various static examples

You can also animate them:

Demo gif

The example project includes these demos and a few more.

Usage

ZWidget includes several constructors depending on the direction where you want your perspective effect to be.

ZWidget.backwards(), ZWidget.forwards() and ZWidget.bothDirections()

ZWidget direction constructors

You must define at least the midChild, which is the base Widget that will be displayed. Depending on the direction you want the perspective to take place, you may also define the extremities and the widget to display between them and the middle child.

Here is a full example for ZWidget.bothDirections():

ZWidget.bothDirections(
  // middle layer
  midChild: Container(
    width: 60,
    height: 60,
    color: Colors.blue,
  ),
  midToBotChild: Container(
    width: 60,
    height: 60,
    color: Colors.blueGrey,
  ),
  // layers between mid and top child
  midToTopChild: Container(
    width: 60,
    height: 60,
    color: Colors.lightBlue,
  ),
  // layer at the bottom of the Stack
  botChild: Container(
    width: 60,
    height: 60,
    color: Colors.grey,
  ),
  // layer at the top of the Stack
  topChild: Container(
    width: 60,
    height: 60,
    color: Colors.lightBlueAccent,
  ),
  // rotation around X axis
  rotationX: pi / 3,
  // rotation around Y axis
  rotationY: -pi / 4,
  // Number of layers. Always odd (increased if even)
  layers: 5,
  // Space between layers
  depth: 32,
)

ZWidget.backwards() and ZWidget.forwards() are similar but they provide only the children parameters adequate to their direction.

The layers of a ZWidget are visibles in the picture below:

ZWidget layers

There is always an odd number of layers. If you define an even number, it will be increased to be an odd number. Increase depth to increase their spacing.

ZDirection can be forward, backward or both.

Mid layer is the midChild. Depending on the ZDirection, other layers are displayed above or below the midChild. You can define the botChild for the child at the bottom of the Stack and topChild for the one at the top. You can also define Widgets between these extremities and midChild with midToTopChild and midToBotChild.

Use the alignment property to set the Transform origin.

To effectively see the 3D effect, you must define a rotation to the ZWidget using rotationX and rotationY.

ZWidget.builder()

In case you would like to control which Widget is displayed at each layer, you may use Widget.builder(). It can be used to make a gradient effect for instance.

Here are a few examples:

ZWidget builder examples

ZWidget()

While the base ZWidget constructor works, it may lead newcomers to define parameters that are not needed. Therefore, it is now deprecated and you should use the other constructors instead.

Performance considerations

If ZWidget has 11 layers, it means that 11 Widgets will be drawn. Try to limit the complexity of widgets used at each layer.

For instance, an opaque Image could use simple Containers to simulate a 3D effect:


final child = ZWidget.forwards(
  midChild: Container(
    color: Colors.black,
    // ...
  ),
  topChild: Image(
    // ...
  ),
  rotationX: pi / 3,
  rotationY: -pi / 4,
  layers: 11,
  depth: 16,
);

All parameters

  // The builder function used to build each layer
  final Widget Function(int) builder;
  // Space between layers
  final double depth;
  // Perspective direction
  final ZDirection direction;
  // Rotation around X axis
  final double rotationX;
  // Rotation around Y axis
  final double rotationY;
  // Number of layers. Always odd (increased if set even)
  final int layers;
  // Experimental, used to reverse the X and Y rotations
  final bool reverse;
  // Shows a pink border representing the size taken by this `ZWidget`.
  // Painting may go beyond that border.
  final bool debug;
  // The bigger the perspective, the more part of the widget will seem to be far away.
  final double perspective;
  // Transform origin
  final Alignment? alignment;
    // A fade effect (experimental)
  final bool fade;

ZWidget size

Depending on the transformations and the 3D effect your want to achieve, your widget might be drawn beyond its limits.

The debug parameter shows a pink border around your ZWidget which indicates its layout size in the widget tree but the painting may go beyond that border.

That's because the Transform that we use doesn't change the base size of the widget, it just changes how it is rendered. The layout remains the same, but the painting can change a lot. See the Transform documentation for more infos. You might want to cut it to fit in the border with a ClipRect for instance.

How does it work?

ZWidget fills a Stack with layers Widgets returned by a builder function. These are then transformed to make the 3D effect. This transformations is done using a Transform widget and a Matrix4. If you'd like to do something similar but not with Widget, you can use the top-level function zMatrix4().

zMatrix4

This method returns a Matrix4 used to perform 3D transformations. You can use it on anything accepting a Matrix4 (Widget, Canvas, Path, ...).

Widget example
Transform(
 transform: zMatrix4(xTilt: pi / 3, yTilt: pi / 4, perspective: 5),
 child: Container(
   color: Colors.red,
   width: 100,
   height: 100,
 ),
);

ZWidget uses zMatrix4() this way.

Canvas example
canvas.transform(zMatrix4(xTilt: pi/3).storage);
Path example
Path p = Path()
 ..addOval(Rect.fromCenter(
      center: Offset(size.width / 2, size.height / 2),
      width: size.width / 4,
      height: size.height / 4))
  ..transform(zMatrix4(xTilt: pi / 3).storage);
canvas.drawPath(p, Paint()..color = Colors.amber);

Using this package

Contact me if you are using this package and I will add you to the list!

所属分类

源码仓库

使用此插件的项目

暂无相关数据。