Flutter渐变不同尺寸按钮插件gradiant_different_size_buttons的使用

Flutter渐变不同尺寸按钮插件gradiant_different_size_buttons的使用

特性

创建了圆形和普通渐变按钮。

开始使用

首先,在项目中添加插件依赖。在 pubspec.yaml 文件中添加以下内容:

dependencies:
  gradiant_different_size_buttons: ^版本号

然后运行以下命令以获取依赖项:

flutter pub get

接下来,导入包并编写代码。以下是示例代码:

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

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

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('CircleButton'),
      ),
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 第一个按钮
            CircleButton(
              iconColor: Colors.blue, // 图标颜色
              linearGradient: const LinearGradient(
                colors: [Color(0xFFf857a6), Color(0xFFff5858)], // 渐变色
              ),
              onPressed: () {}, // 点击事件
              width: 150, // 宽度
              height: 50, // 高度
              buttonChild: const Center(
                child: Icon(Icons.add), // 按钮内部内容
              ),
              borderRadius: BorderRadius.circular(10), // 圆角
            ),
            const SizedBox(height: 10), // 添加间距

            // 第二个按钮
            CircleButton(
              linearGradient: const LinearGradient(
                colors: [Color(0xFFDE6262), Color(0xFFFFB88C)],
              ),
              onPressed: () {},
              width: 100,
              height: 50,
              buttonChild: const Center(
                child: Icon(Icons.access_time_filled_rounded, color: Colors.white),
              ),
              borderRadius: BorderRadius.circular(20),
            ),
            const SizedBox(height: 10),

            // 第三个按钮
            CircleButton(
              linearGradient: const LinearGradient(
                colors: [Color(0xAAED4264), Color(0xFFFFEDBC)],
              ),
              iconColor: Colors.cyanAccent, // 图标颜色
              onPressed: () {},
              width: 50,
              height: 50,
              buttonChild: const Center(
                child: Icon(Icons.ac_unit),
              ),
              borderRadius: BorderRadius.circular(50),
            ),
            const SizedBox(height: 10),

            // 第四个按钮
            CircleButton(
              linearGradient: const LinearGradient(
                colors: [Color(0xAAED4264), Color(0xFFFFEDBC)],
              ),
              onPressed: () {},
              width: 50,
              height: 50,
              buttonChild: Icon(Icons.add_a_photo_rounded, color: Colors.deepPurpleAccent),
              borderRadius: BorderRadius.circular(50),
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


gradiant_different_size_buttons 是一个 Flutter 插件,用于创建具有渐变背景和不同尺寸的按钮。使用这个插件,你可以轻松地自定义按钮的外观和大小,使其适应你的应用设计需求。

以下是如何使用 gradiant_different_size_buttons 插件的步骤:

1. 添加依赖

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

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

然后运行 flutter pub get 来获取依赖。

2. 导入插件

在你的 Dart 文件中导入插件:

import 'package:gradiant_different_size_buttons/gradiant_different_size_buttons.dart';

3. 使用 GradientButton

GradientButton 是插件提供的一个小部件,你可以使用它来创建具有渐变背景的按钮。

GradientButton(
  onPressed: () {
    // 按钮点击事件
    print('Button Pressed!');
  },
  gradient: LinearGradient(
    colors: [Colors.blue, Colors.green],
    begin: Alignment.topLeft,
    end: Alignment.bottomRight,
  ),
  width: 200.0,
  height: 50.0,
  child: Text(
    'Click Me',
    style: TextStyle(color: Colors.white, fontSize: 18.0),
  ),
),

4. 自定义按钮

你可以通过调整 gradientwidthheightchild 等属性来自定义按钮的外观和尺寸。

  • gradient: 设置按钮的背景渐变。
  • width: 设置按钮的宽度。
  • height: 设置按钮的高度。
  • child: 设置按钮的内容,通常是一个 TextIcon

5. 完整示例

以下是一个完整的示例,展示了如何使用 GradientButton 创建不同尺寸和渐变的按钮。

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Gradient Buttons Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              GradientButton(
                onPressed: () {
                  print('Button 1 Pressed!');
                },
                gradient: LinearGradient(
                  colors: [Colors.blue, Colors.green],
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                ),
                width: 200.0,
                height: 50.0,
                child: Text(
                  'Button 1',
                  style: TextStyle(color: Colors.white, fontSize: 18.0),
                ),
              ),
              SizedBox(height: 20.0),
              GradientButton(
                onPressed: () {
                  print('Button 2 Pressed!');
                },
                gradient: LinearGradient(
                  colors: [Colors.purple, Colors.orange],
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                ),
                width: 150.0,
                height: 40.0,
                child: Text(
                  'Button 2',
                  style: TextStyle(color: Colors.white, fontSize: 16.0),
                ),
              ),
              SizedBox(height: 20.0),
              GradientButton(
                onPressed: () {
                  print('Button 3 Pressed!');
                },
                gradient: LinearGradient(
                  colors: [Colors.red, Colors.yellow],
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                ),
                width: 100.0,
                height: 30.0,
                child: Text(
                  'Button 3',
                  style: TextStyle(color: Colors.white, fontSize: 14.0),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
回到顶部