Flutter渐变效果插件flutter_gradients_reborn的使用

Flutter渐变效果插件flutter_gradients_reborn的使用

Flutter Gradients Reborn

Header

声明:我所做的只是将项目迁移到具有空安全(我可能或可能不会添加一些渐变)。本项目的作者与原始项目无关。这是原始项目:https://pub.dev/packages/flutter_gradients

这是一个用Dart编写的精选渐变集合(https://webgradients.com 的Flutter端口)。目前只包含线性渐变。

安装

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

dependencies:
  flutter_gradients_reborn: ^1.0.0+7

使用

导入包

在你的Dart文件中导入以下包:

import 'package:flutter_gradients_reborn/flutter_gradients_reborn.dart';

如何使用

线性渐变

默认情况下,FlutterGradient将生成一个LinearGradient

GradientType type: GradientType.linear;
Container(
  width: 150,
  height: 150,
  decoration: BoxDecoration(
    shape: BoxShape.circle,
    gradient: FlutterGradient.warmFlame(),
  ),
);

或者

Container(
  width: 150,
  height: 150,
  decoration: BoxDecoration(
    shape: BoxShape.circle,
    gradient: FlutterGradient.warmFlame(type: GradientType.linear),
  ),
);

径向渐变

你可以自定义以下值:

  • center: 渐变中心,作为(-1.0, -1.0) x (1.0, 1.0) 方框内的偏移量。
  • radius: 渐变半径,为绘制框最短边长度的比例。
  • tileMode: 当渐变超出radius像素范围时,如何平铺该渐变。
GradientType type: GradientType.radial;
Container(
  width: 150,
  height: 150,
  decoration: BoxDecoration(
    shape: BoxShape.circle,
    gradient: FlutterGradient.warmFlame(
      type: GradientType.radial,
      center: Alignment.center,
      radius: 0.5,
      tileMode: TileMode.clamp,
    ),
  ),
);

扫描渐变

你可以自定义以下值:

  • center: 渐变中心,作为(-1.0, -1.0) x (1.0, 1.0) 方框内的偏移量。
  • startAngle: 停止 0.0 处的角度(弧度)。
  • endAngle: 停止 1.0 处的角度(弧度)。
  • tileMode: 当渐变超出radius像素范围时,如何平铺该渐变。
GradientType type: GradientType.sweep;
Container(
  width: 150,
  height: 150,
  decoration: BoxDecoration(
    shape: BoxShape.circle,
    gradient: FlutterGradient.warmFlame(
      type: GradientType.sweep,
      center: Alignment.center,
      startAngle: 0.0,
      endAngle: math.pi * 2,
      tileMode: TileMode.clamp,
    ),
  ),
);

示例

Flutter 示例

安装 Flutter

为了运行Flutter示例,你必须安装Flutter。有关安装说明,请参阅在线文档。

运行应用

  1. 打开Android模拟器、iOS模拟器或连接适当的移动设备进行调试。
  2. 打开终端。
  3. 进入example/lib/目录。
  4. 运行flutter doctor以确保所有Flutter依赖项正常工作。
  5. 运行flutter packages get
  6. 运行flutter run

以下是完整的示例代码:

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

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: const Text('Flutter Gradients'),
        ),
        body: MyApp(),
      ),
    ),
  );
}

class MyApp extends StatefulWidget {
  [@override](/user/override)
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    var listGradientName = FlutterGradientNames.values;

    return Center(
      child: ListView.separated(
        itemCount: listGradientName.length,
        separatorBuilder: (BuildContext context, int index) => Divider(),
        itemBuilder: (BuildContext context, int index) {
          var gradient = listGradientName[index];
          var name = FlutterGradients.names[gradient.index];

          return ListTile(
            title: Text(name),
            leading: AspectRatio(
              aspectRatio: 1,
              child: Container(
                decoration: BoxDecoration(
                    gradient: _findGradients(gradient), shape: BoxShape.circle),
              ),
            ),
            onTap: () {
              _onTap(name, gradient);
            },
          );
        },
      ),
    );
  }

  /// Find the gradient from his name in the list
  Gradient _findGradients(FlutterGradientNames gradient,
          [GradientType type = GradientType.linear]) =>
      FlutterGradients.findByName(gradient, type: type);

  /// onTap method
  _onTap(String name, FlutterGradientNames gradient) {
    showDialog(
        context: context,
        builder: (_) {
          var linearGradient = _findGradients(gradient);
          var radialGradient = _findGradients(gradient, GradientType.radial);
          var sweepGradient = _findGradients(gradient, GradientType.sweep);

          var linearGradientBoxDecoration =
              BoxDecoration(shape: BoxShape.circle, gradient: linearGradient);
          var radialGradientBoxDecoration =
              BoxDecoration(shape: BoxShape.circle, gradient: radialGradient);
          var sweepGradientBoxDecoration =
              BoxDecoration(shape: BoxShape.circle, gradient: sweepGradient);

          var size = 200.0;

          var linearGradientCircleContainer = Container(
              width: size,
              height: size,
              decoration: linearGradientBoxDecoration);
          var radialGradientCircleContainer = Container(
              width: size,
              height: size,
              decoration: radialGradientBoxDecoration);
          var sweepGradientCircleContainer = Container(
              width: size,
              height: size,
              decoration: sweepGradientBoxDecoration);

          return AlertDialog(
            title: Center(child: Text(name)),
            content: SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      Text("LinearGradient"),
                      Padding(
                          padding: EdgeInsets.all(10),
                          child: linearGradientCircleContainer),
                      Text("RadialGradient"),
                      Padding(
                          padding: EdgeInsets.all(10),
                          child: radialGradientCircleContainer),
                      Text("SweepGradient"),
                      Padding(
                          padding: EdgeInsets.all(10),
                          child: sweepGradientCircleContainer),
                    ],
                  )
                ],
              ),
            ),
          );
        });
  }
}

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

1 回复

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


flutter_gradients_reborn 是一个用于在 Flutter 应用中创建渐变效果的插件。它提供了多种渐变类型和配置选项,使得开发者可以轻松地在应用中实现复杂的渐变背景、文本渐变等效果。

安装插件

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

dependencies:
  flutter:
    sdk: flutter
  flutter_gradients_reborn: ^1.0.0  # 请使用最新版本

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

基本用法

1. 使用 GradientContainer

GradientContainer 是一个容器小部件,允许你为其背景设置渐变效果。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: GradientContainer(
            gradient: LinearGradient(
              colors: [Colors.blue, Colors.green],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
            ),
            child: Text('Hello, Gradient!', style: TextStyle(color: Colors.white)),
          ),
        ),
      ),
    );
  }
}

2. 使用 GradientText

GradientText 是一个文本小部件,允许你为文本设置渐变颜色。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: GradientText(
            'Gradient Text',
            gradient: LinearGradient(
              colors: [Colors.red, Colors.yellow],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
            ),
            style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold),
          ),
        ),
      ),
    );
  }
}

其他渐变类型

除了 LinearGradientflutter_gradients_reborn 还支持其他渐变类型,如 RadialGradientSweepGradient

使用 RadialGradient

GradientContainer(
  gradient: RadialGradient(
    colors: [Colors.blue, Colors.green],
    center: Alignment.center,
    radius: 0.5,
  ),
  child: Text('Radial Gradient', style: TextStyle(color: Colors.white)),
)

使用 SweepGradient

GradientContainer(
  gradient: SweepGradient(
    colors: [Colors.red, Colors.yellow, Colors.green, Colors.blue],
    center: Alignment.center,
    startAngle: 0.0,
    endAngle: 3.14,
  ),
  child: Text('Sweep Gradient', style: TextStyle(color: Colors.white)),
)

自定义渐变

你还可以通过 Gradient 类自定义渐变效果,使用 GradientContainerGradientText 来应用这些自定义渐变。

GradientContainer(
  gradient: Gradient(
    colors: [Colors.purple, Colors.orange],
    stops: [0.3, 0.7],
    tileMode: TileMode.mirror,
  ),
  child: Text('Custom Gradient', style: TextStyle(color: Colors.white)),
)
回到顶部