Flutter渐变发光边框插件gradient_glow_border的使用

Flutter渐变发光边框插件gradient_glow_border的使用

一个用于在 Flutter 中创建带有动画渐变边框的插件,支持普通、虚线和点线样式。此插件允许你轻松地为你的小部件添加具有可定制颜色、半径、厚度和动画效果的边框。

特性

  • 动画渐变边框:为任何小部件创建平滑的动画渐变边框。
  • 可定制的边框样式
    • 普通渐变:一个实心的动画渐变边框。
    • 虚线渐变:具有自定义虚线间距的虚线动画渐变边框。
    • 点线渐变:具有自定义点间距的点状动画渐变边框。
  • 可定制的边框圆角:为边框的每个角应用不同的圆角半径。
  • 发光效果:为边框添加发光阴影效果。
  • 完全可定制:控制动画持续时间、渐变颜色、厚度等。

安装

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

dependencies:
  gradient_glow_border: ^1.0.3

然后运行 flutter pub get 来安装包。

使用

1. 普通渐变边框

使用 GradientGlowBorder.normalGradient 构造函数来创建一个实心的动画渐变边框:

GradientGlowBorder.normalGradient(
  borderRadius: BorderRadius.circular(15),
  blurRadius: 1,
  spreadRadius: 1,
  colors: [Colors.blue, Colors.red],
  glowOpacity: 1,
  duration: Duration(milliseconds: 800),
  thickness: 3,
  child: YourChildWidget(),
)

Normal Gradient

2. 虚线渐变边框

使用 GradientGlowBorder.dashedGradient 构造函数来创建一个虚线的动画渐变边框:

GradientGlowBorder.dashedGradient(
  borderRadius: BorderRadius.circular(15),
  blurRadius: 1,
  spreadRadius: 0,
  colors: [Colors.blue, Colors.red],
  glowOpacity: 1,
  duration: Duration(milliseconds: 800),
  thickness: 3,
  dashSpace: 5, // 虚线之间的间距
  child: YourChildWidget(),
)

Dashed Gradient

3. 点线渐变边框

使用 GradientGlowBorder.dottedGradient 构造函数来创建一个点线的动画渐变边框:

GradientGlowBorder.dottedGradient(
  borderRadius: BorderRadius.circular(20),
  blurRadius: 1,
  spreadRadius: 0,
  colors: [Colors.blue, Colors.red],
  glowOpacity: 1,
  duration: Duration(milliseconds: 800),
  thickness: 3,
  dottedSpace: 5, // 点之间的间距
  child: YourChildWidget(),
)

Dotted Gradient

快速参考

由于自定义需要很多属性,这里是一个快速的备忘单:

参数名 类型 功能描述
borderRadius BorderRadius 每个角的边框圆角半径
spreadRadius double 发光效果的扩散半径
glowOpacity double 发光效果的不透明度
dottedSpace double 点之间的间距(仅用于点线边框)
dashSpace double 虚线之间的间距(仅用于虚线边框)
blurRadius double 发光效果的模糊半径
duration Duration 动画的持续时间
thickness double 边框的厚度
colors List<Color> 渐变的颜色列表
child Widget? 应用动画边框的小部件

示例

以下是使用 GradientGlowBorder 包的一个完整示例:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.black,
        body: Center(
          child: SizedBox(
                height: 70,
                width: double.infinity,
                child: GradientGlowBorder.normalGradient(
                  borderRadius: BorderRadius.all(Radius.circular(15)),
                  blurRadius: 1,
                  colors: [
                    Colors.blue,
                    Colors.red,
                  ],
                  duration: Duration(milliseconds: 500),
                  glowOpacity: 1,
                  spreadRadius: 0,
                  thickness: 3,
                  child: Center(
                    child: Text("Mohamed Elbaiomy"),
                  ),
                ),
              ),
        ),
      ),
    );
  }
}

更多关于Flutter渐变发光边框插件gradient_glow_border的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter渐变发光边框插件gradient_glow_border的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter中使用gradient_glow_border插件来实现渐变发光边框的示例代码。假设你已经在pubspec.yaml文件中添加了gradient_glow_border依赖并运行了flutter pub get

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

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

然后,你可以在你的Flutter项目中使用GradientGlowBorder来创建渐变发光边框。以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Gradient Glow Border Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Gradient Glow Border Example'),
        ),
        body: Center(
          child: Container(
            decoration: BoxDecoration(
              shape: BoxShape.circle,  // 可以根据需要调整形状
              border: Border.all(
                color: Colors.transparent,  // 透明边框,因为我们使用自定义的渐变发光边框
                width: 10.0,
              ),
            ),
            child: GradientGlowBorder(
              // 设置发光边框的颜色渐变
              gradient: LinearGradient(
                colors: [Colors.red, Colors.yellow],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
              // 设置边框的宽度和发光的模糊半径
              borderWidth: 10.0,
              blurRadius: 20.0,
              // 设置子组件
              child: Container(
                width: 200.0,
                height: 200.0,
                decoration: BoxDecoration(
                  color: Colors.white,  // 内部颜色
                  shape: BoxShape.circle,  // 内部形状
                ),
                child: Center(
                  child: Text(
                    'Glow!',
                    style: TextStyle(fontSize: 24, color: Colors.black),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个圆形的容器,并在其周围应用了一个渐变发光边框。GradientGlowBorder组件接收以下参数:

  • gradient:定义边框的渐变颜色。
  • borderWidth:定义边框的宽度。
  • blurRadius:定义发光效果的模糊半径。
  • child:定义被边框包围的子组件。

你可以根据需要调整这些参数来实现不同的效果。此外,shape属性也可以根据你的需求调整为BoxShape.rectangle或其他形状。

希望这个示例能帮助你理解如何在Flutter中使用gradient_glow_border插件来创建渐变发光边框。

回到顶部