flutter - 如果传递了渐变,则框装饰中的渐变或颜色

标签 flutter dart colors linear-gradients

  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,
          ),
        ),
     
      ),
    );
  }

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

最佳答案

试试这个:

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,
          ),
        ),
     
      ),
    );
  }

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

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,
          ),
        ),
     
      ),
    );
  }

关于flutter - 如果传递了渐变,则框装饰中的渐变或颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73618268/

相关文章:

flutter - 在另一状态状态窗口中单击按钮时,如何在一个状态状态窗口中设置状态?

android - 可以使用 flutter run 命令进行热重载吗?

dart - 带有剪辑椭圆的 flutter 圆形文件图像

dart - json.decode 之后的空检查

flutter - 如何在子小部件中禁用底部的SafeArea或更改其颜色?

flutter - 从多个小部件访问StreamController流

json - HttpRequest.getString遇到错误功能,即使其成功

python-3.x - 在 Tkinter 中绘制从红色到绿色的刻度

python - 查找图像中检测到指定颜色的坐标

flutter - 如何在2种颜色的 flutter 中创建Flatbutton?不渐变,并排2种纯色