如何在颤动中显示带有顶部和底部渐变阴影的图像?

Rag*_*ddy 7 flutter flutter-layout

我正在处理颤振以显示带有顶部和底部渐变叠加的图像。图像顶部和底部的某些部分应显示阴影,例如被灰色等其他颜色覆盖。

请找到我绘制的附图以供参考。

我已经将 Container 与 CachedNetworkImage 一起使用。并尝试使用 BoxDecoration。这没有给我预期的结果。以下代码的效果仅在图像下方应用阴影。但我想在我的图像顶部显示为叠加的顶部和底部渐变效果。

请给我建议。

代码参考:

 Container(
  height: 174.0,
  width: 116.0,
    decoration: new BoxDecoration(
    boxShadow: [
    BoxShadow(
    color: Colors.red,
    blurRadius: 20.0, // has the effect of softening the shadow
    spreadRadius: 5.0, // has the effect of extending the shadow
    offset: Offset(
    10.0, // horizontal, move right 10
    10.0, // vertical, move down 10
),
)
],),
child: CachedNetworkImage(...),
Run Code Online (Sandbox Code Playgroud)

表示期望的图像:

在此处输入图片说明

小智 7

作为一个选项,您可以通过更改stops值来控制散布黑色

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Container(
            width: 116.0,
            height: 174.0,
            foregroundDecoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [Colors.black, Colors.transparent, Colors.transparent, Colors.black],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                stops: [0, 0.2, 0.8, 1],
              ),
            ),
            child: Image.network('https://i.picsum.photos/id/200/400/600.jpg'),
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Pat*_*ick 5

我在下面为顶部和底部阴影框装饰写了一个代码,这意味着将有两个容器。您可以通过两种方式使用此解决方案,

  1. 嵌套容器,就是把你的图片放在 Container(topShadow) => Container(bottomShadow)=> Image

  2. 将您的 2 个容器和您的图像放入 Stack 中,对齐顶部和底部的容器,并为您的容器设置固定高度,就像您在图片中标记的区域一样。(确保容器在堆栈内的图像项下方,否则阴影将被图像覆盖)

 decoration: new BoxDecoration(
                gradient: new LinearGradient(
                  end: const Alignment(0.0, -1),
                  begin: const Alignment(0.0, 0.6),
                  colors: <Color>[
                    const Color(0x8A000000),
                    Colors.black12.withOpacity(0.0)
                  ],
                ),

              ),


decoration: new BoxDecoration(
                gradient: new LinearGradient(
                  end: const Alignment(0.0, -1),
                  begin: const Alignment(0.0, 0.6),
                  colors: <Color>[
                    const Color(0x8A000000),
                    Colors.black12.withOpacity(0.0)
                  ],
                ),

              ),

Run Code Online (Sandbox Code Playgroud)

第二种方法(第 2 点)工作代码:

Stack(
            children: <Widget>[
            //image code
            Image(..),
            //top grey shadow
            Align(
                alignment: Alignment.topCenter,
                child: Container(
                  height: 50,
                  width: double.infinity,
                  decoration: new BoxDecoration(
                    gradient: new LinearGradient(
                      end: const Alignment(0.0, 0.4),
                      begin: const Alignment(0.0, -1),
                      colors: <Color>[
                        const Color(0x8A000000),
                        Colors.black12.withOpacity(0.0)
                      ],
                    ),

                  ),
                ),
              ),
              //bottom grey shadow
              Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  height: 50,
                  width: double.infinity,
                  decoration: new BoxDecoration(
                    gradient: new LinearGradient(
                      end: const Alignment(0.0, -1),
                      begin: const Alignment(0.0, 0.4),
                      colors: <Color>[
                        const Color(0x8A000000),
                        Colors.black12.withOpacity(0.0)
                      ],
                    ),

                  ),
                ),
              ),
            
            ],),
Run Code Online (Sandbox Code Playgroud)