MorphableShape 是一个 Flutter 包,可创建响应式、动态形状,能够平滑地在不同形态之间过渡。专为交互式和动画 UI 设计,它允许开发者定义自定义路径,并在多个平台上实时动画化其变形。适用于视觉效果、加载指示器或创意界面,通过直观的 API 简化复杂的路径动画。该包支持所有主要的 Flutter 平台,确保从移动设备到桌面和 Web 的行为一致。
适合场景
动画UI元素
交互式形状过渡
加载动画
创意界面设计
响应式形状组件
主要特点
平滑地在形状之间过渡
响应式设计支持
跨平台兼容性
自定义路径定义
实时动画控制
适合对象
寻求动态视觉效果的UI/UX设计师
开发交互式应用的开发者
需要流体形状动画的项目
原型化实验性界面
注重视觉叙事的应用程序
使用建议
复杂路径带来的性能影响
需要仔细定义路径以实现平滑过渡
可能需要针对低端设备进行优化
除了基本示例外,文档有限
最适合中等复杂度的动画
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
morphable_shape
A Flutter package for creating various shapes that are responsive
and can morph between each other.
Notice: This class uses the dimension package but only the px and percent units are respected since ShapeBorder has no access to the build context and the screen size.
Getting Started
First, you need to create a ShapeBorder instance. The responsive feature means that
you can have a single ShapeBorder instance that adapts to different sizes without you calculating the desired dimensions. For example, the following
code will give you a rounded rectangle border with a 60 px circular radius at the top left corner and a (60 px, 10%) elliptical corner at the bottom right corner.
(for more information of how to use the Length class, see dimension).
You can run the example app to create a local shape editing tool to see the various shapes supported by this package (also hosted online at https://fluttershape.com/).
DynamicBorderSide
The DynamicBorderSide is an extension to the built-in BorderSide class. It supports gradient filling in addtion to single color filling. It also supports painting partially by specifing the begin, end and offset parameters (e.g. paint only the first half of the border side). You can change the strokeJoin and strokeCap paramter as well.
This class is used to configure how the borders are painted with different shapes.
The most powerful and commonly used one should be the RectangleShapeBorder class. It allows to you configure each corner of the rectangle individually or at once.
It also automatically scales all sides so that they don’t overlap (just like what CSS does). The RectangleShapeBorder supports four different corner styles:
You can make a triangle, a diamond, a trapezoid, or even an arrow shape by just using the RectangleShapeBorder class and providing the right corner style and border radius.
CircleShapeBorder
CircleShapeBorder gives you a circle. Simple as that.
RoundedRectangleShapeBorder
If you use the RoundedRectangleShapeBorder class, then the four border sides can be configured individually. The four border sides can be styled independently or at once, similar to what CSS offers:
Below are some border designs using this class. This class is very similar to what CSS
offers and is a combination of the BoxBorder and RoundedRectangleBorder class that Flutter
offers.
PolygonShapeBorder
PolygonShapeBorder supports changing the number of sides as well as corner radius and corner style:
These shape borders are also supported and responsive. Check out their constructors to see how to make them.
PathShapeBorder
Accepts a DynamicPath instance to draw a custom path border. Right now only straight line and cubic Bezier curves are supported. Arcs need to be translated to cubic Beziers first. In the future I may let this class accept close SVG path as well.
Shape Morphing
Every shape in this package can be morphed into one another, including the border side(s). Hence the name of this package. To morph between two shapes you first need to create a ShapeBorderTween:
The MorphMethod.weighted tries to use as little control
points as possible to do the morphing and takes into account the length of each side of a shape. The MorphMethod.unweighted uses more points
but do not utilize the length information. The MorphMethod.auto will choose either
one of the two methods based on some geometric criteria to make the morphing
process to look more natural. The auto method generally works well, but you can
try other ones if the morphing looks weird.
Shapes with the same geometry can be morphed faster and in a more consistent way. For example, rectangles and rounded rectangles, or polygons with the same number of sides. For other shapes, the morphing takes more time and may not look great especially for two very distinct shapes.
Shape Serialization
Every shape in this package supports serialization. If you have designed some shape you like, just call toJson() on it. Then you can reuse it by writting.
As I mentioned before, the example app in this package is a shape editing tool for
you to quickly design the shape you want and generate the corresponding code. Below are
some screenshots of the interfaces of this tool. I put about equal amount of effort
into developing this tool compared to developing this package so I strongly recommend you
to check it out. You can either build it locally or visit:
https://fluttershape.com/.