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

标签 flutter flutter-layout

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

请找到我绘制的附图以供引用。

我已经将 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(...),

表示期望的图像:

enter image description here

最佳答案

作为一个选项,您可以通过更改 stops 来控制展开黑色。值(value)观

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

enter image description here

关于flutter - 如何在 flutter 中显示带有顶部和底部渐变阴影的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60951683/

相关文章:

flutter - 垂直分隔线未显示

flutter - 如何在flutter中从ModalBottomSheet刷新ParentStatefulWidget的状态

flutter - 弹出框编辑后无法保存日期

flutter - flutter 本地化中是否有僧伽罗语库?

flutter - Dart : How To add or Subtract Hours and minutes to TimeOfDay

flutter - 有没有办法在列中使用 ListView.Builder 而无需收缩包装?

flutter - 如何在 Flutter 中将两个 TextFormField 属性并排对齐

dart - 在 Flutter 中显示全屏图像 onTap

flutter - 从命名路由重定向用户

Flutter 连接节点/小部件图