一个 Flutter 包,可实现对画布元素或小部件的可自定义图案(如条纹、圆点和网格)的创建。它非常适合在注重设计和美学的应用中添加装饰性背景、纹理或视觉效果。该包支持 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台,适用于跨平台开发。通过简单的 API,开发者可以轻松地将图案渲染集成到其 UI 组件中。
适合场景
创建纹理背景
设计自定义 UI 元素
添加装饰性叠加层
生成重复图案
通过着色器增强应用视觉效果
构建艺术界面
主要特点
支持条纹、点状和网格图案
跨平台兼容性
与 Canvas 和 Widgets 的轻松集成
可自定义的图案大小和颜色
高性能渲染
可扩展的模式定义
适合对象
寻求视觉多样性的UI设计师
开发艺术或创意应用的开发者
需要动态背景效果的应用
专注于绘画和涂鸦的项目
致力于多平台设计的团队
使用建议
可能会影响复杂模式的性能
需要了解 Canvas 渲染
仅限基于模式的图形
不适合高精度矢量图形
最好与优化的渲染策略配合使用
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Patterns
Flutter library to draw patterns like stripes or dots on Canvas elements or widgets.
patterns_canvas
A utility library that lets you paint Canvas shapes or widgets with patterns like dots, stripes, squares, etc.:
Simple Example
Inside your CustomPainter class's paint method, create a Pattern class with background (bgColor) and foreground (fgColor) colors and use one of its paintOn methods:
classMyPainterextendsCustomPainter{
@overridevoidpaint(Canvas canvas, Size size) {
// Prepare a rectangle shape to draw the pattern on.final rect = Rect.fromLTWH(80, 50, 200, 100);
// Create a Pattern object of diagonal stripes with the colors we want.finalPattern pattern = DiagonalStripesThick(bgColor: Colors.lightGreenAccent, fgColor: Colors.black);
// Paint the pattern on the rectangle.
pattern.paintOnRect(canvas, size, rect);
}
}
Results in this pattern drawn:
Pattern constructors and types:
You can construct a Pattern with three different ways:
Rectangular widgets, like Containers, Rows, Columns, IconButtons, etc. can be painted directly with the above method. For widgets that have a custom shape, like a BottomAppbar, you need to provide a different clipBehavior property, e.g. Clip.antiAlias, to make sure the pattern is clipped to the special shape. Check the second screen in the example app for more.
Patterns and settings
Available patterns
All Patterns require a background and foreground color on instantiation. The currently available patterns are:
Description
Class
PatternType enum
Horizontal Stripes Light
HorizontalStripesLight(Color bgColor, Color fgColor)
PatternType.horizontalLight
Horizontal Stripes Thick
HorizontalStripesThick(Color bgColor, Color fgColor)
PatternType.horizontalThick
Vertical Stripes Light
VerticalStripesLight(Color bgColor, Color fgColor)
PatternType.verticalLight
Vertical Stripes Thick
VerticalStripesThick(Color bgColor, Color fgColor)
PatternType.verticalThick
Diagonal Stripes Light
DiagonalStripesLight(Color bgColor, Color fgColor)
PatternType.diagonalLight
Diagonal Stripes Thick
DiagonalStripesThick(Color bgColor, Color fgColor)
PatternType.diagonalThick
Checkers
Checkers(Color bgColor, Color fgColor)
PatternType.checkers
Crosshatch
Crosshatch(Color bgColor, Color fgColor)
PatternType.crosshatch
Dots
Dots(Color bgColor, Color fgColor)
PatternType.dots
Raindrops
Raindrops(Color bgColor, Color fgColor)
PatternType.raindrops
Subtle Patch
SubtlePatch(Color bgColor, Color fgColor)
PatternType.subtlepatch
Texture
TexturePattern(Color bgColor, Color fgColor)
PatternType.texture
Setting the number of pattern features to draw
You can pass a featuresCount argument when creating a Pattern, to set how many features, e.g. how many stripes, you want to be drawn.
All patterns are by default scaled to their container (patternScaleBehavior = PatternScaleBehavior.container). This means that, regardless of its size, a Rectangle will contain 15 diagonal stripes or 10 dots.
You can change this behavior by providing a different argument to the paintOn method:
Pass patternScaleBehavior: PatternScaleBehavior.canvas to scale the pattern to the whole Canvas area. Now the Canvas will contain 15 diagonal stripes or 10 dots, so, a Rectangle will contain as many dots or stripes as it can fit:
Pass patternScaleBehavior: PatternScaleBehavior.customRect and a customRect (e.g. a Rectangle with half the Canvas height) to further customize the Pattern size: