Flutter自定义按钮插件flutter_awesome_buttons的使用

发布于 1周前 作者 sinazl 来自 Flutter

Flutter自定义按钮插件flutter_awesome_buttons的使用

flutter_awesome_buttons 是一个用于创建各种自定义按钮的 Flutter 插件。通过使用这个插件,你可以轻松地在你的 Flutter 应用中添加不同风格的按钮,如成功按钮、危险按钮、图标按钮等。

使用示例

首先,确保你已经在 pubspec.yaml 文件中添加了 flutter_awesome_buttons 依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_awesome_buttons: ^最新版本号

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

接下来是一个完整的示例代码,展示了如何使用 flutter_awesome_buttons 创建不同的按钮样式:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

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

class _HomePageState extends State<HomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter Awesome Buttons"),
      ),
      body: ListView(
        children: <Widget>[
          Wrap(
            children: <Widget>[
              // 成功按钮
              SuccessButton(
                title: "Success Button",
                onPressed: () {
                  // 按钮点击事件处理
                },
              ),
              SizedBox(width: 2.0),
              // 危险按钮
              DangerButton(
                title: "Danger Button",
                onPressed: () {
                  // 按钮点击事件处理
                },
              ),
              SizedBox(width: 2.0),
              // 主要按钮
              PrimaryButton(
                title: "Primary Button",
                onPressed: () {
                  // 按钮点击事件处理
                },
              ),
              SizedBox(width: 2.0),
              // 警告按钮
              WarningButton(
                title: "Warning Button",
                onPressed: () {
                  // 按钮点击事件处理
                },
              ),
            ],
          ),
          Wrap(
            children: <Widget>[
              // 禁用按钮
              DisabledButtton(
                onPressed: () {},
                title: "Disabled Button",
              ),
              SizedBox(width: 2.0),
              // 暗色按钮
              DarkButtton(
                onPressed: () {},
                title: "Dark Button",
              ),
              // 带图标的按钮
              ButttonWithIcon(
                icon: Icons.local_cafe,
                title: "Icon Button",
                onPressed: () {},
              ),
              // 浮动图标按钮
              FloatingIconButton(
                icon: Icons.home,
                onPressed: () {},
              ),
              FloatingIconButton(
                icon: Icons.home,
                onPressed: () {},
                buttonColor: Colors.orange,
              ),
            ],
          ),
          Wrap(
            children: <Widget>[
              SizedBox(width: 2),
              // 主线按钮
              PrimaryLineButton(
                onPressed: () {},
                title: "Line Button",
              ),
              SizedBox(width: 2),
              // 默认线条按钮
              DefaultLineButton(
                onPressed: () {},
                title: "Default Line Button",
              ),
              // 次级线条按钮
              SecoundaryLineButton(
                onPressed: () {},
                title: "Secoundary Line Button",
              ),
              // 信息线条按钮
              InfoLineButton(
                onPressed: () {},
                title: "Info Line Button",
              ),
            ],
          ),
          Wrap(
            children: <Widget>[
              // 成功线条按钮
              SuccessLineButton(
                onPressed: () {},
                title: "Success Line Button",
              ),
              // 警告线条按钮
              WarningLineButton(
                onPressed: () {},
                title: "Warning Line Button",
              ),
              // 危险线条按钮
              DangerLineButton(
                onPressed: () {},
                title: "Danger Line Button",
              ),
            ],
          ),
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: <Widget>[
                // 圆角按钮
                RoundedButton(
                  onPressed: () {},
                  title: "Rounded Button",
                ),
                RoundedButtonWithIcon(
                  onPressed: () {},
                  icon: Icons.title,
                  title: "Rounded Button With Icon",
                  buttonColor: Colors.green,
                ),
                RoundedButtonWithIcon(
                  onPressed: () {},
                  icon: Icons.home,
                  title: "Home",
                  buttonColor: Colors.blueGrey,
                ),
                InfoButtton(
                  onPressed: () {},
                  title: "Info Button",
                ),
              ],
            ),
          ),
          Wrap(
            children: <Widget>[
              // 次级按钮
              SecondaryButtton(
                onPressed: () {},
                title: "Secondary Button",
              ),
              // 社交媒体按钮
              FacebookButton(
                onPressed: () {},
              ),
              TwitterButton(
                onPressed: () {},
              ),
              GithubButton(
                onPressed: () {},
              ),
              InstagramButton(
                onPressed: () {},
              ),
              LinkedinButton(
                onPressed: () {},
              ),
              MicrosoftButton(
                onPressed: () {},
              ),
              GoogleButton(
                onPressed: () {},
              ),
            ],
          ),
          Column(
            children: <Widget>[
              Wrap(
                children: <Widget>[
                  // 渐变按钮
                  GredientButton(
                    onPressed: () {},
                    splashColor: Colors.orange,
                    colors: [
                      Colors.red,
                      Colors.orange,
                    ],
                    title: "Gredient Button",
                  ),
                  GredientButton(
                    onPressed: () {},
                    splashColor: Colors.orange,
                    colors: [
                      Colors.blue,
                      Colors.blueGrey,
                    ],
                    title: "Gredient Button",
                  ),
                  GredientButton(
                    onPressed: () {},
                    splashColor: Colors.orange,
                    colors: [
                      Color(0xFFC33764),
                      Color(0xFF1D2671),
                    ],
                    title: "Gredient Button",
                  ),
                  GredientButton(
                    onPressed: () {},
                    splashColor: Colors.orange,
                    colors: [
                      Color(0xFFF7971E),
                      Color(0xFFFFD200),
                    ],
                    title: "Gredient Button",
                  ),
                ],
              ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithEmail(
                  onPressed: () {},
                ),
              ),
              SizedBox(height: 4.0),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithGoogle(
                  onPressed: () {},
                ),
              ),
              SizedBox(height: 4.0),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithMicrosoft(
                  onPressed: () {},
                ),
              ),
              SizedBox(height: 4.0),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithApple(
                  onPressed: () {},
                ),
              ),
              SizedBox(height: 4.0),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithPinterest(
                  onPressed: () {},
                ),
              ),
              SizedBox(height: 4.0),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithLinkedin(
                  onPressed: () {},
                ),
              ),
              SizedBox(height: 4.0),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithFacebook(
                  onPressed: () {},
                ),
              ),
              SizedBox(height: 4.0),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithTwitter(
                  onPressed: () {},
                ),
              ),
              SizedBox(height: 4.0),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithInstagram(
                  onPressed: () {},
                ),
              ),
              SizedBox(height: 4.0),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 60),
                child: SignInWithGithub(
                  onPressed: () {},
                ),
              ),
            ],
          )
        ],
      ),
    );
  }
}

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

1 回复

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


当然,下面是一个关于如何在Flutter项目中使用flutter_awesome_buttons插件来自定义按钮的代码示例。这个插件提供了多种样式的按钮,你可以根据需要选择或自定义按钮的外观。

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

dependencies:
  flutter:
    sdk: flutter
  flutter_awesome_buttons: ^1.0.0  # 请检查最新版本号

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

接下来是一个简单的示例,展示如何使用flutter_awesome_buttons中的一些按钮:

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

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

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Awesome Buttons Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // 示例:Google风格的按钮
            AwesomeButton(
              text: 'Google Button',
              color: Colors.red,
              iconData: Icons.google,
              onPressed: () {
                print('Google button pressed');
              },
              borderColor: Colors.transparent,
              borderWidth: 0.0,
              radius: 25.0,
              elevation: 5.0,
              height: 50.0,
              minWidth: double.infinity,
              fontSize: 16.0,
              fontWeight: FontWeight.bold,
            ),

            SizedBox(height: 20.0),

            // 示例:Facebook风格的按钮
            AwesomeButton(
              text: 'Facebook Button',
              color: Colors.blue,
              iconData: Icons.facebook,
              onPressed: () {
                print('Facebook button pressed');
              },
              borderColor: Colors.transparent,
              borderWidth: 0.0,
              radius: 25.0,
              elevation: 5.0,
              height: 50.0,
              minWidth: double.infinity,
              fontSize: 16.0,
              fontWeight: FontWeight.bold,
            ),

            SizedBox(height: 20.0),

            // 示例:Apple风格的按钮
            AwesomeButton(
              text: 'Apple Button',
              color: Colors.black,
              iconData: Icons.apple,
              iconColor: Colors.white,
              onPressed: () {
                print('Apple button pressed');
              },
              borderColor: Colors.white,
              borderWidth: 2.0,
              radius: 25.0,
              elevation: 5.0,
              height: 50.0,
              minWidth: double.infinity,
              fontSize: 16.0,
              fontWeight: FontWeight.bold,
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们展示了三种不同风格的按钮:Google风格、Facebook风格和Apple风格。每个按钮都有自定义的文本、颜色、图标、点击事件等属性。你可以根据需要调整这些属性来创建符合你应用风格的按钮。

请注意,flutter_awesome_buttons插件提供了更多的自定义选项,比如动画效果、阴影、渐变颜色等,你可以查阅官方文档获取更多详细信息:flutter_awesome_buttons

回到顶部