Flutter渐变浮动按钮插件gradient_floating_button的使用

Flutter渐变浮动按钮插件gradient_floating_button的使用

gradient_floating_button是一个用于创建带有线性渐变和径向渐变背景的浮动按钮(FAB)的Flutter插件。本文将介绍如何安装并使用该插件,以及提供一个完整的示例。

功能

  • 创建带有线性渐变背景的浮动按钮
  • 创建带有径向渐变背景的浮动按钮

截图

Screenshot_1 Screenshot_2

安装

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

dependencies:
  gradient_floating_button: ^0.0.2

确保获取最新版本,请访问Pub.dev以获取最新的版本号。

使用方法

线性渐变浮动按钮

您可以使用GradientFloatingButton().withLinearGradient()来创建带有线性渐变背景的浮动按钮:

floatingActionButton: GradientFloatingButton().withLinearGradient(
    onTap: _incrementCounter,
    iconWidget: const Icon(
        Icons.add,
        color: Colors.white,
    ),
    alignmentEnd: Alignment.topRight,
    alignmentBegin: Alignment.bottomLeft,
    colors: [Colors.blue, Colors.purpleAccent]),

径向渐变浮动按钮

您也可以使用GradientFloatingButton().withRadialGradient()来创建带有径向渐变背景的浮动按钮:

GradientFloatingButton().withRadialGradient(
    onTap: _incrementCounter,
    iconWidget: const Icon(
        Icons.add,
        color: Colors.white,
    ),
    radius: 0.4,
    colors: [Colors.indigo, Colors.blue])

完整示例

下面是一个完整的示例代码,展示了如何在应用中同时使用线性和径向渐变浮动按钮:

import 'package:flutter/material.dart';
import 'package:gradient_floating_button/gradient_floating_button.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(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Gradient Floating Button Demo'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          GradientFloatingButton().withLinearGradient(
              onTap: _incrementCounter,
              iconWidget: const Icon(
                Icons.add,
                color: Colors.white,
              ),
              alignmentEnd: Alignment.topRight,
              alignmentBegin: Alignment.bottomLeft,
              colors: [Colors.blue, Colors.purpleAccent]),
          const SizedBox(
            height: 10,
          ),
          GradientFloatingButton().withRadialGradient(
              onTap: _incrementCounter,
              iconWidget: const Icon(
                Icons.add,
                color: Colors.white,
              ),
              radius: 0.4,
              colors: [Colors.indigo, Colors.blue])
        ],
      ),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter项目中使用gradient_floating_button插件的示例代码。这个插件允许你创建一个具有渐变效果的浮动按钮。

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

dependencies:
  flutter:
    sdk: flutter
  gradient_floating_button: ^x.y.z  # 请将x.y.z替换为插件的最新版本号

然后,运行flutter pub get来安装依赖项。

接下来,在你的Flutter项目中,你可以使用GradientFloatingActionButton来创建一个渐变浮动按钮。以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Gradient Floating Button Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Gradient Floating Button Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Tap the button below',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 20),
            // 使用 GradientFloatingActionButton
            GradientFloatingActionButton(
              onPressed: () {
                // 按钮点击事件处理
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Button tapped!')),
                );
              },
              child: Icon(Icons.add),
              gradient: LinearGradient(
                colors: [Colors.blue, Colors.red],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
              elevation: 10,
              shape: GradientFloatingActionButtonShape.roundedRectangle(
                borderRadius: BorderRadius.circular(30),
              ),
              size: 70,
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个中心对齐的文本和一个渐变浮动按钮。GradientFloatingActionButton的属性包括:

  • onPressed: 按钮点击时的回调函数。
  • child: 按钮内的图标或子控件。
  • gradient: 按钮的渐变效果,使用LinearGradient来定义。
  • elevation: 按钮的阴影高度。
  • shape: 按钮的形状,这里使用了GradientFloatingActionButtonShape.roundedRectangle并指定了圆角半径。
  • size: 按钮的大小。

你可以根据需要调整这些属性来创建符合你需求的渐变浮动按钮。

回到顶部