Flutter图形绘制插件figma_squircle_updated的使用

发布于 1周前 作者 nodeper 来自 Flutter

Flutter图形绘制插件figma_squircle_updated的使用

image

使用

装饰(Decoration)

SmoothRectangleBorder 可以通过自定义的 SmoothBorderRadius 提供给常规的 ShapeDecoration

Container(
    height: 100,
    width: 100,
    decoration: ShapeDecoration(
        color: Colors.red.withOpacity(0.75), // 设置颜色为红色,透明度为0.75
        shape: SmoothRectangleBorder(
            borderRadius: SmoothBorderRadius(
              cornerRadius: 10, // 设置圆角半径为10
              cornerSmoothing: 0.5, // 设置圆角平滑度为0.5
            ),
        ),
    ),
)

独立设置每个角(Only)

每个角可以有独立的平滑度和半径。

SmoothBorderRadius.only(
    topLeft: SmoothRadius(
        cornerRadius: 10, // 设置左上角圆角半径为10
        cornerSmoothing: 1, // 设置左上角圆角平滑度为1
    ),
    topRight: SmoothRadius(
        cornerRadius: 20, // 设置右上角圆角半径为20
        cornerSmoothing: 0.4, // 设置右上角圆角平滑度为0.4
    ),
    bottomLeft: SmoothRadius(
        cornerRadius: 5, // 设置左下角圆角半径为5
        cornerSmoothing: 0.8, // 设置左下角圆角平滑度为0.8
    ),
    bottomRight: SmoothRadius(
        cornerRadius: 30, // 设置右下角圆角半径为30
        cornerSmoothing: 0.6, // 设置右下角圆角平滑度为0.6
    ),
),

剪裁(Clip)

要使用平滑矩形剪裁任何小部件,可以使用 ClipSmoothRect

ClipSmoothRect(
    radius: SmoothBorderRadius(
        cornerRadius: 10, // 设置圆角半径为10
        cornerSmoothing: 1, // 设置圆角平滑度为1
    ),
    child: Container(
        color: Colors.red, // 设置颜色为红色
        width: 100, // 宽度为100
        height: 100, // 高度为100
    ),
)

更多关于Flutter图形绘制插件figma_squircle_updated的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter图形绘制插件figma_squircle_updated的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何使用 figma_squircle_updated Flutter 插件进行图形绘制的示例代码。这个插件允许你在 Flutter 应用中绘制 Squircle(方形圆角)图形。

首先,确保你已经在 pubspec.yaml 文件中添加了 figma_squircle_updated 依赖:

dependencies:
  flutter:
    sdk: flutter
  figma_squircle_updated: ^最新版本号

然后运行 flutter pub get 来获取依赖。

接下来,你可以在 Flutter 应用中使用这个插件来绘制 Squircle。以下是一个完整的示例代码:

import 'package:flutter/material.dart';
import 'package:figma_squircle_updated/figma_squircle_updated.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SquircleDemo(),
    );
  }
}

class SquircleDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Squircle Demo'),
      ),
      body: Center(
        child: CustomPaint(
          size: Size(300, 300),
          painter: SquirclePainter(
            color: Colors.blue,
            rect: Rect.fromLTWH(50, 50, 200, 200),
            borderRadius: BorderRadius.circular(50), // This is actually used for squircle effect
            squircleRatio: 0.5, // Adjust this value to change the squircle shape
          ),
        ),
      ),
    );
  }
}

class SquirclePainter extends CustomPainter {
  final Color color;
  final Rect rect;
  final BorderRadius borderRadius;
  final double squircleRatio;

  SquirclePainter({
    required this.color,
    required this.rect,
    required this.borderRadius,
    required this.squircleRatio,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;

    // Calculate the squircle path
    final Path path = SquirclePath()
        .addOval(rect, borderRadius: borderRadius, squircleRatio: squircleRatio);

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

// Note: SquirclePath is a hypothetical class since figma_squircle_updated does not provide a direct SquirclePath.
// You may need to implement the squircle path calculation yourself using the provided ratio.
// Below is a simplified version of how you might calculate a squircle path:
class SquirclePath {
  Path addOval(Rect rect, {required BorderRadius borderRadius, required double squircleRatio}) {
    final Path path = Path();
    final double width = rect.width;
    final double height = rect.height;
    final double radiusX = width / 2;
    final double radiusY = height / 2;
    final double cornerSize = radiusX * squircleRatio; // Adjust corner size based on squircle ratio

    // Top-left corner
    path.moveTo(rect.left + cornerSize, rect.top);
    path.quadraticBezierTo(
      rect.left,
      rect.top,
      rect.left,
      rect.top + cornerSize,
    );

    // Top-right corner
    path.quadraticBezierTo(
      rect.left,
      rect.top + radiusY - cornerSize,
      rect.right - cornerSize,
      rect.top + radiusY,
    );
    path.lineTo(rect.right - cornerSize, rect.bottom - radiusY);

    // Bottom-right corner
    path.quadraticBezierTo(
      rect.right,
      rect.bottom - radiusY,
      rect.right,
      rect.bottom - cornerSize,
    );
    path.lineTo(rect.right, rect.bottom - cornerSize);

    // Bottom-left corner
    path.quadraticBezierTo(
      rect.right,
      rect.bottom,
      rect.right - cornerSize,
      rect.bottom - radiusY + cornerSize,
    );
    path.lineTo(rect.left + cornerSize, rect.bottom - radiusY + cornerSize);

    // Closing the path to form a squircle shape
    path.quadraticBezierTo(
      rect.left + radiusX - cornerSize,
      rect.bottom,
      rect.left + radiusX,
      rect.bottom - cornerSize,
    );
    path.lineTo(rect.left + radiusX, rect.top + cornerSize);

    // Completing the top-left corner
    path.quadraticBezierTo(
      rect.left + radiusX,
      rect.top,
      rect.left + cornerSize,
      rect.top,
    );

    path.close();
    return path;
  }
}

注意

  1. figma_squircle_updated 插件可能并没有直接提供一个 SquirclePath 类。上述代码中的 SquirclePath 类是一个假设的实现,用于展示如何根据 squircle ratio 计算路径。
  2. 实际的插件使用可能会有所不同,请参考插件的官方文档和示例代码进行调整。
  3. 由于 figma_squircle_updated 插件的具体 API 可能会随时间变化,请查阅最新的插件文档以确保代码的准确性。

如果你发现 figma_squircle_updated 插件提供了直接的 Squircle 绘制方法,那么你应该直接使用插件提供的方法,而不是自己实现路径计算。

回到顶部