Flutter特效绘制插件neonpen的使用

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

Flutter特效绘制插件neonpen的使用

Neonpen

一个用于在文本上添加霓虹笔效果的Flutter插件。该插件提供了颜色、透明度、锯齿、双层等选项。

安装

在你的pubspec.yaml文件中添加以下依赖:

dependencies:
  neonpen: ^2.0.5

然后在你的Dart文件中导入该库:

import 'package:neonpen/neonpen.dart';

如何使用

通过简单的添加带有必要参数的Neonpen小部件来使用该插件。以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, 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: <Widget>[
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.red,
              opacity: 0.3,
              emphasisWidth: 5,
              emphasisOpacity: 0.3,
              emphasisAngleDegree: 0.5,
              enableLineZiggle: true,
              lineZiggleLevel: 0,
              isDoubleLayer: false,
            ),
            SizedBox(height: 30),
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.yellow,
              padding: EdgeInsets.symmetric(vertical: 5, horizontal: 15),
              opacity: 0.3,
              disableEmphasis: true,
              lineZiggleLevel: 1,
              isDoubleLayer: false,
            ),
            SizedBox(height: 30),
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.orange,
              padding: EdgeInsets.only(
                top: -10,
                left: 5,
                right: 5,
              ),
              opacity: 0.3,
              disableEmphasis: true,
              enableLineZiggle: true,
              lineZiggleLevel: 1,
              isDoubleLayer: false,
            ),
            SizedBox(height: 30),
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.grey,
              padding: EdgeInsets.symmetric(vertical: 5, horizontal: 15),
              opacity: 0.7,
              emphasisWidth: 5,
              emphasisOpacity: 0.5,
              emphasisAngleDegree: 0.5,
              enableLineZiggle: true,
              lineZiggleLevel: 1,
              isDoubleLayer: false,
            ),
            SizedBox(height: 30),
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.blue,
              padding: EdgeInsets.symmetric(vertical: 5, horizontal: 15),
              opacity: 0.3,
              emphasisWidth: 15,
              emphasisOpacity: 0.3,
              emphasisAngleDegree: 2,
              enableLineZiggle: true,
              lineZiggleLevel: 1,
              isDoubleLayer: false,
            ),
            SizedBox(height: 30),
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.purple,
              padding: EdgeInsets.symmetric(vertical: 5, horizontal: 15),
              opacity: 0.3,
              emphasisWidth: 10,
              emphasisOpacity: 0.3,
              emphasisAngleDegree: 2,
              enableLineZiggle: true,
              lineZiggleLevel: 2,
              isDoubleLayer: false,
            ),
            SizedBox(height: 30),
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.green,
              padding: EdgeInsets.symmetric(vertical: 5, horizontal: 20),
              opacity: 0.3,
              emphasisWidth: 5,
              emphasisOpacity: 0.3,
              emphasisAngleDegree: 0.5,
              enableLineZiggle: true,
              lineZiggleLevel: 1,
              isDoubleLayer: true,
            ),
            SizedBox(height: 30),
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.lime,
              padding: EdgeInsets.symmetric(vertical: 5, horizontal: 20),
              opacity: 0.3,
              emphasisWidth: 5,
              emphasisOpacity: 0.3,
              emphasisAngleDegree: 2,
              enableLineZiggle: true,
              lineZiggleLevel: 1,
              isDoubleLayer: true,
            ),
            SizedBox(height: 30),
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.amber,
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 25),
              opacity: 0.3,
              emphasisWidth: 5,
              emphasisOpacity: 0.3,
              emphasisAngleDegree: 0.5,
              enableLineZiggle: true,
              lineZiggleLevel: 1,
              isDoubleLayer: true,
            ),
            SizedBox(height: 30),
            Neonpen(
              text: Text(
                'Hello World!',
                style: TextStyle(
                  fontSize: 15,
                ),
              ),
              color: Colors.indigo,
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 25),
              opacity: 0.3,
              emphasisWidth: 8,
              emphasisOpacity: 0.1,
              emphasisAngleDegree: 0.8,
              enableLineZiggle: true,
              lineZiggleLevel: 1,
              isDoubleLayer: true,
            ),
            SizedBox(height: 30),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter中使用neonpen插件进行特效绘制的代码示例。neonpen是一个用于绘制具有霓虹效果的图形的Flutter插件。假设你已经在pubspec.yaml文件中添加了neonpen依赖,并运行了flutter pub get来安装它。

首先,确保你的pubspec.yaml包含以下依赖:

dependencies:
  flutter:
    sdk: flutter
  neonpen: ^最新版本号  # 请替换为实际的最新版本号

接下来,以下是一个简单的示例,展示如何使用neonpen绘制具有霓虹效果的图形:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('NeonPen Example'),
        ),
        body: Center(
          child: NeonDrawingBoard(
            onDraw: _onDraw,
            neonStyle: NeonStyle(
              color: Colors.cyanAccent,
              strokeWidth: 5.0,
            ),
          ),
        ),
      ),
    );
  }

  void _onDraw(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..color = Colors.transparent
      ..style = PaintingStyle.stroke
      ..strokeWidth = 5.0;

    // 创建一个路径
    Path path = Path();
    path.moveTo(size.width * 0.1, size.height * 0.5);
    path.lineTo(size.width * 0.5, size.height * 0.1);
    path.lineTo(size.width * 0.9, size.height * 0.5);
    path.lineTo(size.width * 0.5, size.height * 0.9);
    path.close();

    // 使用NeonPen绘制路径
    NeonPen.drawPath(
      canvas: canvas,
      path: path,
      neonStyle: NeonStyle(
        color: Colors.lightBlueAccent,
        strokeWidth: 5.0,
        blurRadius: 10.0,
        spreadRadius: 5.0,
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个自定义的NeonDrawingBoard。这个自定义组件允许我们通过onDraw回调在画布上绘制内容。

_onDraw方法中,我们创建了一个简单的路径(一个菱形),并使用NeonPen.drawPath方法将其绘制到画布上。NeonStyle类允许我们自定义霓虹效果的颜色、笔触宽度、模糊半径和扩展半径。

请注意,NeonPen是一个假设的插件名称,实际上可能不存在一个名为neonpen的官方Flutter插件。不过,基于类似的插件或自定义绘制逻辑,上述代码示例提供了一个如何使用Flutter绘制具有霓虹效果的图形的基本思路。如果你确实找到一个名为neonpen的插件,并希望使用它,请参考该插件的官方文档进行适配。

如果没有一个现成的neonpen插件,你可以考虑使用CustomPainterShaderMask等Flutter提供的绘制工具来实现类似的霓虹效果。

回到顶部