提问者:小点点

如果通过渐变,则框装饰中的渐变或颜色


  InkWell buildAppBarActionItem({
     Gradient gradient,
    String icon,
    double width = 36,
    double height = 36,
    double radius = 12,
  
    Color color,
    Function onpressed,
    double opacity = 1,
  }) {
    return InkWell(
      onTap: onpressed,
      child: Container(
        width: width,
        height: height,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(
            Radius.circular(radius),
          ),
          **if(color){
            color: color.withOpacity(opacity)
           }
          else{
          gradient: StyleBtn.buildLinearGradient(begin: Alignment.topLeft, eng: Alignment.topRight)
             }**

          // color? color: color.withOpacity(opacity) : gradient: StyleBtn.buildLinearGradient(begin: Alignment.topLeft, eng: Alignment.topRight),
        ),
        child: Center(
          child: ImageIcon(
            AssetImage("assets/icon/${icon}"),
            size: 16.0,
            color: MyTheme.white,
          ),
        ),
     
      ),
    );
  }

我想显示渐变,如果渐变被传递给函数,或者如果颜色被传递,颜色被显示。我已经实现了这个,但是它需要一个标识符。不知道如何把这个放在哪里


共1个答案

匿名用户

试试这个:

InkWell buildAppBarActionItem({
     Gradient? gradient,
    String? icon,
    double width = 36,
    double height = 36,
    double radius = 12,
  
    Color? color,
    Function()? onpressed,
    double opacity = 1,
  }) {
    return InkWell(
      onTap: onpressed,
      child: Container(
        width: width,
        height: height,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(
            Radius.circular(radius),
          ),
          color: color != null? color.withOpacity(opacity) : null,
          gradient: color == null? StyleBtn.buildLinearGradient(begin: Alignment.topLeft, eng: Alignment.topRight) : null,
        ),
        child: Center(
          child: ImageIcon(
            AssetImage("assets/icon/${icon}"),
            size: 16.0,
            color: MyTheme.white,
          ),
        ),
     
      ),
    );
  }

在不可为空的应用程序中执行以下操作:

InkWell buildAppBarActionItem({
     Gradient gradient,
    String icon,
    double width = 36,
    double height = 36,
    double radius = 12,
  
    Color color,
    Function() onpressed,
    double opacity = 1,
  }) {
    return InkWell(
      onTap: onpressed,
      child: Container(
        width: width,
        height: height,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(
            Radius.circular(radius),
          ),
          color: color != null? color.withOpacity(opacity) : null,
          gradient: color == null? StyleBtn.buildLinearGradient(begin: Alignment.topLeft, eng: Alignment.topRight) : null,
        ),
        child: Center(
          child: ImageIcon(
            AssetImage("assets/icon/${icon}"),
            size: 16.0,
            color: MyTheme.white,
          ),
        ),
     
      ),
    );
  }