Flutter自定义按钮样式插件stylishbutton的使用

Flutter 自定义按钮样式插件 stylishbutton 的使用

特性

Stylish Button 包允许你在应用中添加一个漂亮、时尚且带有渐变效果的按钮。

安装

  1. pubspec.yaml 文件中添加最新的包版本(并运行 flutter pub get):
    dependencies:
      flutter:
        sdk: flutter
      stylishbutton: any
    
  2. 导入包并在你的应用中使用它。

使用示例

import 'package:flutter/material.dart';
import 'package:stylishbutton/stylishbutton.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(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Stylish Buttons'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  [@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(widget.title),
      ),
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const [
              SizedBox(
                height: 30,
              ),
              Text("Square Button", style: TextStyle(color: Colors.black, fontWeight: FontWeight.w500, fontSize: 20)),
              SizedBox(
                height: 20,
              ),
              SquareButton(text: 'Simple Button'),
              SizedBox(
                height: 5,
              ),
              SquareGradButton(
                  text: 'Gradient Button',
                  grad1: Colors.blue,
                  grad2: Colors.deepPurpleAccent),
              SizedBox(
                height: 5,
              ),
              SquareButtonWithImage(
                text: "Google Login",
                assetsImage: 'assets/google.png'
              ),
              SizedBox(
                height: 5,
              ),
              SquareGradButtonWithImage(
                text: "Google Login",
                assetsImage: 'assets/google.png',
                grad1: Colors.blue,
                grad2: Colors.deepPurpleAccent,
              ),
              SizedBox(
                height: 5,
              ),
              SquareButtonWithTwoImage(text: "Google Login",
                  assetsImage: 'assets/google.png', iconColor: Colors.white),
              SizedBox(
                height: 5,
              ),
              SquareGradButtonWithTwoImage(text: "Google Login", assetsImage: 'assets/google.png', iconColor: Colors.white,  grad1: Colors.blue,
                grad2: Colors.deepPurpleAccent),
              SizedBox(
                height: 20,
              ),
              Text("Curve Button", style: TextStyle(color: Colors.black, fontWeight: FontWeight.w500, fontSize: 20)),
              SizedBox(
                height: 20,
              ),
              CurveButton(text: "Curve Button"),
              SizedBox(
                height: 5,
              ),
              CurveGradButton(
                  text: 'Gradient Button',
                  grad1: Colors.blue,
                  grad2: Colors.deepPurpleAccent),
              SizedBox(
                height: 5,
              ),
              CurveButtonWithImage(
                  text: "Google Login",
                  assetsImage: 'assets/google.png'
              ),
              SizedBox(
                height: 5,
              ),
              CurveGradButtonWithImage(
                text: "Google Login",
                assetsImage: 'assets/google.png',
                grad1: Colors.blue,
                grad2: Colors.deepPurpleAccent,
              ),
              SizedBox(
                height: 5,
              ),
              CurveButtonWithTwoImage(text: "Google Login",
                assetsImage: 'assets/google.png', iconColor: Colors.white),
              SizedBox(
                height: 5,
              ),
              CurveGradButtonWithTwoImage(text: "Google Login", assetsImage: 'assets/google.png', iconColor: Colors.white,  grad1: Colors.blue,
                grad2: Colors.deepPurpleAccent),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter自定义按钮样式插件stylishbutton的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义按钮样式插件stylishbutton的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


stylishbutton 是一个用于 Flutter 的自定义按钮样式插件,它允许开发者轻松地创建具有各种样式和动画效果的按钮。以下是使用 stylishbutton 插件的基本步骤和方法。

1. 添加依赖

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

dependencies:
  stylishbutton: ^1.0.0  # 请使用最新版本

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

2. 导入库

在需要使用 stylishbutton 的 Dart 文件中导入库:

import 'package:stylishbutton/stylishbutton.dart';

3. 基本用法

stylishbutton 提供了多种预定义的按钮样式和动画效果。你可以通过简单的配置来创建自定义按钮。

StylishButton(
  onPressed: () {
    // 按钮点击事件
    print('Button Pressed!');
  },
  text: 'Click Me',
  textColor: Colors.white,
  backgroundColor: Colors.blue,
  borderRadius: 10.0,
  elevation: 5.0,
  padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
  animationType: AnimationType.Slide, // 动画类型
  icon: Icons.arrow_forward, // 可选的图标
  iconAlignment: IconAlignment.right, // 图标位置
)

4. 参数说明

以下是一些常用的参数及其作用:

  • onPressed: 按钮点击事件回调。
  • text: 按钮显示的文本。
  • textColor: 文本颜色。
  • backgroundColor: 按钮背景颜色。
  • borderRadius: 按钮边框圆角半径。
  • elevation: 按钮阴影高度。
  • padding: 按钮内边距。
  • animationType: 按钮动画类型(如 AnimationType.Slide, AnimationType.Fade, 等)。
  • icon: 可选的图标。
  • iconAlignment: 图标的位置(如 IconAlignment.left, IconAlignment.right)。

5. 自定义动画

stylishbutton 提供了多种动画效果,你可以通过 animationType 参数来选择不同的动画类型:

StylishButton(
  onPressed: () {
    print('Button Pressed!');
  },
  text: 'Slide Animation',
  animationType: AnimationType.Slide,
)

StylishButton(
  onPressed: () {
    print('Button Pressed!');
  },
  text: 'Fade Animation',
  animationType: AnimationType.Fade,
)

6. 复杂样式

你还可以通过组合不同的参数来创建更复杂的按钮样式:

StylishButton(
  onPressed: () {
    print('Button Pressed!');
  },
  text: 'Complex Button',
  textColor: Colors.white,
  backgroundColor: Colors.purple,
  borderRadius: 20.0,
  elevation: 10.0,
  padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),
  animationType: AnimationType.Rotate,
  icon: Icons.check,
  iconAlignment: IconAlignment.left,
)

7. 完整示例

以下是一个完整的示例,展示了如何使用 stylishbutton 创建一个带有动画效果的自定义按钮:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('StylishButton Example'),
        ),
        body: Center(
          child: StylishButton(
            onPressed: () {
              print('Button Pressed!');
            },
            text: 'Click Me',
            textColor: Colors.white,
            backgroundColor: Colors.blue,
            borderRadius: 10.0,
            elevation: 5.0,
            padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
            animationType: AnimationType.Slide,
            icon: Icons.arrow_forward,
            iconAlignment: IconAlignment.right,
          ),
        ),
      ),
    );
  }
}
回到顶部