Flutter Button封装实践 2023-07-26 周三

2023-08-05  本文已影响0人  勇往直前888

简介

项目中用到的按钮,主要分为以下几类,现在准备封装一下:

企业微信截图_a57681a9-1c8d-4d4e-9e1a-a4990a72e512.png

实现方式

在这里,我们选择方案2:用Container模拟

属性设置

/// 按钮类型
enum PandaButtonType {
  main,
  second,
}
/// 按钮类型
enum PandaButtonSize {
  big,
  middle,
  small,
}
Map<String, Color?> get lightInfo => {
  PandaColorSring.text333333: const Color(0xFF333333),
  /// 其他的颜色定义
};

Map<String, Color?> get darkInfo => {
  PandaColorSring.text333333: const Color(0xFFC2C2CA),
  /// 其他的颜色定义
};

也就是命令模式是#333333,暗黑模式是#C2C2CA

样例代码

不能直接copy,仅供参考

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pandabuy/theme/panda_color_config.dart';

class PandaButton extends StatelessWidget {
  const PandaButton({
    Key? key,
    required this.text,
    this.enable = true,
    this.type = PandaButtonType.main,
    this.size = PandaButtonSize.big,
    this.margin,
    this.width,
    this.onClick,
  }) : super(key: key);

  final String text;
  final bool enable;
  final PandaButtonType type;
  final PandaButtonSize size;
  final EdgeInsetsGeometry? margin;
  final double? width;
  final void Function()? onClick;

  @override
  Widget build(BuildContext context) {
    /// 背景色
    Color backgroundColor;
    if (enable) {
      if (type == PandaButtonType.main) {
        backgroundColor = PandaColorConfig().background11BA66Both;
      } else {
        backgroundColor = PandaColorConfig().backgroundFFFFFF;
      }
    } else {
      backgroundColor = PandaColorConfig().backgroundD8D8D8;
    }

    /// 高度
    double height = 40.h;
    if (size == PandaButtonSize.middle) {
      height = 32.h;
    }
    if (size == PandaButtonSize.small) {
      height = 28.h;
    }

    /// 边框
    BoxBorder? border;
    if (enable) {
      if (type == PandaButtonType.second) {
        border = Border.all(
          color: PandaColorConfig().borderCCCCCC,
          width: 0.5,
        );
      }
    }

    /// 文本颜色
    Color textColor;
    if (enable) {
      if (type == PandaButtonType.main) {
        textColor = PandaColorConfig().textFFFFFFBoth;
      } else {
        textColor = PandaColorConfig().text333333;
      }
    } else {
      textColor = PandaColorConfig().text999999;
    }

    /// 文本大小
    double fontSize = 16.sp;
    if (size == PandaButtonSize.middle) {
      fontSize = 14.sp;
    }
    if (size == PandaButtonSize.small) {
      fontSize = 12.sp;
    }

    /// 文本字重
    FontWeight fontWeight = FontWeight.w600;
    if ((size == PandaButtonSize.big) && (type == PandaButtonType.second)) {
      fontWeight = FontWeight.w500;
    }
    if ((size == PandaButtonSize.middle) || (size == PandaButtonSize.small)) {
      fontWeight = FontWeight.w400;
    }

    return GestureDetector(
      onTap: () {
        if (enable) {
          if (onClick != null) {
            onClick!();
          }
        }
      },
      child: Container(
        width: width,
        height: height,
        margin: margin,
        padding: EdgeInsets.symmetric(horizontal: 8.w),
        decoration: BoxDecoration(
          color: backgroundColor,
          borderRadius: BorderRadius.circular(5.r),
          border: border,
        ),
        child: Center(
          child: Text(
            text,
            style: TextStyle(
              color: textColor,
              fontSize: fontSize,
              fontWeight: fontWeight,
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
    );
  }
}

/// 按钮类型
enum PandaButtonType {
  main,
  second,
}

/// 按钮类型
enum PandaButtonSize {
  big,
  middle,
  small,
}
上一篇 下一篇

猜你喜欢

热点阅读