Flutter通用UI之Button

示例图:

在这里插入图片描述

功能描述:

按钮支持渐变色,按钮宽高定制,字体大小,字体颜色,边框颜色,按钮圆角,按钮图标,按钮图标、宽高定制,按钮文字与图标位置,按钮与图标间距定制

代码

import 'package:flutter/material.dart';

enum Position { TOP, BOTTOM, LEFT, RIGHT }

class GradientButton extends StatelessWidget {
  final List<Color> colors; //渐变色色值
  final double width; //按钮宽度
  final double height; //按钮高度
  final Color textColor; //按钮字体色值
  final double textSize; //按钮字体大小
  final double radius; //按钮圆角
  final String imagePath; //按钮图标
  final double imageWidth; //按钮图标宽度
  final double imageHeight; //按钮图标高度
  final double marge; //图标与文字间距
  final String text; //文字
  int position = -1; //图标位置
  final Color borderColor; //边框
  double _marge;
  VoidCallback onTap;

  GradientButton(
      {this.colors,
      this.width,
      this.height,
      this.textColor,
      this.textSize,
      this.radius,
      this.position,
      this.imagePath,
      this.imageWidth,
      this.imageHeight,
      this.marge,
      this.borderColor,
      @required this.text,
      this.onTap});

  @override
  Widget build(BuildContext context) {
    List<Color> _colors = colors ?? [Color(0xFFE251), Color(0xFDD108)];
    _marge = marge ?? 2;
    var _borderColor = borderColor ?? Colors.transparent;
    return GestureDetector(
      onTap: () {
        onTap();
      },
      child: DecoratedBox(
        decoration: BoxDecoration(
            gradient: LinearGradient(colors: _colors),
            borderRadius: BorderRadius.circular(radius ?? 8),
            border: Border.all(color: _borderColor, width: 1)),
        child: ConstrainedBox(
          constraints: BoxConstraints.tightFor(width: width, height: height),
          child: Center(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: DefaultTextStyle(
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: textSize,
                    color: textColor),
                child: _buildButtonWidget(),
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildButtonWidget() {
    if (Position.LEFT.index == position) {
      return _buildLeftImageButton();
    } else if (Position.RIGHT.index == position) {
      return _buildRightImageButton();
    } else if (Position.TOP.index == position) {
      return _buildTopImageButton();
    } else if (Position.BOTTOM.index == position) {
      return _buildBottomImageButton();
    } else {
      return _buildButtonNoImage();
    }
  }

  Widget _buildLeftImageButton() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Padding(
          padding: EdgeInsets.fromLTRB(0, 0, _marge, 0),
          child: Image.asset(
            imagePath,
            height: imageHeight,
            width: imageWidth,
          ),
        ),
        Text(text)
      ],
    );
  }

  Widget _buildRightImageButton() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(text),
        Padding(
          padding: EdgeInsets.fromLTRB(_marge, 0, 0, 0),
          child: Image.asset(
            imagePath,
            height: imageHeight,
            width: imageWidth,
          ),
        ),
      ],
    );
  }

  Widget _buildTopImageButton() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Padding(
          padding: EdgeInsets.fromLTRB(0, 0, 0, _marge),
          child: Image.asset(
            imagePath,
            height: imageHeight,
            width: imageWidth,
          ),
        ),
        Text(text)
      ],
    );
  }

  Widget _buildBottomImageButton() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(text),
        Padding(
          padding: EdgeInsets.fromLTRB(0, _marge, 0, 0),
          child: Image.asset(
            imagePath,
            height: imageHeight,
            width: imageWidth,
          ),
        ),
      ],
    );
  }

  Widget _buildButtonNoImage() {
    return Text(text);
  }
}

demo

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

class GradientButtonRoute extends StatefulWidget {
  @override
  _GradientButtonRouteState createState() => _GradientButtonRouteState();
}

class _GradientButtonRouteState extends State<GradientButtonRoute> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(20.0),
      child: Column(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: GradientButton(
              colors: [Color(0xffFFE251), Color(0xffFDD108)],
              height: 60.0,
              width: 100,
              textColor: Colors.red,
              textSize: 10,
              text: "上",
              position: Position.TOP.index,
              imagePath: "images/icons/icon_man.png",
              imageHeight: 20,
              imageWidth: 20,
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: GradientButton(
              colors: [Color(0xffFFE251), Color(0xffFDD108)],
              height: 60.0,
              width: 100,
              textColor: Colors.red,
              textSize: 10,
              text: "下",
              position: Position.BOTTOM.index,
              imagePath: "images/icons/icon_man.png",
              imageWidth: 20,
              imageHeight: 20,
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: GradientButton(
              colors: [Color(0xffFFE251), Color(0xffFDD108)],
              height: 50.0,
              width: 100,
              textColor: Colors.red,
              textSize: 20,
              text: "左",
              position: Position.LEFT.index,
              imagePath: "images/icons/icon_man.png",
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: GradientButton(
              colors: [Color(0xffFFE251), Color(0xffFDD108)],
              height: 50.0,
              width: 100,
              textColor: Colors.red,
              textSize: 20,
              radius: 5,
              text: "右",
              position: Position.RIGHT.index,
              imagePath: "images/icons/icon_man.png",
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: GradientButton(
              colors: [Color(0xffFFE251), Color(0xffFDD108)],
              height: 50.0,
              width: 100,
              textColor: Colors.red,
              textSize: 20,
              text: "正常",
              borderColor: Colors.black,
              onTap: () {
                print("00000");
              },
            ),
          ),
        ],
      ),
    );
  }
}

属性含义

属性名称含义
colors按钮背景色(支持渐变色)
width按钮宽度
height按钮高度
textColor按钮字体颜色
textSize按钮字体大小
radius按钮圆角角度
imagePath按钮图标
imageWidth按钮图标宽度
imageHeight按钮图标高度
marge图标与文字之间间距
text按钮文字
position图标位于文字的位置(Position.Left.index,Position.RIGHT.index,Position.TOP.index,Position.BOTTOM.index)
borderColor边框色值
onTap按钮点击事件

Demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值