简体   繁体   English

水平列表视图 flutter 的模糊边缘

[英]blur edeges of horizontal listview flutter

I want to blur the right and left edges of horizontal ListView.我想模糊水平 ListView 的左右边缘。 Now all I got is this:现在我得到的是:

在此处输入图像描述

Could it be done?能做到吗?

It can be done in few ways:可以通过以下几种方式完成:

  • Using Stack with 3 Positioned widgets(left blur with defined width with z=1, your full-width list with z = 0, right blur with defined width and z=1)Stack与 3 个Positioned小部件一起使用(左模糊,定义宽度,z=1,全宽度列表,z = 0,右模糊,定义宽度和 z=1)
  • Using ShaderMask使用ShaderMask

I will propose an example code for the second approach.我将为第二种方法提出一个示例代码。 This is how your BlurredHorizontalListView build method might look like:这就是您的BlurredHorizontalListView构建方法的样子:

@override
  Widget build(BuildContext context) {
    return Container(
      height: 400,
      child: ShaderMask(
        shaderCallback: (Rect bounds) {
          return LinearGradient(
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
            colors: <Color>[
              Colors.transparent,
              Colors.white,
              Colors.transparent
            ],
          ).createShader(bounds);
        },
        blendMode: BlendMode.dstIn,
        child: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            
            return Card(
              child: Container(
                width: 220,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Image.network(
                      YOUR_URL,
                      height: 190,
                      fit: BoxFit.cover,
                    ),
                  ],
                ),
              ),
            );
          },
          scrollDirection: Axis.horizontal,
          itemCount: 3,
        ),
      ),
    );
  }

Parts of the example are hardcoded, so feel free to adjust it by your needs.该示例的部分内容是硬编码的,因此请随意根据您的需要进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM