Flutter线条绘制插件idkit_line的使用

Flutter线条绘制插件idkit_line的使用

idkit_line 是一个方便开发的工具包,用于在 Flutter 项目中扩展分隔线的需求。通过该插件,可以轻松创建实线、虚线、波浪线以及删除线等多种样式。

包扩展方法的使用案例

实线 (solid)

IDKitLine.solid(),
const SizedBox(height: 20),
IDKitLine.solid(
    color: Colors.red, // 设置颜色为红色
    width: 300,        // 设置宽度为300
    height: 5,         // 设置高度为5
    thickness: 3,      // 设置厚度为3
),
const SizedBox(height: 20),
IDKitLine.solid(
    color: Colors.purple, // 设置颜色为紫色
    width: 300,           // 设置宽度为300
    axis: Axis.vertical,  // 垂直方向
    height: 20,           // 设置高度为20
)

虚线 (dotted)

IDKitLine.dotted(),
const SizedBox(height: 20),
IDKitLine.dotted(
    dashLength: 5,   // 设置虚线段长度为5
    interval: 3,     // 设置间隔长度为3
    thickness: 3,    // 设置厚度为3
),
const SizedBox(height: 20),
IDKitLine.dotted(
    width: 200,      // 设置宽度为200
    color: Colors.purple, // 设置颜色为紫色
    dashLength: 5,   // 设置虚线段长度为5
    interval: 3,     // 设置间隔长度为3
),
const SizedBox(height: 20),
IDKitLine.dotted(
    dashLength: 5,   // 设置虚线段长度为5
    interval: 3,     // 设置间隔长度为3
    dottedType: DottedType.dash_dot, // 设置虚线类型为虚点线
),
const SizedBox(height: 20),
IDKitLine.dotted(
    width: 200,      // 设置宽度为200
    color: Colors.purple, // 设置颜色为紫色
    dashLength: 10,  // 设置虚线段长度为10
    interval: 3,     // 设置间隔长度为3
    thickness: 4,    // 设置厚度为4
    dottedType: DottedType.dash_dot, // 设置虚线类型为虚点线
)

波浪线 (wavy)

IDKitLine.wavy(),
const SizedBox(height: 20),
IDKitLine.wavy(
    color: Colors.red, // 设置颜色为红色
    width: 300,        // 设置宽度为300
),
const SizedBox(height: 20),
IDKitLine.wavy(
    axis: Axis.vertical, // 设置为垂直方向
    height: 200,         // 设置高度为200
    thickness: 4,        // 设置厚度为4
    a: 6,                // 波浪参数a为6
    w: 0.2,              // 波浪参数w为0.2
)

删除线 (delete)

IDKitLine.delete(
  child: const Text('¥:10'), // 显示文本为¥:10
  thickness: 2,               // 设置厚度为2
  color: Colors.red,          // 设置颜色为红色
),
const SizedBox(height: 20),
IDKitLine.delete(
  child: const Text('¥:10000000000'), // 显示文本为¥:10000000000
  thickness: 4,                        // 设置厚度为4
  color: Colors.purple,                // 设置颜色为紫色
),

注意事项

如果想了解更多关于 idkitline 的用法,请访问 GitHub 并搜索 idkitline

完整示例代码

以下是完整的示例代码,展示了如何在 Flutter 项目中使用 idkit_line 插件:

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Extension test of line package'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            IDKitLine.solid(), // 实线
            const SizedBox(height: 20),
            IDKitLine.solid(
              color: Colors.red,
              width: 300,
              height: 5,
              thickness: 3,
            ), // 自定义实线
            const SizedBox(height: 20),
            IDKitLine.solid(
              color: Colors.purple,
              width: 300,
              axis: Axis.vertical,
              height: 20,
            ), // 垂直方向的实线
            const SizedBox(height: 20),
            IDKitLine.dotted(), // 虚线
            const SizedBox(height: 20),
            IDKitLine.dotted(
              dashLength: 5,
              interval: 3,
              thickness: 3,
            ), // 自定义虚线
            const SizedBox(height: 20),
            IDKitLine.dotted(
              width: 200,
              color: Colors.purple,
              dashLength: 5,
              interval: 3,
            ), // 自定义虚线样式
            const SizedBox(height: 20),
            IDKitLine.dotted(
              dashLength: 5,
              interval: 3,
              dottedType: DottedType.dash_dot,
            ), // 虚点线
            const SizedBox(height: 20),
            IDKitLine.dotted(
              width: 200,
              color: Colors.purple,
              dashLength: 10,
              interval: 3,
              thickness: 4,
              dottedType: DottedType.dash_dot,
            ), // 自定义虚点线
            const SizedBox(height: 20),
            Row(
              children: [
                IDKitLine.dotted(
                  width: 200,
                  height: 30,
                  color: Colors.purple,
                  dashLength: 5,
                  interval: 3,
                  axis: Axis.vertical,
                ),
                IDKitLine.dotted(
                  width: 200,
                  height: 30,
                  color: Colors.purple,
                  dashLength: 5,
                  interval: 3,
                  dottedType: DottedType.dash_dot,
                  axis: Axis.vertical,
                ),
              ],
            ),
            const SizedBox(height: 20),
            IDKitLine.wavy(), // 波浪线
            const SizedBox(height: 20),
            IDKitLine.wavy(
              color: Colors.red,
              width: 300,
            ), // 自定义波浪线
            const SizedBox(height: 20),
            IDKitLine.wavy(
              axis: Axis.vertical,
              height: 200,
              thickness: 4,
              a: 6,
              w: 0.2,
            ), // 垂直方向的波浪线
            const SizedBox(height: 20),
            IDKitLine.delete(
              child: const Text('¥:10'),
              thickness: 2,
              color: Colors.red,
            ), // 删除线
            const SizedBox(height: 20),
            IDKitLine.delete(
              child: const Text('¥:10000000000'),
              thickness: 4,
              color: Colors.purple,
            ), // 自定义删除线
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


idkit_line 是一个用于在 Flutter 中绘制线条的插件。它提供了简单易用的 API,可以帮助开发者在 Flutter 应用中绘制各种类型的线条,包括直线、曲线、虚线等。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 idkit_line 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  idkit_line: ^1.0.0  # 请确保使用最新版本

然后运行 flutter pub get 来安装插件。

基本用法

以下是一些使用 idkit_line 插件绘制线条的基本示例。

1. 绘制直线

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

class LineExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Line Example'),
      ),
      body: Center(
        child: Container(
          width: 300,
          height: 200,
          child: CustomPaint(
            painter: LinePainter(),
          ),
        ),
      ),
    );
  }
}

class LinePainter extends CustomPainter {
  [@override](/user/override)
  void paint(Canvas canvas, Size size) {
    final line = Line()
      ..start = Offset(0, size.height / 2)
      ..end = Offset(size.width, size.height / 2)
      ..color = Colors.blue
      ..strokeWidth = 2.0;

    line.paint(canvas);
  }

  [@override](/user/override)
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}

2. 绘制虚线

class DashedLinePainter extends CustomPainter {
  [@override](/user/override)
  void paint(Canvas canvas, Size size) {
    final line = Line()
      ..start = Offset(0, size.height / 2)
      ..end = Offset(size.width, size.height / 2)
      ..color = Colors.red
      ..strokeWidth = 2.0
      ..dashPattern = [10, 5];  // 设置虚线模式

    line.paint(canvas);
  }

  [@override](/user/override)
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}

3. 绘制曲线

class CurveLinePainter extends CustomPainter {
  [@override](/user/override)
  void paint(Canvas canvas, Size size) {
    final path = Path()
      ..moveTo(0, size.height / 2)
      ..quadraticBezierTo(size.width / 2, size.height, size.width, size.height / 2);

    final line = Line()
      ..path = path
      ..color = Colors.green
      ..strokeWidth = 3.0;

    line.paint(canvas);
  }

  [@override](/user/override)
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}
回到顶部