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

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

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

xy_custom_btn 是一个功能强大的自定义按钮插件,支持多种按钮效果,例如 titleimage 的不同布局方式以及按钮的高亮效果等。

特性

  • 支持 titleimage 的多种布局方式。
  • 提供按钮高亮效果。
  • 可灵活调整按钮样式。

开始使用

直接将插件添加到您的项目中即可开始使用。

使用示例

以下是一个完整的示例代码,展示了如何使用 xy_custom_button 插件创建各种类型的按钮。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('xy_custom_button 示例')),
        body: ExamplePage(),
      ),
    );
  }
}

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

class _ExamplePageState extends State<ExamplePage> {
  bool imgSelected = false;

  [@override](/user/override)
  Widget build(BuildContext context) {
    double margin = 16.0;

    late String text;
    late Widget content = Container(
      width: ScreenUtil.getScreenW(context),
      height: 44,
      color: Colors.red,
    );

    switch (widget.btnType) {
      case UseBtnType.onlyTitle:
        text = "只有文字的情况:";
        content = CustomButton(
          margin: EdgeInsets.only(left: margin, right: margin),
          title: "登录",
          fontSize: 18,
          backgroundColor: Color(0xFFFB1B1B),
          disabledBgColor: Color(0x80FB1B1B),
          width: ScreenUtil.getScreenW(context) - 2 * margin,
          height: 44,
          borderRadius: BorderRadius.circular(6),
          onPressed: (context, btn) {},
        );
        break;
      case UseBtnType.onlyImage:
        text = '只有图片的情况:';
        content = CustomButton(
          margin: EdgeInsets.only(left: margin),
          image: Image.asset('images/login_no_eye.png'),
          selectedImage: Image.asset('images/login_eye.png'),
          width: 50,
          height: 50,
          isSelected: imgSelected,
          onPressed: (context, btn) {
            setState(() {
              imgSelected = !imgSelected;
              print('imgSelected:$imgSelected');
            });
          },
        );
        break;
      case UseBtnType.horizontal:
        text = '水平布局情况:';
        content = Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleLeftImageRight,
              title: '标题左图片右',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 44,
              borderRadius: BorderRadius.circular(6),
            ),
            SizedBox(height: 10),
            CustomButton(
              positionType: CustomButtonTitleImagePosition.imageLeftTitleRight,
              title: '标题右图片左',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 44,
              borderRadius: BorderRadius.circular(6),
            ),
            SizedBox(height: 10),
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleLeftImageRight,
              title: '标题左图片右 左对齐',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 250,
              height: 44,
              contentAlignment: CustomButtonContentAlignment.left,
              hPadding: 10, // 左对齐10边距
              borderRadius: BorderRadius.circular(6),
            ),
            SizedBox(height: 10),
            Row(
              children: [
                CustomButton(
                  margin: EdgeInsets.only(left: margin),
                  positionType: CustomButtonTitleImagePosition.titleLeftImageRight,
                  title: '标题右图片左 撑开模式',
                  titleColor: Colors.black,
                  image: Image.asset('images/default_head_icon.png'),
                  space: 10,
                  backgroundColor: Color(0xFFFB1B1B),
                  borderRadius: BorderRadius.circular(6),
                ),
              ],
            ),
            SizedBox(height: 10),
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleLeftImageRight,
              title: '标题左图片右 between',
              titleColor: Colors.black,
              image: Image.asset('images/default_head_icon.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 300,
              height: 44,
              contentAlignment: CustomButtonContentAlignment.between,
              ltPadding: 10, // 左对齐10边距
              borderRadius: BorderRadius.circular(6),
            ),
          ],
        );
        break;
      case UseBtnType.vertical:
        text = '垂直布局情况:';
        content = Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleTopImageBottom,
              title: '标题上图片下',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 60,
              borderRadius: BorderRadius.circular(6),
            ),
            SizedBox(height: 10),
            CustomButton(
              positionType: CustomButtonTitleImagePosition.imageTopTitleBottom,
              title: '标题下图片上',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 60,
              borderRadius: BorderRadius.circular(6),
            ),
            SizedBox(height: 10),
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleTopImageBottom,
              title: '标题上图片下 左对齐',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 44,
              contentAlignment: CustomButtonContentAlignment.left,
              hPadding: 10,
              borderRadius: BorderRadius.circular(6),
            ),
            SizedBox(height: 10),
            CustomButton(
              margin: EdgeInsets.only(left: margin),
              positionType: CustomButtonTitleImagePosition.imageTopTitleBottom,
              title: '标题下图片上 撑开模式',
              titleColor: Colors.black,
              image: Image.asset('images/default_head_icon.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              borderRadius: BorderRadius.circular(6),
              stretchPadding: EdgeInsets.only(left: 15, top: 10), // 撑开边距
            ),
            SizedBox(height: 10),
            CustomButton(
              margin: EdgeInsets.only(left: margin),
              positionType: CustomButtonTitleImagePosition.titleTopImageBottom,
              title: '标题上图片下 between',
              titleColor: Colors.black,
              image: Image.asset('images/default_head_icon.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 100,
              contentAlignment: CustomButtonContentAlignment.between,
              ltPadding: 10, // 上对齐10边距
              borderRadius: BorderRadius.circular(6),
            ),
          ],
        );
        break;
      case UseBtnType.alignment:
        text = '各种对齐方式:';
        content = Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleLeftImageRight,
              contentAlignment: CustomButtonContentAlignment.left,
              title: '标题左图片右',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 44,
              borderRadius: BorderRadius.circular(6),
            ),
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleLeftImageRight,
              contentAlignment: CustomButtonContentAlignment.right,
              title: '标题左图片右',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 44,
              borderRadius: BorderRadius.circular(6),
            ),
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleLeftImageRight,
              contentAlignment: CustomButtonContentAlignment.top,
              title: '标题左图片右',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 44,
              borderRadius: BorderRadius.circular(6),
            ),
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleLeftImageRight,
              contentAlignment: CustomButtonContentAlignment.bottom,
              title: '标题左图片右',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 44,
              borderRadius: BorderRadius.circular(6),
            ),
            CustomButton(
              positionType: CustomButtonTitleImagePosition.titleLeftImageRight,
              contentAlignment: CustomButtonContentAlignment.between,
              title: '标题左图片右',
              titleColor: Colors.black,
              image: Image.asset('images/login_eye.png'),
              space: 10,
              backgroundColor: Color(0xFFFB1B1B),
              width: 200,
              height: 44,
              borderRadius: BorderRadius.circular(6),
            ),
          ],
        );
        break;
    }

    return Container(
      width: ScreenUtil.getScreenW(context),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            text,
            style: TextStyle(fontSize: 15),
          ),
          content,
        ],
      ),
    );
  }
}

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

1 回复

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


xy_custom_button 是一个假设的 Flutter 自定义按钮插件。为了帮助你理解如何使用这个插件,我将提供一个示例代码和步骤,假设这个插件已经发布在 pub.dev 上。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  xy_custom_button: ^1.0.0  # 假设版本号为 1.0.0

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

2. 导入插件

在你的 Dart 文件中导入 xy_custom_button 插件。

import 'package:xy_custom_button/xy_custom_button.dart';

3. 使用 XyCustomButton

假设 xy_custom_button 提供了一个 XyCustomButton 组件,你可以像使用其他 Flutter 组件一样使用它。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('XyCustomButton Example'),
        ),
        body: Center(
          child: XyCustomButton(
            onPressed: () {
              print('Button Pressed!');
            },
            text: 'Click Me',
            color: Colors.blue,
            textColor: Colors.white,
            borderRadius: 8.0,
            padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
          ),
        ),
      ),
    );
  }
}

4. 参数说明

假设 XyCustomButton 支持以下参数:

  • onPressed: 按钮点击时的回调函数。
  • text: 按钮上显示的文本。
  • color: 按钮的背景颜色。
  • textColor: 按钮文本的颜色。
  • borderRadius: 按钮的圆角半径。
  • padding: 按钮的内边距。

5. 运行项目

保存文件并运行你的 Flutter 项目。你应该会看到一个自定义按钮,点击按钮时会在控制台输出 “Button Pressed!”。

6. 自定义样式

你可以根据需要调整按钮的样式,例如更改颜色、圆角、内边距等。

XyCustomButton(
  onPressed: () {
    print('Custom Button Pressed!');
  },
  text: 'Custom Button',
  color: Colors.green,
  textColor: Colors.black,
  borderRadius: 20.0,
  padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),
),
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!